You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eagle.apache.org by ji...@apache.org on 2017/02/28 06:36:58 UTC

eagle git commit: [MINOR] add string:listSize siddhi extension

Repository: eagle
Updated Branches:
  refs/heads/master 6e0fc410d -> 7bfe55de7


[MINOR] add string:listSize siddhi extension

Author: wujinhu <wu...@126.com>

Closes #846 from wujinhu/EAGLE-848.


Project: http://git-wip-us.apache.org/repos/asf/eagle/repo
Commit: http://git-wip-us.apache.org/repos/asf/eagle/commit/7bfe55de
Tree: http://git-wip-us.apache.org/repos/asf/eagle/tree/7bfe55de
Diff: http://git-wip-us.apache.org/repos/asf/eagle/diff/7bfe55de

Branch: refs/heads/master
Commit: 7bfe55de7bf37242a0c60b47f3759fe706ff44b1
Parents: 6e0fc41
Author: wujinhu <wu...@126.com>
Authored: Tue Feb 28 14:36:52 2017 +0800
Committer: wujinhu <wu...@126.com>
Committed: Tue Feb 28 14:36:52 2017 +0800

----------------------------------------------------------------------
 .../StringListSizeFunctionExtension.java        | 100 +++++++++++++++++
 .../src/main/resources/string.siddhiext         |   3 +-
 .../StringListSizeFunctionExtensionTest.java    | 111 +++++++++++++++++++
 .../src/test/resources/string.siddhiext         |   3 +-
 4 files changed, 215 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/eagle/blob/7bfe55de/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/siddhiext/StringListSizeFunctionExtension.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/siddhiext/StringListSizeFunctionExtension.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/siddhiext/StringListSizeFunctionExtension.java
new file mode 100644
index 0000000..5d3d3ae
--- /dev/null
+++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/siddhiext/StringListSizeFunctionExtension.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eagle.alert.siddhiext;
+
+import org.apache.commons.collections.ListUtils;
+import org.apache.eagle.alert.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.wso2.siddhi.core.config.ExecutionPlanContext;
+import org.wso2.siddhi.core.executor.ExpressionExecutor;
+import org.wso2.siddhi.core.executor.function.FunctionExecutor;
+import org.wso2.siddhi.query.api.definition.Attribute;
+import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
+
+import java.util.List;
+
+public class StringListSizeFunctionExtension extends FunctionExecutor {
+    private static final Logger LOG = LoggerFactory.getLogger(StringListSizeFunctionExtension.class);
+
+    /**
+     * The initialization method for StringListSizeFunctionExtension, this method will be called before the other methods.
+     *
+     * @param attributeExpressionExecutors the executors of each function parameter
+     * @param executionPlanContext         the context of the execution plan
+     */
+    @Override
+    protected void init(ExpressionExecutor[] attributeExpressionExecutors, ExecutionPlanContext executionPlanContext) {
+        if (attributeExpressionExecutors.length != 1) {
+            throw new ExecutionPlanValidationException("Invalid no of arguments passed to string:listSize() function, "
+                    + "required 1, but found " + attributeExpressionExecutors.length);
+        }
+
+        Attribute.Type attributeType = attributeExpressionExecutors[0].getReturnType();
+        if (attributeType != Attribute.Type.STRING) {
+            throw new ExecutionPlanValidationException("Invalid parameter type found for the argument of string:listSize() function, "
+                    + "required " + Attribute.Type.STRING
+                    + ", but found " + attributeType.toString());
+        }
+    }
+
+    /**
+     * The main execution method which will be called upon event arrival.
+     * when there are more than one function parameter
+     * This method calculates subtraction of two List Of Strings
+     * Each String is a jobs string needs to be loaded
+     * @param data the runtime values of function parameters
+     * @return the function result
+     */
+    @Override
+    protected Object execute(Object[] data) {
+        return null;
+    }
+
+    @Override
+    protected Object execute(Object data) {
+        try {
+            return JsonUtils.jsonStringToList((String)data).size();
+        } catch (Exception e) {
+            LOG.warn("exception found {}", e);
+            return 0;
+        }
+    }
+
+    @Override
+    public void start() {
+    }
+
+    @Override
+    public void stop() {
+    }
+
+    @Override
+    public Attribute.Type getReturnType() {
+        return Attribute.Type.INT;
+    }
+
+    @Override
+    public Object[] currentState() {
+        return null;
+    }
+
+    @Override
+    public void restoreState(Object[] state) {
+    }
+}

http://git-wip-us.apache.org/repos/asf/eagle/blob/7bfe55de/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/resources/string.siddhiext
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/resources/string.siddhiext b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/resources/string.siddhiext
index ac8e099..7176611 100644
--- a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/resources/string.siddhiext
+++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/resources/string.siddhiext
@@ -16,4 +16,5 @@
  */
 
 empty=org.apache.eagle.alert.siddhiext.StringEmptyFunctionExtension
