David Gomez
David Gomez

Reputation: 33

Query with Siddhi CEP using two times windows

I 'm trying to do a simple query but a two time windows ; the query would be something like trying to send a message to users who have visited a product of the web more than twice in the last four months, provided that you have already sent the last month advertising for this product.

define stream webvisit (idClient string, idProduct string, chanel string)

from webvisit select idCliente, idProducto, canal,sum(1) as visits group by idCliente insert into visits

from visits[idProduct=='Fondos' and visits > 2]#window.time(4) insert into alert

and will continue ?

Upvotes: 1

Views: 249

Answers (1)

Rajeev Sampath
Rajeev Sampath

Reputation: 2757

You can do something like following:

define stream webvisit (idClient string, idProduct string, chanel string)

from visits[productId =='Fondos’]#window.time(4 days)
select idClient, idProduct, chanel, count(idClient) as visitCount
group by idClient
insert into visits;


from visits[visitCount > 2]
select *
insert into resultStream; 

In the second query we get the visit counts for each client during last 4 days and in the last query we filter those results with count > 2.

EDIT:

Since you need to send a notification only if it's not been sent within the last day (assuming it's defined as: current time - 24 hours ), you can try following:

 define stream webvisit (idClient string, idProduct string, chanel string);

 from webvisit[idProduct == 'Fondos']#window.time(4 days) 
 select idClient, idProduct, chanel, count(idClient) as visitCount 
 group by idClient insert into visits for current-events; 

 from visits[visitCount > 2]#window.time(1 day) 
 select idClient, idProduct, chanel, count(idClient) as hitsForClientPerDay 
 insert into tempStream;

 from tempStream[hitsForClientPerDay < 2] 
 select idClient, idProduct, chanel, 'your custom message here' as advertisement 
 insert into advertisementStream;

The second (1 day window) query keeps track of how many alerts ('hitsForClientPerDay') have been generated in the last 24 hours, the last query sends out the advertisement only if there hasn't been any during that period (note that hitsForClientPerDay will be 1 when the event comes since the current event is also considered for count(), so we check it as < 2 ).

Upvotes: 1

Related Questions