Event processing over time
Hi Jesus.
First, thank you for durable. I've been learning and using durable for a few weeks now. Still trying to grasp some of the concepts, so my question may be a basic one.
I'm mainly interested in creating a solution for event processing. More specifically, where I'm processing events over periods of time, and making decisions based on the events that occur, how often they occur, and if they occur consecutively.
So for example, I'd like to track any number of different events. For each event, if I have the same event (or an event that matches some condition) occur on consecutive days, then I'd like to take some action. For each consecutive day where that event occurs, another action could be taken. But when a day passes with the event not occurring, everything resets and we start over again.
Example: If I applied this concept to an exercise incentive application. If a user exercises today, that's one event. Then tomorrow, that person again exercises. That means we have 2 exercise events on 2 consecutive days. When that occurs, I'd like the user to be awarded some number of points. Then the user exercises a 3rd consecutive day, at which time, the system would then award even more points (some multiplier) than before. With each consecutive day of exercise, the user would get a higher multiplier of points. But when a day of exercise is missed, the multiplier resets, and we start all over checking for consecutive days of exercise.
I think there is a solution here using correlated sequences, but I can't piece it together. And to be honest, the syntax for defining the first, second, etc. events is still a bit confusing to me. I also think there may be a solution here if the facts that persist can be manually retracted, but I don't see how that can be done unless I interact directly with Redis.
Any pointers or suggestions would be greatly appreciated.
Thank you! Dave
Thanks for posting the question. Yes you can write rules to reason about events that happen over a period of time. Below is a simple example, which detects a pattern of three messages within a span of 5 seconds. The documentation has a few more details about this:
https://github.com/jruizgit/rules/blob/master/docs/py/reference.md#timers
from durable.lang import *
with statechart('risk3'):
with state('start'):
@to('meter')
def start(c):
c.start_timer('RiskTimer', 5)
with state('meter'):
@to('fraud')
@when_all(count(3), c.message << m.amount > 100)
def fraud(c):
for e in c.m:
print(e.message)
@to('exit')
@when_all(timeout('RiskTimer'))
def exit(c):
print('exit')
state('fraud')
state('exit')
@when_start
def on_start(host):
# three events in a row will trigger the fraud rule
host.post('risk3', { 'amount': 200 })
host.post('risk3', { 'amount': 300 })
host.post('risk3', { 'amount': 400 })
# two events will exit after 5 seconds
host.post('risk3', { 'sid': 1, 'amount': 500 })
host.post('risk3', { 'sid': 1, 'amount': 600 })
Hope this helps.
Is it possible to define expiration time with JSON? Because I see only one $t in the JSON document
Thank you very much.