Search in sources :

Example 1 with ParamOption

use of org.apache.zeppelin.display.ui.OptionInput.ParamOption in project zeppelin by apache.

the class SubmarineInterpreter method createOldGUI.

private String createOldGUI(InterpreterContext context) {
    // submarine command - Format
    ParamOption[] commandOptions = new ParamOption[4];
    commandOptions[0] = new ParamOption(COMMAND_JOB_RUN, COMMAND_JOB_RUN);
    commandOptions[1] = new ParamOption(COMMAND_JOB_SHOW, COMMAND_JOB_SHOW);
    commandOptions[2] = new ParamOption(COMMAND_USAGE, COMMAND_USAGE);
    String command = (String) context.getGui().select("Submarine Command", commandOptions, "");
    String distributed = this.properties.getProperty(MACHINELEARNING_DISTRIBUTED_ENABLE, "false");
    if (command.equals(COMMAND_JOB_RUN)) {
        String inputPath = (String) context.getGui().textbox("Input Path(input_path)");
        String checkpoinkPath = (String) context.getGui().textbox("Checkpoint Path(checkpoint_path)");
        if (distributed.equals("true")) {
            String psLaunchCmd = (String) context.getGui().textbox("PS Launch Command");
        }
        String workerLaunchCmd = (String) context.getGui().textbox("Worker Launch Command");
    }
    return command;
}
Also used : ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption)

Example 2 with ParamOption

use of org.apache.zeppelin.display.ui.OptionInput.ParamOption in project zeppelin by apache.

the class InputTest method testFormExtraction.

@Test
public void testFormExtraction() {
    // textbox form
    String script = "${input_form=}";
    Map<String, Input> forms = Input.extractSimpleQueryForm(script, false);
    assertEquals(1, forms.size());
    Input form = forms.get("input_form");
    assertEquals("input_form", form.name);
    assertEquals("input_form", form.displayName);
    assertEquals("", form.defaultValue);
    assertTrue(form instanceof TextBox);
    // textbox form with display name & default value
    script = "${input_form(Input Form)=xxx}";
    forms = Input.extractSimpleQueryForm(script, false);
    form = forms.get("input_form");
    assertEquals("xxx", form.defaultValue);
    assertTrue(form instanceof TextBox);
    assertEquals("Input Form", form.getDisplayName());
    // password form with display name
    script = "${password:my_pwd(My Password)}";
    forms = Input.extractSimpleQueryForm(script, false);
    form = forms.get("my_pwd");
    assertTrue(form instanceof Password);
    assertEquals("My Password", form.getDisplayName());
    // selection form
    script = "${select_form(Selection Form)=op1,op1|op2(Option 2)|op3}";
    form = Input.extractSimpleQueryForm(script, false).get("select_form");
    assertEquals("select_form", form.name);
    assertEquals("op1", form.defaultValue);
    assertEquals("Selection Form", form.getDisplayName());
    assertTrue(form instanceof Select);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", null), new ParamOption("op2", "Option 2"), new ParamOption("op3", null) }, ((Select) form).getOptions());
    // checkbox form
    script = "${checkbox:checkbox_form=op1,op1|op2|op3}";
    form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
    assertEquals("checkbox_form", form.name);
    assertEquals("checkbox_form", form.displayName);
    assertTrue(form instanceof CheckBox);
    assertArrayEquals(new Object[] { "op1" }, (Object[]) form.defaultValue);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", null), new ParamOption("op2", null), new ParamOption("op3", null) }, ((CheckBox) form).getOptions());
    // checkbox form with multiple default checks
    script = "${checkbox:checkbox_form(Checkbox Form)=op1|op3,op1(Option 1)|op2|op3}";
    form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
    assertEquals("checkbox_form", form.name);
    assertEquals("Checkbox Form", form.displayName);
    assertTrue(form instanceof CheckBox);
    assertArrayEquals(new Object[] { "op1", "op3" }, (Object[]) form.defaultValue);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", "Option 1"), new ParamOption("op2", null), new ParamOption("op3", null) }, ((CheckBox) form).getOptions());
    // checkbox form with no default check
    script = "${checkbox:checkbox_form(Checkbox Form)=,op1(Option 1)|op2(Option 2)|op3(Option 3)}";
    form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
    assertEquals("checkbox_form", form.name);
    assertEquals("Checkbox Form", form.displayName);
    assertTrue(form instanceof CheckBox);
    assertArrayEquals(new Object[] {}, (Object[]) form.defaultValue);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", "Option 1"), new ParamOption("op2", "Option 2"), new ParamOption("op3", "Option 3") }, ((CheckBox) form).getOptions());
}
Also used : CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) TextBox(org.apache.zeppelin.display.ui.TextBox) Password(org.apache.zeppelin.display.ui.Password) ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption) Test(org.junit.Test)

Example 3 with ParamOption

use of org.apache.zeppelin.display.ui.OptionInput.ParamOption in project zeppelin by apache.

the class Input method getSimpleQuery.

