use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.
the class AggregationTestCase method incrementalStreamProcessorTest27.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest26" }, expectedExceptions = StoreQueryCreationException.class)
public void incrementalStreamProcessorTest27() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest27");
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-06-** **:**:**\" " + "per 1000");
EventPrinter.print(events);
Thread.sleep(100);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.
the class AggregationTestCase method incrementalStreamProcessorTest28.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest27" }, expectedExceptions = StoreQueryCreationException.class)
public void incrementalStreamProcessorTest28() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest28");
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-06-02 00:00:00\", \"2017-06-01 00:00:00\" " + "per \"hours\"");
EventPrinter.print(events);
Thread.sleep(100);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.
the class AggregationTestCase method incrementalStreamProcessorTest40.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest39" })
public void incrementalStreamProcessorTest40() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest40");
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 " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
siddhiAppRuntime.start();
LocalDate currentDate = LocalDate.now();
String year = String.valueOf(currentDate.getYear());
String month = String.valueOf(currentDate.getMonth().getValue());
if (month.length() == 1) {
month = "0".concat(month);
}
Event[] events = siddhiAppRuntime.query("from stockAggregation " + "on symbol == \"IBM\" " + "within \"" + year + "-" + month + "-** **:**:** +05:30\" " + "per \"seconds\"; ");
AssertJUnit.assertEquals(null, events);
Thread.sleep(100);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.
the class AggregationTestCase 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(100);
Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 06:**:**\" " + "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();
}
use of org.wso2.siddhi.query.api.aggregation.Within in project siddhi by wso2.
the class AbsentLogicalPreStateProcessor method process.
@Override
public void process(ComplexEventChunk complexEventChunk) {
if (!this.active) {
return;
}
this.lock.lock();
boolean notProcessed = true;
try {
long currentTime = complexEventChunk.getFirst().getTimestamp();
if (currentTime >= this.lastArrivalTime + waitingTime) {
ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<>(false);
Iterator<StateEvent> iterator;
if (isStartState && stateType == StateInputStream.Type.SEQUENCE && newAndEveryStateEventList.isEmpty() && pendingStateEventList.isEmpty()) {
StateEvent stateEvent = stateEventPool.borrowEvent();
addState(stateEvent);
} else if (stateType == StateInputStream.Type.SEQUENCE && !newAndEveryStateEventList.isEmpty()) {
this.resetState();
}
this.updateState();
iterator = pendingStateEventList.iterator();
while (iterator.hasNext()) {
StateEvent stateEvent = iterator.next();
// Remove expired events based on within
if (withinStates.size() > 0) {
if (isExpired(stateEvent, currentTime)) {
iterator.remove();
continue;
}
}
// Collect the events that came before the waiting time
if (waitingTimePassed(currentTime, stateEvent)) {
iterator.remove();
if (logicalType == LogicalStateElement.Type.OR && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) == null) {
// OR Partner not received
stateEvent.addEvent(stateId, streamEventPool.borrowEvent());
retEventChunk.add(stateEvent);
} else if (logicalType == LogicalStateElement.Type.AND && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
// AND partner received but didn't send out
retEventChunk.add(stateEvent);
} else if (logicalType == LogicalStateElement.Type.AND && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) == null) {
// AND partner didn't receive
// Let the partner to process or not
stateEvent.addEvent(stateId, streamEventPool.borrowEvent());
}
}
}
retEventChunk.reset();
notProcessed = retEventChunk.getFirst() == null;
while (retEventChunk.hasNext()) {
StateEvent stateEvent = retEventChunk.next();
retEventChunk.remove();
sendEvent(stateEvent);
}
this.lastArrivalTime = 0;
}
} finally {
this.lock.unlock();
}
if (thisStatePostProcessor.nextEveryStatePerProcessor != null || (notProcessed && isStartState)) {
// If every or (notProcessed and startState), schedule again
long nextBreak;
if (lastArrivalTime == 0) {
nextBreak = siddhiAppContext.getTimestampGenerator().currentTime() + waitingTime;
} else {
nextBreak = lastArrivalTime + waitingTime;
}
this.scheduler.notifyAt(nextBreak);
}
}
Aggregations