From 7bcc058d8bbd717d9fa2e90e583d6ba64acd38c1 Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Thu, 17 Aug 2017 15:18:50 -0400 Subject: [PATCH 1/4] Small refactor --- .../stellar/common/shell/StellarShell.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) 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 0d2f0c363c..438da79958 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 @@ -283,11 +283,12 @@ private void handleStellar(String expression) { } /** - * 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)) { // list all functions @@ -301,7 +302,6 @@ private void handleMagic( String rawExpression) { } else if(MAGIC_VARS.equals(expression)) { // list all variables - executor.getVariables() .forEach((k,v) -> writeLine(String.format("%s = %s", k, v))); @@ -311,8 +311,8 @@ private void handleMagic( String 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) { @@ -324,6 +324,17 @@ private void handleDoc(String expression) { .forEach(doc -> write(doc)); } + /** + * 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. @@ -380,16 +391,12 @@ public int execute(ConsoleOperation output) throws InterruptedException { 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); } } From 52355a227a30ff82e752e40c436785508b7f2394 Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Thu, 17 Aug 2017 17:06:38 -0400 Subject: [PATCH 2/4] METRON-1117 Filters Functions Returns by '%functions' --- .../stellar/common/shell/StellarShell.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) 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 438da79958..c7772c578a 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.List; import java.util.Map; import java.util.Properties; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -287,26 +288,34 @@ private void handleStellar(String expression) { * @param rawExpression The expression to execute. */ private void handleMagic( String rawExpression) { - String expression = rawExpression.trim(); + String[] expression = rawExpression.trim().split(" "); - if(MAGIC_FUNCTIONS.equals(expression)) { + String command = expression[0]; + if(MAGIC_FUNCTIONS.equals(command)) { - // list all functions + // if '%functions FOO' then show only functions that contain 'FOO' + Predicate 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); } } @@ -431,7 +440,6 @@ else if(isMagic(lastToken)) { } } } - } private static String stripOff(String baseString, String lastBit) { @@ -441,4 +449,8 @@ private static String stripOff(String baseString, String lastBit) { } return baseString.substring(0, index); } + + protected Console getConsole() { + return console; + } } From 8f49f10e82b077aa1e69045eac0771f467cb6a2c Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Thu, 17 Aug 2017 17:13:17 -0400 Subject: [PATCH 3/4] Added docs to README --- metron-stellar/stellar-common/README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/metron-stellar/stellar-common/README.md b/metron-stellar/stellar-common/README.md index f12c77d84c..c9f9c2714a 100644 --- a/metron-stellar/stellar-common/README.md +++ b/metron-stellar/stellar-common/README.md @@ -1024,7 +1024,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 @@ -1038,7 +1038,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` From 4b8a8826d8059ef29bad5a1acdae6720d893d7de Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Thu, 17 Aug 2017 17:22:26 -0400 Subject: [PATCH 4/4] Remove unnecessary method --- .../org/apache/metron/stellar/common/shell/StellarShell.java | 4 ---- 1 file changed, 4 deletions(-) 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 c7772c578a..a860778b98 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 @@ -449,8 +449,4 @@ private static String stripOff(String baseString, String lastBit) { } return baseString.substring(0, index); } - - protected Console getConsole() { - return console; - } }