Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulative amount of events will trigger fraud detection #396

Open
jaballe opened this issue Feb 26, 2024 · 1 comment
Open

Cumulative amount of events will trigger fraud detection #396

jaballe opened this issue Feb 26, 2024 · 1 comment

Comments

@jaballe
Copy link

jaballe commented Feb 26, 2024

Hello,

How do I create a rule that can detect that sums up the amount of x number of events and if it hits a certain number threshold, it will be detected for example:
like so
with ruleset('accumulated_fraud_detection'):
@when_all(accumulate(amount_sum=lambda ev: ev.amount) > 1000)
def fraud_detected(c):
print('Fraud detected! Total amount: {0}'.format(c.events[-1].amount))

post('evnt_amount', { 'amount': 100 })
 post('evnt_amount', { 'amount': 200 })

post('evnt_amount', { 'amount': 500 })
post('evnt_amount', { 'amount': 100 })
post('evnt_amount', { 'amount': 101})

@mathrep
Copy link

mathrep commented Aug 7, 2024

Do you mean something like the following?
`
from durable.lang import *

with ruleset('accumulated_fraud_detection'):

@when_all(+m.amount)
def accumulate_amount(c):
    if c.s.total_amount is None:
        c.s.total_amount = 0
    
    c.s.total_amount += c.m.amount
    print(f"Current total amount: {c.s.total_amount}")
    
    if c.s.total_amount > 1000:
        print(f"Fraud detected! Total amount: {c.s.total_amount}")
        # Reset the total amount or some other actions
        c.s.total_amount = 0 

post('accumulated_fraud_detection', { 'amount': 100 })
post('accumulated_fraud_detection', { 'amount': 200 })
post('accumulated_fraud_detection', { 'amount': 500 })
post('accumulated_fraud_detection', { 'amount': 100 })
post('accumulated_fraud_detection', { 'amount': 101 })
`
This code will print out

Current total amount: 100
Current total amount: 300
Current total amount: 800
Current total amount: 900
Current total amount: 1001
Fraud detected! Total amount: 1001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants