siddhi

Siddhi 5, compare value before


I need that when I get a temperature higher than the maximum temperature stored or lower than the minimum stored, I send an alert with the new temperature to the alert stream.

The problem that as I get a higher or lower temperature, first adds it to the maximum and / or minimum and then makes the check, so I never skip the alert.

How can I do so that I first compare the new temperature that comes to me and jump on alert?

@App:name("MaxTenMinutesApp")
define stream CargoStream (temperature int);

@sink(type='log', prefix='ouput >')
define stream OutputStream(temperature int, temperatureMax int, temperatureMin int);

@sink(type='log', prefix='alert >')
define stream AlertStream(temperature int);

@info(name='Alert')
from OutputStream[temperatureMax < temperature or temperatureMin > temperature ]
select temperature
insert into AlertStream;

@info(name='MaxTenMinutes')
from CargoStream#window.time(5 min)
select temperature, max(temperature) as temperatureMax, min(temperature) as temperatureMin
insert into OutputStream;

// LOGS

[2020-02-26_10-41-49_404] INFO {org.wso2.siddhi.core.stream.output.sink.LogSink} - ouput > : Event{timestamp=1582710109402, data=[10, 10, 10], isExpired=false} (Encoded)
[2020-02-26_10-41-54_600] INFO {org.wso2.siddhi.core.stream.output.sink.LogSink} - ouput > : Event{timestamp=1582710114596, data=[11, 11, 10], isExpired=false} (Encoded)
[2020-02-26_10-41-59_462] INFO {org.wso2.siddhi.core.stream.output.sink.LogSink} - ouput > : Event{timestamp=1582710119461, data=[9, 11, 9], isExpired=false} (Encoded)

// EXAMPLE

VALUE   TEM MAX MIN  EVENT
5         5   5   5  SENT ALARM NEW MAX Y MIN
10       10  10   5  SENT ALARM NEW MAX
6         6  10   5  NOTHING
3         3  10   3  SENT ALARM NEW MIN

Solution

  • You'll have to maintain a placeholder with the last max and min temperatures and use it when input temperature comes in.

    Please refer the sample code

    @App:name("MaxTenMinutesApp")
    
    define stream CargoStream (temperature int);
    
    define table tempValueHolder (index int, temperatureMax int, temperatureMin int);
    
    -- Sets initial value
    define trigger triggerAtStart at 'start';
    
    @sink(type='log', prefix='ouput >')
    define stream OutputStream(temperature int, temperatureMax int, temperatureMin int);
    
    @sink(type='log', prefix='alert >')
    define stream AlertStream(temperature int);
    
    @info(name='Sets initial value')
    from triggerAtStart
    select 0 as index, 0 as temperatureMax, 0 as temperatureMin
    insert into tempValueHolder;
    
    @info(name='Alert')
    from CargoStream as strm
    join tempValueHolder as tbl
    select strm.temperature, tbl.temperatureMax, tbl.temperatureMin
    insert into OutputStream;
    
    from OutputStream[temperatureMax < temperature or temperatureMin > temperature ]
    select temperature
    insert into AlertStream;
    
    
    @info(name='MaxTenMinutes')
    from OutputStream#window.time(5 min)
    select max(temperature) as temperatureMax, min(temperature) as temperatureMin
    update tempValueHolder
    on tempValueHolder.index == 0;