-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleengine.go
158 lines (140 loc) · 3.76 KB
/
ruleengine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gorules
import (
"bytes"
"encoding/gob"
"fmt"
"log"
"strconv"
"time"
dbqueue "github.com/a8a-io/go-dbqueue"
"github.com/a8a-io/gorules/utils"
)
const RuleEvent = "RuleEvents"
type RuleEngine interface {
Start()
// AwardCoins(userId string, eventName string, fieldValues map[string]string, timestamp time.Time)
setRules(rules map[string]RulesList)
getRules(eventName string) (RulesList, bool)
}
type _RuleEngine struct {
rulesFolderPath string
Rules map[string]RulesList
EventQ *dbqueue.DBQueue
RewardQ *dbqueue.DBQueue
}
func NewRuleEngine(rulesFolderPath string, EventQ *dbqueue.DBQueue, RewardQ *dbqueue.DBQueue) RuleEngine {
return _RuleEngine{rulesFolderPath: rulesFolderPath, EventQ: EventQ, RewardQ: RewardQ}
}
func (this _RuleEngine) setRules(rulesMap map[string]RulesList) {
this.Rules = rulesMap
}
func (this _RuleEngine) Start() {
ticker := time.NewTicker(5 * time.Minute)
quit := make(chan struct{})
go refreshRules(this.rulesFolderPath, this, ticker, quit)
go calculateCoins(this)
}
func (this _RuleEngine) getRules(eventName string) (RulesList, bool) {
rl, ok := this.Rules[eventName]
return rl, ok
}
func refreshRules(rulesFolderPath string, ruleEngine RuleEngine, ticker *time.Ticker, quit <-chan struct{}) {
for {
select {
case <-ticker.C:
rulesMap := LoadRulesFromFolder(rulesFolderPath)
ruleEngine.setRules(rulesMap)
case <-quit:
ticker.Stop()
return
}
}
}
func calculateCoins(re _RuleEngine) int {
for {
event, err := DequeEvent(re.EventQ)
if err != nil {
fmt.Println(err)
time.Sleep(1 * time.Second)
continue
}
rulesList, ok := re.getRules(event.EventName)
if !ok {
log.Printf("No rule found for event %s", event.EventName)
}
var reward Reward
for _, rule := range rulesList.Rules {
if validateEvent(rule, event) {
reward = Reward{event, rule.Reward, rule}
break
}
}
var rewardBytes bytes.Buffer
err = gob.NewEncoder(&rewardBytes).Encode(reward)
if err != nil {
fmt.Println(err)
continue
}
err = (*re.RewardQ).Enque("Rewards", nil, rewardBytes.Bytes())
if err != nil {
fmt.Println(err)
}
}
}
func DequeEvent(EventQueue *dbqueue.DBQueue) (Event, error) {
var event Event
msg, err := (*EventQueue).Deque(RuleEvent)
if err != nil {
return Event{}, err
}
z := bytes.NewBuffer(msg.Body)
err = gob.NewDecoder(z).Decode(&event)
if err != nil {
return Event{}, err
}
return event, nil
}
func validateEvent(rule Rule, event Event) bool {
if !utils.InBetween(event.Timestamp, rule.StartTime, rule.EndTime) {
return false
}
var allConditionMatch bool = true
for _, condition := range rule.Conditions {
field := condition.Field
val, ok := event.Meta[field]
if !ok {
return false
}
switch condition.Op {
case LessThan, LessThanEqual, GreaterThan, GreaterThanEqual:
vi, err := strconv.ParseInt(val, 10, 32)
cvi, err2 := strconv.ParseInt(condition.Value, 10, 32)
if err != nil || err2 != nil {
allConditionMatch = false
} else {
switch condition.Op {
case LessThan:
allConditionMatch = allConditionMatch && (vi < cvi)
case LessThanEqual:
allConditionMatch = allConditionMatch && (vi <= cvi)
case GreaterThan:
allConditionMatch = allConditionMatch && (vi > cvi)
case GreaterThanEqual:
allConditionMatch = allConditionMatch && (vi >= cvi)
}
}
case Equal:
allConditionMatch = allConditionMatch && (val == condition.Value)
case NotEqual:
allConditionMatch = allConditionMatch && (val != condition.Value)
case In:
allConditionMatch = allConditionMatch && utils.Contains(condition.ValueArr, val)
case NotIn:
allConditionMatch = allConditionMatch && !utils.Contains(condition.ValueArr, val)
}
if !allConditionMatch {
break
}
}
return allConditionMatch
}