-
Notifications
You must be signed in to change notification settings - Fork 154
/
state.go
107 lines (91 loc) · 2.68 KB
/
state.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
package controller
import (
"encoding/json"
"fmt"
"sort"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/notifications-engine/pkg/services"
"github.com/argoproj/notifications-engine/pkg/subscriptions"
"github.com/argoproj/notifications-engine/pkg/triggers"
)
const (
notifiedHistoryMaxSize = 100
NotifiedAnnotationKey = "notified." + subscriptions.AnnotationPrefix
)
func StateItemKey(trigger string, conditionResult triggers.ConditionResult, dest services.Destination) string {
key := fmt.Sprintf("%s:%s:%s:%s", trigger, conditionResult.Key, dest.Service, dest.Recipient)
if conditionResult.OncePer != "" {
key = conditionResult.OncePer + ":" + key
}
return key
}
// NotificationsState track notification triggers state (already notified/not notified)
type NotificationsState map[string]int64
// truncate ensures that state has no more than specified number of items and
// removes unnecessary items starting from oldest
func (s NotificationsState) truncate(maxSize int) {
if cnt := len(s) - maxSize; cnt > 0 {
var keys []string
for k := range s {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return s[keys[i]] < s[keys[j]]
})
for i := 0; i < cnt; i++ {
delete(s, keys[i])
}
}
}
// SetAlreadyNotified set the state of given trigger/destination and return if state has been changed
func (s NotificationsState) SetAlreadyNotified(trigger string, result triggers.ConditionResult, dest services.Destination, isNotified bool) bool {
key := StateItemKey(trigger, result, dest)
if _, alreadyNotified := s[key]; alreadyNotified == isNotified {
return false
}
if isNotified {
s[key] = time.Now().Unix()
} else {
if result.OncePer != "" {
return false
}
delete(s, key)
}
return true
}
func (s NotificationsState) Persist(res metav1.Object) (map[string]string, error) {
s.truncate(notifiedHistoryMaxSize)
annotations := map[string]string{}
if res.GetAnnotations() != nil {
for k, v := range res.GetAnnotations() {
annotations[k] = v
}
}
if len(s) == 0 {
delete(annotations, NotifiedAnnotationKey)
} else {
stateJson, err := json.Marshal(s)
if err != nil {
return nil, err
}
annotations[NotifiedAnnotationKey] = string(stateJson)
}
return annotations, nil
}
func NewState(val string) NotificationsState {
if val == "" {
return NotificationsState{}
}
res := NotificationsState{}
if err := json.Unmarshal([]byte(val), &res); err != nil {
return NotificationsState{}
}
return res
}
func NewStateFromRes(res metav1.Object) NotificationsState {
if annotations := res.GetAnnotations(); annotations != nil {
return NewState(annotations[NotifiedAnnotationKey])
}
return NotificationsState{}
}