Search in sources :

Example 36 with AngularObjectRegistry

use of org.apache.zeppelin.display.AngularObjectRegistry in project zeppelin by apache.

the class MockInterpreterAngular method interpret.

@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
    String[] stmt = st.split(" ");
    String cmd = stmt[0];
    String name = null;
    if (stmt.length >= 2) {
        name = stmt[1];
    }
    String value = null;
    if (stmt.length == 3) {
        value = stmt[2];
    }
    AngularObjectRegistry registry = context.getAngularObjectRegistry();
    if (cmd.equals("add")) {
        registry.add(name, value, context.getNoteId(), null);
        registry.get(name, context.getNoteId(), null).addWatcher(new AngularObjectWatcher(null) {

            @Override
            public void watch(Object oldObject, Object newObject, InterpreterContext context) {
                numWatch.incrementAndGet();
            }
        });
    } else if (cmd.equalsIgnoreCase("update")) {
        registry.get(name, context.getNoteId(), null).set(value);
    } else if (cmd.equals("remove")) {
        registry.remove(name, context.getNoteId(), null);
    }
    try {
        // wait for watcher executed
        Thread.sleep(500);
    } catch (InterruptedException e) {
        logger.error("Exception in MockInterpreterAngular while interpret Thread.sleep", e);
    }
    String msg = registry.getAll(context.getNoteId(), null).size() + " " + Integer.toString(numWatch.get());
    return new InterpreterResult(Code.SUCCESS, msg);
}
Also used : AngularObjectWatcher(org.apache.zeppelin.display.AngularObjectWatcher) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Example 37 with AngularObjectRegistry

use of org.apache.zeppelin.display.AngularObjectRegistry in project zeppelin by apache.

the class NotebookServer method angularObjectClientBind.

/**
   * Push the given Angular variable to the target
   * interpreter angular registry given a noteId
   * and a paragraph id
   */
protected void angularObjectClientBind(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws Exception {
    String noteId = fromMessage.getType("noteId");
    String varName = fromMessage.getType("name");
    Object varValue = fromMessage.get("value");
    String paragraphId = fromMessage.getType("paragraphId");
    Note note = notebook.getNote(noteId);
    if (paragraphId == null) {
        throw new IllegalArgumentException("target paragraph not specified for " + "angular value bind");
    }
    if (note != null) {
        final InterpreterGroup interpreterGroup = findInterpreterGroupForParagraph(note, paragraphId);
        final AngularObjectRegistry registry = interpreterGroup.getAngularObjectRegistry();
        if (registry instanceof RemoteAngularObjectRegistry) {
            RemoteAngularObjectRegistry remoteRegistry = (RemoteAngularObjectRegistry) registry;
            pushAngularObjectToRemoteRegistry(noteId, paragraphId, varName, varValue, remoteRegistry, interpreterGroup.getId(), conn);
        } else {
            pushAngularObjectToLocalRepo(noteId, paragraphId, varName, varValue, registry, interpreterGroup.getId(), conn);
        }
    }
}
Also used : InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Note(org.apache.zeppelin.notebook.Note) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) JsonObject(com.google.gson.JsonObject) AngularObject(org.apache.zeppelin.display.AngularObject) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Example 38 with AngularObjectRegistry

use of org.apache.zeppelin.display.AngularObjectRegistry in project zeppelin by apache.

the class InterpreterLogicTest method should_extract_variable_and_choices.

@Test
public void should_extract_variable_and_choices() throws Exception {
    //Given
    AngularObjectRegistry angularObjectRegistry = new AngularObjectRegistry("cassandra", null);
    when(intrContext.getAngularObjectRegistry()).thenReturn(angularObjectRegistry);
    when(intrContext.getGui().select(eq("name"), eq("'Paul'"), optionsCaptor.capture())).thenReturn("'Jack'");
    //When
    final String actual = helper.maybeExtractVariables("SELECT * FROM zeppelin.artists WHERE name={{name='Paul'|'Jack'|'Smith'}}", intrContext);
    //Then
    assertThat(actual).isEqualTo("SELECT * FROM zeppelin.artists WHERE name='Jack'");
    final List<ParamOption> paramOptions = asList(optionsCaptor.getValue());
    assertThat(paramOptions.get(0).getValue()).isEqualTo("'Paul'");
    assertThat(paramOptions.get(1).getValue()).isEqualTo("'Jack'");
    assertThat(paramOptions.get(2).getValue()).isEqualTo("'Smith'");
}
Also used : AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) ParamOption(org.apache.zeppelin.display.Input.ParamOption) Test(org.junit.Test)

Example 39 with AngularObjectRegistry

use of org.apache.zeppelin.display.AngularObjectRegistry in project zeppelin by apache.

the class InterpreterLogicTest method should_extract_variable_from_angular_object_registry.

@Test
public void should_extract_variable_from_angular_object_registry() throws Exception {
    //Given
    AngularObjectRegistry angularObjectRegistry = new AngularObjectRegistry("cassandra", null);
    angularObjectRegistry.add("id", "from_angular_registry", "noteId", "paragraphId");
    when(intrContext.getAngularObjectRegistry()).thenReturn(angularObjectRegistry);
    when(intrContext.getNoteId()).thenReturn("noteId");
    when(intrContext.getParagraphId()).thenReturn("paragraphId");
    //When
    final String actual = helper.maybeExtractVariables("SELECT * FROM zeppelin.demo WHERE id='{{id=John}}'", intrContext);
    //Then
    assertThat(actual).isEqualTo("SELECT * FROM zeppelin.demo WHERE id='from_angular_registry'");
    verify(intrContext, never()).getGui();
}
Also used : AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Test(org.junit.Test)

Example 40 with AngularObjectRegistry

use of org.apache.zeppelin.display.AngularObjectRegistry in project zeppelin by apache.

the class InterpreterLogicTest method should_extract_variable_and_default_value.

@Test
public void should_extract_variable_and_default_value() throws Exception {
    //Given
    AngularObjectRegistry angularObjectRegistry = new AngularObjectRegistry("cassandra", null);
    when(intrContext.getAngularObjectRegistry()).thenReturn(angularObjectRegistry);
    when(intrContext.getGui().input("table", "zeppelin.demo")).thenReturn("zeppelin.demo");
    when(intrContext.getGui().input("id", "'John'")).thenReturn("'John'");
    //When
    final String actual = helper.maybeExtractVariables("SELECT * FROM {{table=zeppelin.demo}} WHERE id={{id='John'}}", intrContext);
    //Then
    assertThat(actual).isEqualTo("SELECT * FROM zeppelin.demo WHERE id='John'");
}
Also used : AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Test(org.junit.Test)

Aggregations

AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)48 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 LinkedList (java.util.LinkedList)18 GUI (org.apache.zeppelin.display.GUI)18 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)18 Properties (java.util.Properties)16 AngularObject (org.apache.zeppelin.display.AngularObject)16 LocalResourcePool (org.apache.zeppelin.resource.LocalResourcePool)16 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)11 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)8 File (java.io.File)5 Map (java.util.Map)5 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)5 Note (org.apache.zeppelin.notebook.Note)5 Before (org.junit.Before)5 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)4 MockInterpreterA (org.apache.zeppelin.interpreter.remote.mock.MockInterpreterA)4 Message (org.apache.zeppelin.notebook.socket.Message)4 Job (org.apache.zeppelin.scheduler.Job)4