-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.go
172 lines (149 loc) · 3.17 KB
/
store.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"sort"
"time"
"github.com/prometheus/alertmanager/template"
)
const (
statusFiring = "firing"
statusResolved = "resolved"
)
// webhookPayload represents the raw data received from Alertmanager.
type webhookPayload struct {
*template.Data
Version string `json:"version"`
GroupKey string `json:"groupKey"`
}
// notification represents a notification received from Alertmanager.
type notification struct {
*webhookPayload
Remote string `json:"remoteAddress"`
Timestamp time.Time `json:"timestamp"`
}
func (n *notification) Key() string {
return fmt.Sprintf("%s:%s", n.Receiver, n.GroupKey)
}
type incident struct {
first, last *notification
}
func (i *incident) Key() string {
return i.first.Key()
}
func (i *incident) Alerts() template.Alerts {
return i.last.Alerts
}
func (i *incident) Duration() time.Duration {
if i.first == nil || i.last == nil {
return time.Duration(0)
}
if i.IsResolved() {
return i.last.Timestamp.Sub(i.first.Timestamp)
}
return time.Now().Sub(i.first.Timestamp)
}
func (i *incident) IsResolved() bool {
return i.last.Status == statusResolved
}
func (i *incident) Update(n *notification) {
if i.first == nil {
i.first = n
}
if i.last == nil || n.Timestamp.After(i.last.Timestamp) {
i.last = n
}
}
// store manages Alertmanager notifications and incidents.
type store struct {
notifications []*notification
incidents map[string]*incident
actionc chan func()
quitc chan struct{}
}
func newStore() *store {
return &store{
notifications: make([]*notification, 0),
incidents: make(map[string]*incident),
actionc: make(chan func()),
quitc: make(chan struct{}),
}
}
func (s *store) stop() {
close(s.quitc)
}
func (s *store) run() {
for {
select {
case <-s.quitc:
return
case f := <-s.actionc:
f()
}
}
}
func (s *store) addNotification(n *notification) {
s.actionc <- func() {
s.notifications = append(s.notifications, n)
}
}
func (s *store) listNotifications() []*notification {
var notifications []*notification
done := make(chan struct{})
s.actionc <- func() {
defer close(done)
notifications = s.notifications
}
<-done
return notifications
}
func (s *store) getIncident(k string) *incident {
var i *incident
done := make(chan struct{})
s.actionc <- func() {
defer close(done)
i, _ = s.incidents[k]
}
<-done
return i
}
func (s *store) listIncidents() []*incident {
var incidents []*incident
done := make(chan struct{})
s.actionc <- func() {
defer close(done)
keys := make([]string, 0, len(s.incidents))
for k := range s.incidents {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
incidents = append(incidents, s.incidents[k])
}
}
<-done
return incidents
}
func (s *store) updateIncident(n *notification) *incident {
var i *incident
done := make(chan struct{})
s.actionc <- func() {
defer close(done)
i, _ = s.incidents[n.Key()]
if i == nil {
i = &incident{}
s.incidents[n.Key()] = i
}
i.Update(n)
}
<-done
return i
}
func (s *store) deleteIncident(i *incident) {
done := make(chan struct{})
s.actionc <- func() {
defer close(done)
delete(s.incidents, i.Key())
}
<-done
return
}