Repository: metron Updated Branches: refs/heads/master cd14cbeef -> 1a4f19c86
METRON-1117 Filter Functions Returned by `%functions` (nickwallen) closes apache/metron#704 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/1a4f19c8 Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/1a4f19c8 Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/1a4f19c8 Branch: refs/heads/master Commit: 1a4f19c86ed94742c08e8e264a7bb45ff9ad27a6 Parents: cd14cbe Author: nickwallen <[email protected]> Authored: Thu Aug 31 10:10:53 2017 -0400 Committer: nickallen <[email protected]> Committed: Thu Aug 31 10:10:53 2017 -0400 ---------------------------------------------------------------------- metron-stellar/stellar-common/README.md | 10 +++- .../stellar/common/shell/StellarShell.java | 55 +++++++++++++------- 2 files changed, 43 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metron/blob/1a4f19c8/metron-stellar/stellar-common/README.md ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/README.md b/metron-stellar/stellar-common/README.md index 8746e60..926132e 100644 --- a/metron-stellar/stellar-common/README.md +++ b/metron-stellar/stellar-common/README.md @@ -1052,7 +1052,7 @@ The REPL has a set of magic commands that provide the REPL user with information #### `%functions` -This command lists all functions resolvable in the Stellar environment. Stellar searches the classpath for Stellar functions. This can make it difficult in some cases to understand which functions are resolvable. +This command lists all functions resolvable in the Stellar environment. ``` [Stellar]>>> %functions @@ -1066,7 +1066,13 @@ STATS_POPULATION_VARIANCE, STATS_QUADRATIC_MEAN, STATS_SD, STATS_SKEWNESS, STATS STATS_SUM_LOGS, STATS_SUM_SQUARES, STATS_VARIANCE, TO_DOUBLE, TO_EPOCH_TIMESTAMP, TO_FLOAT, TO_INTEGER, TO_LOWER, TO_STRING, TO_UPPER, TRIM, URL_TO_HOST, URL_TO_PATH, URL_TO_PORT, URL_TO_PROTOCOL, WEEK_OF_MONTH, WEEK_OF_YEAR, YEAR -[Stellar]>>> +``` + +The list of functions returned can also be filtered by passing an argument. Only the functions containing the argument as a substring will be returned. + +``` +[Stellar]>>> %functions NET +IN_SUBNET ``` #### `%vars` http://git-wip-us.apache.org/repos/asf/metron/blob/1a4f19c8/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShell.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShell.java b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShell.java index 0d2f0c3..a860778 100644 --- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShell.java +++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShell.java @@ -54,6 +54,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -283,36 +284,44 @@ public class StellarShell extends AeshConsoleCallback implements Completion { } /** - * Handles user interaction when executing a Magic command. + * Executes a magic expression. * @param rawExpression The expression to execute. */ private void handleMagic( String rawExpression) { - String expression = rawExpression.trim(); - if(MAGIC_FUNCTIONS.equals(expression)) { + String[] expression = rawExpression.trim().split(" "); - // list all functions + String command = expression[0]; + if(MAGIC_FUNCTIONS.equals(command)) { + + // if '%functions FOO' then show only functions that contain 'FOO' + Predicate<String> nameFilter = (name -> true); + if(expression.length > 1) { + nameFilter = (name -> name.contains(expression[1])); + } + + // list available functions String functions = StreamSupport .stream(executor.getFunctionResolver().getFunctionInfo().spliterator(), false) .map(info -> String.format("%s", info.getName())) + .filter(nameFilter) .sorted() .collect(Collectors.joining(", ")); writeLine(functions); - } else if(MAGIC_VARS.equals(expression)) { + } else if(MAGIC_VARS.equals(command)) { // list all variables - executor.getVariables() .forEach((k,v) -> writeLine(String.format("%s = %s", k, v))); } else { - writeLine(ERROR_PROMPT + "undefined magic command: " + expression); + writeLine(ERROR_PROMPT + "undefined magic command: " + rawExpression); } } /** - * Handles user interaction when executing a doc command. - * @param expression The expression to execute. + * Executes a doc expression. + * @param expression The doc expression to execute. */ private void handleDoc(String expression) { @@ -325,6 +334,17 @@ public class StellarShell extends AeshConsoleCallback implements Completion { } /** + * Executes a quit. + */ + private void handleQuit() { + try { + console.stop(); + } catch (Throwable e) { + e.printStackTrace(); + } + } + + /** * Formats the Stellar function info object into a readable string. * @param info The stellar function info object. * @return A readable string. @@ -380,16 +400,12 @@ public class StellarShell extends AeshConsoleCallback implements Completion { handleDoc(expression); } else if (expression.equals("quit")) { - try { - console.stop(); - } catch (Throwable e) { - e.printStackTrace(); - } - } - else if(expression.charAt(0) == '#') { - return 0; - } - else { + handleQuit(); + + } else if(expression.charAt(0) == '#') { + return 0; // comment, do nothing + + } else { handleStellar(expression); } } @@ -424,7 +440,6 @@ public class StellarShell extends AeshConsoleCallback implements Completion { } } } - } private static String stripOff(String baseString, String lastBit) {
