forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.go
164 lines (129 loc) · 3.41 KB
/
linkedlist.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
// Copyright 2017 Xiaomi, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package judge
import (
"container/list"
"sync"
"github.com/didi/nightingale/v5/vos"
)
type SafeLinkedList struct {
sync.RWMutex
L *list.List
}
func (ll *SafeLinkedList) Front() *list.Element {
ll.RLock()
defer ll.RUnlock()
return ll.L.Front()
}
func (ll *SafeLinkedList) Len() int {
ll.RLock()
defer ll.RUnlock()
return ll.L.Len()
}
func (ll *SafeLinkedList) PushFrontAndMaintain(v *vos.MetricPoint, maintainDuration int64) {
ll.Lock()
defer ll.Unlock()
sz := ll.L.Len()
lastPointTs := ll.L.Front().Value.(*vos.MetricPoint).Time
earliestTs := v.Time - maintainDuration
if sz > 0 {
// 新push上来的数据有可能重复了,或者timestamp不对,这种数据要丢掉
if v.Time <= lastPointTs {
return
}
}
ll.L.PushFront(v)
sz++
for i := 0; i < sz; i++ {
if ll.L.Back().Value.(*vos.MetricPoint).Time >= earliestTs {
break
}
//最前面的点已经不在告警策略时间周期内,丢弃掉
ll.L.Remove(ll.L.Back())
}
}
func (ll *SafeLinkedList) HistoryPoints(smallestTime int64) []*vos.HPoint {
size := ll.Len()
if size == 0 {
return []*vos.HPoint{}
}
firstElement := ll.Front()
firstItem := firstElement.Value.(*vos.MetricPoint)
vs := make([]*vos.HPoint, 0)
if firstItem.Time < smallestTime {
return vs
}
v := &vos.HPoint{
Timestamp: firstItem.Time,
Value: vos.JsonFloat(firstItem.Value),
}
vs = append(vs, v)
currentElement := firstElement
for i := 1; i < size; i++ {
nextElement := currentElement.Next()
if nextElement == nil {
return vs
}
item := nextElement.Value.(*vos.MetricPoint)
if item.Time < smallestTime {
return vs
}
v := &vos.HPoint{
Timestamp: item.Time,
Value: vos.JsonFloat(item.Value),
}
vs = append(vs, v)
currentElement = nextElement
}
return vs
}
// func (ll *SafeLinkedList) QueryDataByTS(start, end int64) []*vos.HPoint {
// size := ll.Len()
// if size == 0 {
// return []*vos.HPoint{}
// }
// firstElement := ll.Front()
// firstItem := firstElement.Value.(*vos.MetricPoint)
// var vs []*vos.HPoint
// if firstItem.Time < start {
// //最新的点也比起始时间旧,直接返回
// return vs
// }
// v := &vos.HPoint{
// Timestamp: firstItem.Time,
// Value: vos.JsonFloat(firstItem.Value),
// }
// vs = append(vs, v)
// currentElement := firstElement
// for {
// nextElement := currentElement.Next()
// if nextElement == nil {
// return vs
// }
// if nextElement.Value.(*vos.MetricPoint).Time < start {
// return vs
// }
// if nextElement.Value.(*vos.MetricPoint).Time > end {
// currentElement = nextElement
// continue
// }
// v := &vos.HPoint{
// Timestamp: nextElement.Value.(*vos.MetricPoint).Time,
// Value: vos.JsonFloat(nextElement.Value.(*vos.MetricPoint).Value),
// }
// vs = append(vs, v)
// currentElement = nextElement
// }
// return vs
// }