Search in sources :

Example 21 with InterpreterCompletion

use of org.apache.zeppelin.interpreter.thrift.InterpreterCompletion in project zeppelin by apache.

the class NotebookServer method completion.

private void completion(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException {
    String paragraphId = (String) fromMessage.get("id");
    String buffer = (String) fromMessage.get("buf");
    int cursor = (int) Double.parseDouble(fromMessage.get("cursor").toString());
    Message resp = new Message(OP.COMPLETION_LIST).put("id", paragraphId);
    if (paragraphId == null) {
        conn.send(serializeMessage(resp));
        return;
    }
    final Note note = notebook.getNote(getOpenNoteId(conn));
    List<InterpreterCompletion> candidates = note.completion(paragraphId, buffer, cursor);
    resp.put("completions", candidates);
    conn.send(serializeMessage(resp));
}
Also used : InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Message(org.apache.zeppelin.notebook.socket.Message) WatcherMessage(org.apache.zeppelin.notebook.socket.WatcherMessage) InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) Note(org.apache.zeppelin.notebook.Note)

Example 22 with InterpreterCompletion

use of org.apache.zeppelin.interpreter.thrift.InterpreterCompletion in project metron by apache.

the class StellarInterpreterTest method testAutoCompletion.

/**
 * The interpreter should support auto-completion.
 */
@Test
public void testAutoCompletion() {
    // the user's input that needs auto-completed
    final String buffer = "TO_";
    // the cursor is at the end of the buffer
    int cursor = buffer.length();
    List<InterpreterCompletion> completions = interpreter.completion(buffer, cursor);
    // expect some completions to be offered
    assertTrue(completions.size() > 0);
    for (InterpreterCompletion iCompletion : completions) {
        String completion = iCompletion.getValue();
        // the auto-complete should include an open paren
        assertEquals("(", completion.substring(completion.length() - 1));
        // the candidate should be a valid, defined function
        String function = completion.substring(0, completion.length() - 1);
        Iterable<String> allFunctions = interpreter.getExecutor().getFunctionResolver().getFunctions();
        String definedFunction = Iterables.find(allFunctions, (fn) -> StringUtils.equals(fn, function));
        assertEquals(function, definedFunction);
    }
}
Also used : InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) Test(org.junit.jupiter.api.Test)

Example 23 with InterpreterCompletion

use of org.apache.zeppelin.interpreter.thrift.InterpreterCompletion in project zeppelin by apache.

the class PythonInterpreter method completion.

@Override
public List<InterpreterCompletion> completion(String buf, int cursor, InterpreterContext interpreterContext) throws InterpreterException {
    if (iPythonInterpreter != null) {
        return iPythonInterpreter.completion(buf, cursor, interpreterContext);
    }
    if (buf.length() < cursor) {
        cursor = buf.length();
    }
    String completionString = getCompletionTargetString(buf, cursor);
    String completionCommand = "__zeppelin_completion__.getCompletion('" + completionString + "')";
    LOGGER.debug("completionCommand: {}", completionCommand);
    pythonInterpretRequest = new PythonInterpretRequest(completionCommand, true);
    statementOutput = null;
    synchronized (statementSetNotifier) {
        statementSetNotifier.notify();
    }
    String[] completionList = null;
    synchronized (statementFinishedNotifier) {
        long startTime = System.currentTimeMillis();
        while (statementOutput == null && pythonProcessLauncher.isRunning()) {
            try {
                if (System.currentTimeMillis() - startTime > MAX_TIMEOUT_SEC * 1000) {
                    LOGGER.error("Python completion didn't have response for {}sec.", MAX_TIMEOUT_SEC);
                    break;
                }
                statementFinishedNotifier.wait(1000);
            } catch (InterruptedException e) {
                // not working
                LOGGER.info("wait drop");
                return new LinkedList<>();
            }
        }
        if (statementError) {
            return new LinkedList<>();
        }
        Gson gson = new Gson();
        completionList = gson.fromJson(statementOutput, String[].class);
    }
    // end code for completion
    if (completionList == null) {
        return new LinkedList<>();
    }
    List<InterpreterCompletion> results = new LinkedList<>();
    for (String name : completionList) {
        results.add(new InterpreterCompletion(name, name, StringUtils.EMPTY));
    }
    return results;
}
Also used : InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList)

Example 24 with InterpreterCompletion

use of org.apache.zeppelin.interpreter.thrift.InterpreterCompletion in project zeppelin by apache.

the class AlluxioInterpreter method completion.

@Override
public List<InterpreterCompletion> completion(String buf, int cursor, InterpreterContext interpreterContext) {
    String[] words = splitAndRemoveEmpty(splitAndRemoveEmpty(buf, "\n"), " ");
    String lastWord = "";
    if (words.length > 0) {
        lastWord = words[words.length - 1];
    }
    List<InterpreterCompletion> voices = new LinkedList<>();
    for (String command : keywords) {
        if (command.startsWith(lastWord)) {
            voices.add(new InterpreterCompletion(command, command, CompletionType.command.name()));
        }
    }
    return voices;
}
Also used : InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) LinkedList(java.util.LinkedList)

Example 25 with InterpreterCompletion

use of org.apache.zeppelin.interpreter.thrift.InterpreterCompletion in project zeppelin by apache.

the class ElasticsearchInterpreterTest method testCompletion.

@Theory
public void testCompletion(ElasticsearchInterpreter interpreter) {
    final List<InterpreterCompletion> expectedResultOne = Arrays.asList(new InterpreterCompletion("count", "count", CompletionType.command.name()));
    final List<InterpreterCompletion> expectedResultTwo = Arrays.asList(new InterpreterCompletion("help", "help", CompletionType.command.name()));
    final List<InterpreterCompletion> resultOne = interpreter.completion("co", 0, null);
    final List<InterpreterCompletion> resultTwo = interpreter.completion("he", 0, null);
    final List<InterpreterCompletion> resultAll = interpreter.completion("", 0, null);
    Assert.assertEquals(expectedResultOne, resultOne);
    Assert.assertEquals(expectedResultTwo, resultTwo);
    final List<String> allCompletionList = new ArrayList<>();
    for (final InterpreterCompletion ic : resultAll) {
        allCompletionList.add(ic.getName());
    }
    Assert.assertEquals(ElasticsearchInterpreter.COMMANDS, allCompletionList);
}
Also used : InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) ArrayList(java.util.ArrayList) Theory(org.junit.experimental.theories.Theory)

Aggregations

InterpreterCompletion (org.apache.zeppelin.interpreter.thrift.InterpreterCompletion)32 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)8 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)6 IOException (java.io.IOException)5 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)5 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Properties (java.util.Properties)4 Gson (com.google.gson.Gson)2 Map (java.util.Map)2 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)2 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)2 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)2 Notebook (org.apache.zeppelin.notebook.Notebook)2 Paragraph (org.apache.zeppelin.notebook.Paragraph)2 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 Connection (java.sql.Connection)1