use of io.siddhi.core.event.Event in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase1.
@Test
public void aggregationFunctionTestcase1() throws InterruptedException {
LOG.info("aggregationFunctionTestcase1 - count w/o group by");
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 count() as count " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select count " + "order by AGG_TIMESTAMP " + "insert all events 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;
}
}
});
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 });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
Thread.sleep(2000);
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);
// AGG_TIMESTAMP cannot be asserted as it is based on the time test case is run
List<Object[]> expected = Arrays.asList(new Object[] { 2L }, new Object[] { 2L }, new Object[] { 2L }, new Object[] { 2L }, new Object[] { 1L }, new Object[] { 1L }, new Object[] { 1L });
SiddhiTestHelper.waitForEvents(100, 7, inEventCount, 60000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 7, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.event.Event in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase2.
@Test(dependsOnMethods = "aggregationFunctionTestcase1")
public void aggregationFunctionTestcase2() throws InterruptedException {
LOG.info("aggregationFunctionTestcase1 - external timestamp count w/o group by");
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 count() as count " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select AGG_TIMESTAMP, count " + "order by AGG_TIMESTAMP " + "insert all events 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;
}
}
});
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:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
Thread.sleep(2000);
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[] { 1496289950000L, 2L }, new Object[] { 1496289952000L, 2L }, new Object[] { 1496289954000L, 2L }, new Object[] { 1496289956000L, 2L }, new Object[] { 1496290016000L, 1L }, new Object[] { 1496290076000L, 1L }, new Object[] { 1496293676000L, 1L });
SiddhiTestHelper.waitForEvents(100, 7, inEventCount, 60000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 7, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.event.Event in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase8.
@Test(dependsOnMethods = "aggregationFunctionTestcase7")
public void aggregationFunctionTestcase8() throws InterruptedException {
LOG.info("aggregationFunctionTestcase8 - count w/o group by on-demand query");
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 count() as count " + "aggregate by timestamp every sec, min ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
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:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
Thread.sleep(2000);
Event[] events = siddhiAppRuntime.query("" + "from stockAggregation " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select AGG_TIMESTAMP, count " + "order by AGG_TIMESTAMP;");
AssertJUnit.assertNotNull("Event arrived", events);
AssertJUnit.assertEquals("Number of success events", 7, events.length);
List<Object[]> actual = new ArrayList<>();
for (Event event : events) {
actual.add(event.getData());
}
List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, 2L }, new Object[] { 1496289952000L, 2L }, new Object[] { 1496289954000L, 2L }, new Object[] { 1496289956000L, 2L }, new Object[] { 1496290016000L, 1L }, new Object[] { 1496290076000L, 1L }, new Object[] { 1496293676000L, 1L });
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(actual, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.event.Event in project siddhi by wso2.
the class Aggregation1TestCase method incrementalStreamProcessorTest33.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest32" })
public void incrementalStreamProcessorTest33() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest33");
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(2000);
Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 06:**:**\" " + "per \"seconds\" " + "select * " + "order by AGG_TIMESTAMP ;");
EventPrinter.print(events);
Assert.assertNotNull(events);
AssertJUnit.assertEquals(1, events.length);
AssertJUnit.assertArrayEquals(new Object[] { 1513578087000L, "CISCO", 100.0, 200.0, 9600f }, events[0].getData());
Thread.sleep(100);
siddhiAppRuntime.shutdown();
}
use of io.siddhi.core.event.Event in project siddhi by wso2.
the class Aggregation1TestCase 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();
}
Aggregations