-
Notifications
You must be signed in to change notification settings - Fork 1
/
alerts.go
60 lines (50 loc) · 1.38 KB
/
alerts.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
package main
import (
"fmt"
"time"
)
type Alert interface {
Check(Event)
}
type TotalTrafficAlert struct {
hits int
duration time.Duration
notification Notification
alertTriggered bool
events []Event
}
func NewTotalTrafficAlert(hits int, duration time.Duration, notification Notification) *TotalTrafficAlert {
return &TotalTrafficAlert{
hits: hits,
duration: duration,
notification: notification,
}
}
func (t *TotalTrafficAlert) Check(event Event) {
t.add(event)
if t.isExceeded() && !t.alertTriggered {
t.notification.Send(fmt.Sprintf("High traffic generated an alert - hits = %d, triggered at %s", len(t.events), event.Time.String()))
t.alertTriggered = true
} else if !t.isExceeded() && t.alertTriggered {
reason := fmt.Sprintf("Traffic returned to normal, triggered at %s", event.Time.String())
t.notification.Send(reason)
t.alertTriggered = false
}
}
func (t *TotalTrafficAlert) add(event Event) {
t.events = append(t.events, event)
t.pruneUpTo(event.Time)
}
func (t *TotalTrafficAlert) isExceeded() bool {
return len(t.events) > 0 && len(t.events) >= t.hits
}
func (t *TotalTrafficAlert) pruneUpTo(currentTime time.Time) {
var lastGreatestIndex int
for i := len(t.events) - 1; i >= 0; i-- {
if currentTime.Sub(t.events[i].Time) > t.duration {
lastGreatestIndex = i + 1
break
}
}
t.events = t.events[lastGreatestIndex:]
}