Search in sources :

Example 51 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow1.

@Test
public void testExternalTimeBatchWindow1() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 6 sec) " + "output all events; ";
    String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total  " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804342L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335814341L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335814345L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335824341L, "192.10.1.7" });
    Thread.sleep(1000);
    assertEquals("Event arrived", true, eventArrived);
    assertEquals("In Events ", 2, inEventCount);
    assertEquals("Remove Events ", 0, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 52 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow14.

@Test
public void testExternalTimeBatchWindow14() throws Exception {
    log.info("ExternalTimeBatchWindow test14");
    SiddhiManager siddhiManager = new SiddhiManager();
    String query = "define stream jmxMetric(cpu int, timestamp long); " + "define window jmxMetricWindow(cpu int, timestamp long) externalTimeBatch(timestamp, 10 sec);" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name='query1') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, count(1) as count insert into tmp;";
    SiddhiAppRuntime runtime = siddhiManager.createSiddhiAppRuntime(query);
    final AtomicInteger recCount = new AtomicInteger(0);
    runtime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            assertEquals(1, inEvents.length);
            recCount.incrementAndGet();
            double avgCpu = (Double) inEvents[0].getData()[0];
            if (recCount.get() == 1) {
                assertEquals(15, avgCpu, 0);
            } else if (recCount.get() == 2) {
                assertEquals(85, avgCpu, 0);
            }
            long count = (Long) inEvents[0].getData()[1];
            assertEquals(3, count);
        }
    });
    InputHandler input = runtime.getInputHandler("jmxMetric");
    runtime.start();
    // external events' time stamp less than the window, should not have event recieved in call back.
    long now = 0;
    int length = 3;
    for (int i = 0; i < length; i++) {
        input.send(new Object[] { 15, now + i * 10 });
    }
    // if the trigger event mix with the last window, we should see the avgValue is not expected
    for (int i = 0; i < length; i++) {
        // the first entity of the second round
        input.send(new Object[] { 85, now + 10000 + i * 10 });
    }
    // to trigger second round
    input.send(new Object[] { 10000, now + 10 * 10000 });
    Thread.sleep(1000);
    assertEquals(2, recCount.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 53 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow8.

@Test
public void testExternalTimeBatchWindow8() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test8");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 2 sec) " + "output all events; ";
    String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total  " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            for (Event event : inEvents) {
                inEventCount++;
                if (inEventCount == 1) {
                    assertEquals(4L, event.getData(2));
                } else if (inEventCount == 2) {
                    assertEquals(3L, event.getData(2));
                } else if (inEventCount == 3) {
                    assertEquals(5L, event.getData(2));
                } else if (inEventCount == 4) {
                    assertEquals(7L, event.getData(2));
                } else if (inEventCount == 5) {
                    assertEquals(2L, event.getData(2));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
    Thread.sleep(2100);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.7" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.8" });
    Thread.sleep(2100);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.91" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.92" });
    inputHandler.send(new Object[] { 1366335806606L, "192.10.1.9" });
    inputHandler.send(new Object[] { 1366335806690L, "192.10.1.10" });
    Thread.sleep(3000);
    assertEquals("Event arrived", true, eventArrived);
    assertEquals("In Events ", 5, inEventCount);
    assertEquals("Remove Events ", 0, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 54 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class FrequentWindowEventTbaleTestCase method testFrequentUniqueWindow1.

@Test
public void testFrequentUniqueWindow1() throws InterruptedException {
    log.info("FrequentWindow test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) frequent(2); ";
    String query = "" + "@info(name = 'query0') " + "from purchase[price >= 30] " + "insert into purchaseWindow; " + "" + "@info(name = 'query1') " + "from purchaseWindow " + "select cardNo, price " + "insert all events into PotentialFraud ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount += inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount += removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchase");
    siddhiAppRuntime.start();
    for (int i = 0; i < 2; i++) {
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 73.36f });
        inputHandler.send(new Object[] { "1234-3244-2432-123", 46.36f });
        inputHandler.send(new Object[] { "5768-3244-2432-5646", 48.36f });
        inputHandler.send(new Object[] { "9853-3244-2432-4125", 78.36f });
    }
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("In Event count", 8, inEventCount);
    AssertJUnit.assertEquals("Out Event count", 6, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 55 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class FrequentWindowEventTbaleTestCase method testFrequentUniqueWindow2.

@Test
public void testFrequentUniqueWindow2() throws InterruptedException {
    log.info("FrequentWindow test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) frequent(2, cardNo); ";
    String query = "" + "@info(name = 'query0') " + "from purchase[price >= 30] " + "insert into purchaseWindow; " + "" + "@info(name = 'query1') " + "from purchaseWindow " + "select cardNo, price " + "insert all events into PotentialFraud ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount += inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount += removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchase");
    siddhiAppRuntime.start();
    for (int i = 0; i < 2; i++) {
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 73.36f });
        inputHandler.send(new Object[] { "1234-3244-2432-123", 46.36f });
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 78.36f });
        inputHandler.send(new Object[] { "1234-3244-2432-123", 86.36f });
        inputHandler.send(new Object[] { "5768-3244-2432-5646", 48.36f });
    }
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("In Event count", 8, inEventCount);
    AssertJUnit.assertEquals("Out Event count", 0, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)348 Test (org.testng.annotations.Test)281 ArrayList (java.util.ArrayList)264 HashMap (java.util.HashMap)214 IOException (java.io.IOException)188 Event (org.wso2.siddhi.core.event.Event)187 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)186 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)186 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)182 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)160 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)153 Map (java.util.Map)116 SQLException (java.sql.SQLException)113 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)107 PreparedStatement (java.sql.PreparedStatement)106 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)96 Connection (java.sql.Connection)92 UserStoreException (org.wso2.carbon.user.api.UserStoreException)87 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)81 Resource (org.wso2.carbon.registry.core.Resource)78