Search in sources :

Example 16 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.

the class AggregationTestCase method incrementalStreamProcessorTest12.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest11" })
public void incrementalStreamProcessorTest12() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest12");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "" + "@BufferSize('3') " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, " + "(price * quantity) as lastTradeValue, max(price) as maxPrice, min(price) as minPrice " + "group by symbol " + "aggregate by timestamp every sec...year  ; " + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "from inputStream as i join stockAggregation as s " + "within \"2017-06-01 04:05:50\", \"2017-06-01 04:06:57\" " + "per \"seconds\" " + "select AGG_TIMESTAMP, totalPrice, avgPrice, lastTradeValue, s.symbol, maxPrice, minPrice " + "order by AGG_TIMESTAMP " + "insert into tempStream; " + "@info(name = 'query1') " + "from tempStream " + "select totalPrice, avgPrice, lastTradeValue, symbol, maxPrice, minPrice " + "insert into outputStream ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : removeEvents) {
                        removeEventsList.add(event.getData());
                        removeEventCount.incrementAndGet();
                    }
                }
                eventArrived = true;
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:53 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289953000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289953000L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289952000L });
        // Thursday, June 1, 2017 4:05:51 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289951000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289951000L });
        // Thursday, June 1, 2017 4:05:53 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289953000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 140f, null, 200L, 11, 1496289953000L });
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496289954000L });
        // Thursday, June 1, 2017 4:06:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 1000f, null, 200L, 9, 1496290016000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { 240.0, 60.0, 700f, "WSO2", 70f, 50f }, new Object[] { 200.0, 100.0, 9600f, "IBM", 100f, 100f }, new Object[] { 1400.0, 700.0, 3500f, "IBM", 900f, 500f }, new Object[] { 300.0, 100.0, 1540f, "WSO2", 140f, 60f }, new Object[] { 400.0, 400.0, 3600f, "IBM", 400f, 400f }, new Object[] { 600.0, 600.0, 3600f, "IBM", 600f, 600f }, new Object[] { 1000.0, 1000.0, 9000f, "IBM", 1000f, 1000f });
        SiddhiTestHelper.waitForEvents(100, 7, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of success events", 7, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        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 17 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.

the class AggregationTestCase method incrementalStreamProcessorTest32.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest31" })
public void incrementalStreamProcessorTest32() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest32");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 26, 1513578087000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 96, 1513578087000L });
    Thread.sleep(100);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 **:**:**\" " + "per \"seconds\" " + "select * " + "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    for (int i = 0; i < events.length; i++) {
        switch(i) {
            case 1:
                AssertJUnit.assertArrayEquals(new Object[] { 1513578087000L, "CISCO", 100.0, 200.0, 9600f }, events[i].getData());
                break;
            default:
                AssertJUnit.assertEquals(1, events.length);
        }
    }
    Thread.sleep(100);
    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) Test(org.testng.annotations.Test)

Example 18 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.

the class AggregationTestCase method incrementalStreamProcessorTest37.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest36" }, expectedExceptions = StoreQueryCreationException.class)
public void incrementalStreamProcessorTest37() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest37");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    siddhiAppRuntime.start();
    Thread.sleep(100);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 11:51:27 +05:30\", 156 " + "per \"hours\"");
    EventPrinter.print(events);
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 19 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.

the class AggregationTestCase method incrementalStreamProcessorTest34.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest33" })
public void incrementalStreamProcessorTest34() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest34");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 26, 1513578087000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 96, 1513578087000L });
    Thread.sleep(100);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 06:21:**\" " + "per \"seconds\" " + "select * " + "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    for (int i = 0; i < events.length; i++) {
        switch(i) {
            case 1:
                AssertJUnit.assertArrayEquals(new Object[] { 1513578087000L, "CISCO", 100.0, 200.0, 9600f }, events[i].getData());
                break;
            default:
                AssertJUnit.assertEquals(1, events.length);
        }
    }
    Thread.sleep(100);
    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) Test(org.testng.annotations.Test)

Example 20 with Within

use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.

the class AggregationTestCase method incrementalStreamProcessorTest25.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest24" })
public void incrementalStreamProcessorTest25() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest25");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
    Thread.sleep(100);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-06-** **:**:**\" " + "per \"seconds\" " + "select * " + "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    for (int i = 0; i < events.length; i++) {
        switch(i) {
            case 0:
                AssertJUnit.assertArrayEquals(new Object[] { 1496289950000L, "WSO2", 60.0, 120.0, 700f }, events[i].getData());
                break;
            case 1:
                AssertJUnit.assertArrayEquals(new Object[] { 1496289952000L, "WSO2", 80.0, 160.0, 1600f }, events[i].getData());
                break;
            case 3:
                AssertJUnit.assertArrayEquals(new Object[] { 1496289954000L, "IBM", 100.0, 200.0, 9600f }, events[i].getData());
                break;
            default:
                AssertJUnit.assertEquals(3, events.length);
        }
    }
    Thread.sleep(100);
    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) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)135 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)101 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)100 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)90 TestUtil (org.wso2.siddhi.core.TestUtil)60 Event (org.wso2.siddhi.core.event.Event)37 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)28 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)27 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)23 BString (org.ballerinalang.model.values.BString)22 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)19 BJSON (org.ballerinalang.model.values.BJSON)18 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)6 Expression (org.wso2.siddhi.query.api.expression.Expression)6 Map (java.util.Map)4 Semaphore (java.util.concurrent.Semaphore)4 Header (org.wso2.carbon.messaging.Header)4 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)4 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)4