-subtract=org.apache.eagle.alert.siddhiext.StringSubtractFunctionExtension
\ No newline at end of file
+subtract=org.apache.eagle.alert.siddhiext.StringSubtractFunctionExtension
+listSize=org.apache.eagle.alert.siddhiext.StringListSizeFunctionExtension
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/eagle/blob/7bfe55de/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/siddhiext/StringListSizeFunctionExtensionTest.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/siddhiext/StringListSizeFunctionExtensionTest.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/siddhiext/StringListSizeFunctionExtensionTest.java
new file mode 100644
index 0000000..6cb3696
--- /dev/null
+++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/siddhiext/StringListSizeFunctionExtensionTest.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eagle.siddhiext;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.wso2.siddhi.core.ExecutionPlanRuntime;
+import org.wso2.siddhi.core.SiddhiManager;
+import org.wso2.siddhi.core.event.Event;
+import org.wso2.siddhi.core.stream.input.InputHandler;
+import org.wso2.siddhi.core.stream.output.StreamCallback;
+import org.wso2.siddhi.core.util.EventPrinter;
+
+import java.util.concurrent.Semaphore;
+
+public class StringListSizeFunctionExtensionTest {
+    private static final Logger LOG = LoggerFactory.getLogger(StringSubtractFunctionExtensionTest.class);
+
+    @Test
+    public void testStringListSize() throws Exception {
+        Semaphore semp = new Semaphore(1);
+        String ql = " define stream log(timestamp long, switchLabel string, port string, message string); " +
+                " from log select string:listSize(switchLabel) as alertKey insert into output; ";
+        SiddhiManager manager = new SiddhiManager();
+        ExecutionPlanRuntime runtime = manager.createExecutionPlanRuntime(ql);
+        runtime.addCallback("output", new StreamCallback() {
+            @Override
+            public void receive(Event[] events) {
+                EventPrinter.print(events);
+                Assert.assertTrue(events.length == 1);
+                Assert.assertTrue(Integer.parseInt(events[0].getData(0).toString()) == 5);
+                semp.release();
+            }
+        });
+
+        runtime.start();
+
+        InputHandler logInput = runtime.getInputHandler("log");
+        semp.acquire();
+        Event e = new Event();
+        e.setTimestamp(System.currentTimeMillis());
+        String ths = "[\"a\", \"b\", \"c\", \"d\", \"e\"]";
+        String rhs = "[\"b\", \"d\"]";
+        e.setData(new Object[] {System.currentTimeMillis(), ths, "port01", rhs});
+        logInput.send(e);
+
+        semp.acquire();
+        runtime.shutdown();
+
+    }
+
+    @Test
+    public void testStringListSize2() throws Exception {
+        Semaphore semp = new Semaphore(1);
+        String ql = " define stream log(timestamp long, site string, component string, resource string, host string, value string); " +
+                " from a = log[resource == \"hadoop.namenode.namenodeinfo.corruptfiles\"],\n" +
+                "b = log[component == a.component and resource == a.resource and host == a.host and a.value != b.value]\n" +
+                "select b.site as site, b.host as host, b.component as component, b.resource as resource, " +
+                "b.timestamp as timestamp, string:listSize(b.value) as newMissingBlocksNumber, string:listSize(a.value) as oldMissingBlocksNumber, string:subtract(b.value, a.value) as missingBlocks\n" +
+                "insert into output;";
+        SiddhiManager manager = new SiddhiManager();
+        ExecutionPlanRuntime runtime = manager.createExecutionPlanRuntime(ql);
+        runtime.addCallback("output", new StreamCallback() {
+            @Override
+            public void receive(Event[] events) {
+                EventPrinter.print(events);
+                Assert.assertTrue(events.length == 1);
+                Assert.assertTrue(Integer.parseInt(events[0].getData(5).toString()) == 5);
+                Assert.assertTrue(Integer.parseInt(events[0].getData(6).toString()) == 2);
+                Assert.assertTrue(events[0].getData(7).toString().equals("a\nc\ne"));
+                semp.release();
+            }
+        });
+
+        runtime.start();
+
+        InputHandler logInput = runtime.getInputHandler("log");
+        semp.acquire();
+        Event e = new Event();
+        e.setTimestamp(System.currentTimeMillis());
+        String rhs = "[\"b\", \"d\"]";
+        e.setData(new Object[] {System.currentTimeMillis(), "a", "a", "hadoop.namenode.namenodeinfo.corruptfiles", "port01", rhs});
+        logInput.send(e);
+
+        e.setTimestamp(System.currentTimeMillis());
+        String ths = "[\"a\", \"b\", \"c\", \"d\", \"e\"]";
+        e.setData(new Object[] {System.currentTimeMillis(), "a", "a", "hadoop.namenode.namenodeinfo.corruptfiles", "port01", ths});
+        logInput.send(e);
+
+        semp.acquire();
+        runtime.shutdown();
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/eagle/blob/7bfe55de/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/string.siddhiext
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/string.siddhiext b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/string.siddhiext
index ac8e099..7176611 100644
--- a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/string.siddhiext
+++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/string.siddhiext
@@ -16,4 +16,5 @@
  */
 
 empty=org.apache.eagle.alert.siddhiext.StringEmptyFunctionExtension
-subtract=org.apache.eagle.alert.siddhiext.StringSubtractFunctionExtension
\ No newline at end of file
+subtract=org.apache.eagle.alert.siddhiext.StringSubtractFunctionExtension
+listSize=org.apache.eagle.alert.siddhiext.StringListSizeFunctionExtension
\ No newline at end of file