public static String getSimpleQuery(Map<String, Object> params, String script, boolean noteForm) {
    String replaced = script;
    Pattern pattern = noteForm ? VAR_NOTE_PTN : VAR_PTN;
    Matcher match = pattern.matcher(replaced);
    while (match.find()) {
        int first = match.start();
        if (!noteForm && first > 0 && replaced.charAt(first - 1) == '$') {
            continue;
        }
        Input input = getInputForm(match);
        Object value;
        if (params.containsKey(input.name)) {
            value = params.get(input.name);
        } else {
            value = input.getDefaultValue();
        }
        String expanded;
        if (value instanceof Object[] || value instanceof Collection) {
            // multi-selection
            OptionInput optionInput = (OptionInput) input;
            String delimiter = input.argument;
            if (delimiter == null) {
                delimiter = DEFAULT_DELIMITER;
            }
            Collection<Object> checked = value instanceof Collection ? (Collection<Object>) value : Arrays.asList((Object[]) value);
            List<Object> validChecked = new LinkedList<>();
            for (Object o : checked) {
                // filter out obsolete checked values
                if (optionInput.getOptions() != null) {
                    for (ParamOption option : optionInput.getOptions()) {
                        if (option.getValue().equals(o)) {
                            validChecked.add(o);
                            break;
                        }
                    }
                }
            }
            if (validChecked.isEmpty()) {
                expanded = StringUtils.join(checked, delimiter);
            } else {
                params.put(input.name, validChecked);
                expanded = StringUtils.join(validChecked, delimiter);
            }
        } else {
            // single-selection
            expanded = StringUtils.defaultString((String) value, "");
        }
        replaced = match.replaceFirst(expanded);
        match = pattern.matcher(replaced);
    }
    return replaced;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) OptionInput(org.apache.zeppelin.display.ui.OptionInput) LinkedList(java.util.LinkedList) ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption) OptionInput(org.apache.zeppelin.display.ui.OptionInput) Collection(java.util.Collection)

Example 4 with ParamOption

use of org.apache.zeppelin.display.ui.OptionInput.ParamOption in project zeppelin by apache.

the class InterpreterLogicTest method should_extract_variable_and_choices.

@Test
public void should_extract_variable_and_choices() {
    // Given
    AngularObjectRegistry angularObjectRegistry = new AngularObjectRegistry("cassandra", null);
    when(intrContext.getAngularObjectRegistry()).thenReturn(angularObjectRegistry);
    when(intrContext.getGui().select(eq("name"), optionsCaptor.capture(), eq("'Paul'"))).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.ui.OptionInput.ParamOption) Test(org.junit.Test)

Example 5 with ParamOption

use of org.apache.zeppelin.display.ui.OptionInput.ParamOption in project zeppelin by apache.

the class Input method getInputForm.

private static Input getInputForm(Matcher match) {
    String hiddenPart = match.group(1);
    boolean hidden = false;
    if ("_".equals(hiddenPart)) {
        hidden = true;
    }
    String m = match.group(2);
    String namePart;
    String valuePart;
    int p = m.indexOf('=');
    if (p > 0) {
        namePart = m.substring(0, p);
        valuePart = m.substring(p + 1);
    } else {
        namePart = m;
        valuePart = null;
    }
    String varName;
    String displayName = null;
    String type = null;
    String arg = null;
    Object defaultValue = null;
    ParamOption[] paramOptions = null;
    // get var name type
    String varNamePart;
    String[] typeArray = getType(namePart);
    if (typeArray != null) {
        type = typeArray[0];
        arg = typeArray[1];
        varNamePart = typeArray[2];
    } else {
        varNamePart = namePart;
    }
    // get var name and displayname
    String[] varNameArray = getNameAndDisplayName(varNamePart);
    if (varNameArray != null) {
        varName = varNameArray[0];
        displayName = varNameArray[1];
    } else {
        varName = varNamePart.trim();
    }
    // get defaultValue
    if (valuePart != null) {
        // find default value
        int optionP = valuePart.indexOf(",");
        if (optionP >= 0) {
            // option available
            defaultValue = valuePart.substring(0, optionP);
            if (type != null && type.equals("checkbox")) {
                // checkbox may contain multiple default checks
                defaultValue = Input.splitPipe((String) defaultValue);
            }
            String optionPart = valuePart.substring(optionP + 1);
            String[] options = Input.splitPipe(optionPart);
            paramOptions = new ParamOption[options.length];
            for (int i = 0; i < options.length; i++) {
                String[] optNameArray = getNameAndDisplayName(options[i]);
                if (optNameArray != null) {
                    paramOptions[i] = new ParamOption(optNameArray[0], optNameArray[1]);
                } else {
                    paramOptions[i] = new ParamOption(options[i], null);
                }
            }
        } else {
            // no option
            defaultValue = valuePart;
        }
    }
    Input input = null;
    if (type == null) {
        if (paramOptions == null) {
            input = new TextBox(varName, (String) defaultValue);
        } else {
            input = new Select(varName, defaultValue, paramOptions);
        }
    } else if (type.equals("checkbox")) {
        input = new CheckBox(varName, (Object[]) defaultValue, paramOptions);
    } else if (type.equals("password")) {
        input = new Password(varName);
    } else {
        throw new RuntimeException("Could not recognize dynamic form with type: " + type);
    }
    input.setArgument(arg);
    if (!StringUtils.isBlank(displayName)) {
        // only set displayName when it is not empty (user explicitly specify it)
        // e.g. ${name(display_name)=value)
        input.setDisplayName(displayName);
    }
    input.setHidden(hidden);
    return input;
}
Also used : TextBox(org.apache.zeppelin.display.ui.TextBox) ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption) OptionInput(org.apache.zeppelin.display.ui.OptionInput) CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) Password(org.apache.zeppelin.display.ui.Password)

Aggregations

ParamOption (org.apache.zeppelin.display.ui.OptionInput.ParamOption)6 CheckBox (org.apache.zeppelin.display.ui.CheckBox)2 OptionInput (org.apache.zeppelin.display.ui.OptionInput)2 Password (org.apache.zeppelin.display.ui.Password)2 Select (org.apache.zeppelin.display.ui.Select)2 TextBox (org.apache.zeppelin.display.ui.TextBox)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 AngularObject (org.apache.zeppelin.display.AngularObject)1 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)1