Matt
Matt

Reputation: 13

Writing an "arrived" and "departed" query with siddhi using timeouts

I'm looking to replace Esper with Siddhi in my application. The esper statement right now is a "timeout" type pattern, where I need to report back when events of a unique "name" and "type" (just string values I can look for on the incoming events) arrive and depart. I know that the event has arrived when the event first arrives in my firstunique window, and I assume the event departs if I don't see any events of the same name and type in a user-defined timeout value. Here's what my esper statements look like(note that there's a lot more going on in the actual esper, I've just simplified this for the sake of example):

create window events_1.std:firstunique(name, type) as NameEvent

insert into events_1 select * from EventCycle[events]

on pattern [every event1=events_1->(timer:interval(4.0 sec) and not events_1(name=event1.name, type=event1.type))]delete from events_1 where name = event1.name AND type=event1.type

I then select the irstream from events_1 and by getting the incoming and removed events I then get the "arrived" and "departed" events from the window.

For the siddhi, the firstunique window is fairly straightforward (I think?):

from EventCycle#window.firstUnique('name')[ type=='type' ] select name, type insert into NameEvent

but I'm really drawing a blank on how to replace that esper "on pattern" with Siddhi. Can I use a single "from every" statement for this or will I need a different approach with Siddhi?

Any help setting me on the right path here will be appreciated!

Upvotes: 1

Views: 476

Answers (1)

Dilini
Dilini

Reputation: 787

One way of achieving your requirement, is by checking the non-occurrence of an event. I'm afraid, AFAIK, non-occurrence check is not supported in WSO2 CEP-3.1.0.
However it is supported in WSO2 CEP-4.0.0 (yet to be released as I'm writing this on 24th Aug 2015). You may refer to non-occurrence detection sample [1].

Explanation:

Here we depart the first event if no unique event has occured 4 seconds (which is the timout) later since the latest unique event has occured. So it appears like we need to check the non-occurance of an event.

In CEP 4.0.0, you could achieve your requirement as follows:

from EventCycle#window.firstUnique(name)[ type=='type' ] 
select name, type 
insert into NameEvents;     -- Note: I renamed NameEvent in the question to NameEvents

-- After seeing the latest unique event (Query-A), 4 seconds later (Query-B), we're checking if no unique event has occured in between (Query-C and Query-D). 
-- So, we're checking the non-occurance of an event here... See link [1] for a sample.

--Query-A
from EventCycle#window.unique(name)[ type=='type' ] 
select name, type 
insert into latestEvents;

-- Query-B
from latestEvents#window.time(4 seconds)    -- Here, I've taken 4 seconds as the timeout.
select *
insert expired events into timedoutEvents;

-- Query-C
from every latestEvent = latestEvents[ type=='type' ] ->
keepAliveEvent = latestEvents[ latestEvent.name == keepAliveEvent.name and type=='type' ] 
    or timedoutEvent = timedoutEvents[ latestEvent.name == timedoutEvent.name and type=='type' ]
select latestEvent.name as name, keepAliveEvent.name as keepAliveName
insert into filteredEvents;

-- Query-D
from filteredEvents [ isNull(keepAliveName)]  
select name
insert into departedLatestEvents;


-- Since we want the name from the NameEvents stream, we're joining it with the departedLatestEvents stream

from departedLatestEvents#window.length(1) as departedLatestEvent join 
NameEvents#window.length(1) as NameEvent 
on departedLatestEvent.name == NameEvent.name       -- not checking type as both departedLatestEvents and NameEvents have events only with type 'type'  
select NameEvent.name as name, 'type' as type
insert into departedFirstEvents;

Link referred in the code sample:

1 https://docs.wso2.com/display/CEP400/Sample+0111+-+Detecting+non-occurrences+with+Patterns

Hope this helps!

Upvotes: 0

Related Questions