-
Notifications
You must be signed in to change notification settings - Fork 0
/
trend.go
74 lines (60 loc) · 1.38 KB
/
trend.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
package trend_prediction
import "sync"
type DataSource struct {
Source chan interface{}
KeyName string
}
type AlertFunc func(...interface{})
type AlertSubscribe struct {
silence bool
inhibition bool
AlertFunc AlertFunc
}
func (s *AlertSubscribe) Silence() {
s.silence = true
}
func (s *AlertSubscribe) Recover() {
s.silence = false
}
func (s *AlertSubscribe) Inhibition() {
s.inhibition = true
}
// ---------------------------------------------------------------------------------------------------------------------
type TrendPrediction struct {
dataSource *DataSource
running bool
option *option
subscribers []*AlertSubscribe
subLocker *sync.RWMutex
lock *sync.Mutex
watchingData *dataInspect
}
func (t *TrendPrediction) getOption(opts ...Option) {
if t.option == nil {
t.option = getDefaultOption()
}
for _, f := range opts {
f(t.option)
}
}
func NewTrendPrediction(source *DataSource, opts ...Option) *TrendPrediction {
tp := TrendPrediction{
dataSource: source,
subLocker: &sync.RWMutex{},
lock: &sync.Mutex{},
}
tp.getOption(opts...)
return &tp
}
func (t *TrendPrediction) Watching() {
t.lock.Lock()
defer t.lock.Unlock()
if !t.running {
go t.startWatch()
}
}
func (t *TrendPrediction) RegisterSubscribe(sub *AlertSubscribe) {
t.subLocker.Lock()
defer t.subLocker.Unlock()
t.subscribers = append(t.subscribers, sub)
}