Search code examples
wso2siddhistream-analyticsstreaming-analytics

Compare batches of average values with each other in WSO2 Stream Processor


I've written some code in Siddhi that logs/prints the average of a batch of the last 100 events. So the average for event 0-100, 101-200, etc. I now want to compare these averages with each other to find some kind of trend. In first place I just want to see if there is some simple downward of upward trend for a certain amount of averages. For example I want to compare all average values with all upcoming 1-10 average values.

I've looked into Siddhi documentation but I did not find the answer that I wanted. I tried some solutions with partitioning, but this did not work. The below code is what I have right now.

define stream HBStream(ID int, DateTime String, Result double);

@info(name = 'Average100Query')
from HBStream#window.lengthBatch(100)
select ID, DateTime, Result, avg(Result)
insert into OutputStream;

Solution

  • Siddhi sequences can be used to match the averages and to identify a trend, https://siddhi.io/en/v5.1/docs/query-guide/#sequence

    from every e1=HBStream, e2=HBStream[e2.avgResult > e1.avgResult], e3=HBStream[e3.avgResult > e2.avgResult]
    select e1.ID, e3.avgResult - e1.avgResult as tempDiff
    insert into TempDiffStream; 
    

    Please note you have to use partition to decide this patter per ID is you need averages to be calculated per Sensor. In your app, also use group by if you need average per sensor

    @info(name = 'Average100Query')
    from HBStream#window.lengthBatch(100)
    select ID, DateTime, Result, avg(Result) as avgResult
    group by ID
    insert into OutputStream;