This repository has been archived by the owner on May 21, 2020. It is now read-only.
forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
193 lines (171 loc) · 4.44 KB
/
helpers.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package moira
import (
"bytes"
"math"
"time"
)
// BytesScanner allows to scan for subslices separated by separator
type BytesScanner struct {
source []byte
index int
separator byte
emitEmptySlice bool
}
//HasNext checks if next subslice available or not
func (it *BytesScanner) HasNext() bool {
return it.index < len(it.source) || it.emitEmptySlice
}
//Next returns available subslice and advances the scanner to next slice
func (it *BytesScanner) Next() (result []byte) {
if it.emitEmptySlice {
it.emitEmptySlice = false
result = make([]byte, 0)
return result
}
scannerIndex := it.index
separatorIndex := bytes.IndexByte(it.source[scannerIndex:], it.separator)
if separatorIndex < 0 {
result = it.source[scannerIndex:]
it.index = len(it.source)
} else {
separatorIndex += scannerIndex
result = it.source[scannerIndex:separatorIndex]
if separatorIndex == len(it.source)-1 {
it.emitEmptySlice = true
}
it.index = separatorIndex + 1
}
return result
}
//NewBytesScanner slices bytes into all subslices separated by separator and returns a scanner which allows to scan for these subslices
func NewBytesScanner(bytes []byte, separator byte) *BytesScanner {
return &BytesScanner{
source: bytes,
index: 0,
separator: separator,
emitEmptySlice: false,
}
}
// Int64ToTime returns time.Time from int64
func Int64ToTime(timeStamp int64) time.Time {
return time.Unix(timeStamp, 0).UTC()
}
// UseString gets pointer value of string or default string if pointer is nil
func UseString(str *string) string {
if str == nil {
return ""
}
return *str
}
// UseFloat64 gets pointer value of float64 or default float64 if pointer is nil
func UseFloat64(f *float64) float64 {
if f == nil {
return 0
}
return *f
}
// IsValidFloat64 checks float64 for Inf and NaN. If it is then float64 is not valid
func IsValidFloat64(val float64) bool {
if math.IsNaN(val) {
return false
}
if math.IsInf(val, 0) {
return false
}
return true
}
// Subset return whether first is a subset of second
func Subset(first, second []string) bool {
set := make(map[string]bool)
for _, value := range second {
set[value] = true
}
for _, value := range first {
if !set[value] {
return false
}
}
return true
}
// GetStringListsDiff returns the members of the set resulting from the difference between the first set and all the successive lists.
func GetStringListsDiff(stringLists ...[]string) []string {
if len(stringLists) == 0 {
return []string{}
}
leftValues := make(map[string]bool)
for _, value := range stringLists[0] {
leftValues[value] = true
}
for _, stringList := range stringLists[1:] {
for _, value := range stringList {
delete(leftValues, value)
}
}
result := make([]string, 0)
for _, value := range stringLists[0] {
if _, ok := leftValues[value]; ok {
result = append(result, value)
}
}
return result
}
// GetStringListsUnion returns the union set of stringLists
func GetStringListsUnion(stringLists ...[]string) []string {
if len(stringLists) == 0 {
return []string{}
}
values := make([]string, 0)
uniqueValues := make(map[string]bool)
for _, stringList := range stringLists {
for _, value := range stringList {
if _, ok := uniqueValues[value]; !ok {
values = append(values, value)
uniqueValues[value] = true
}
}
}
return values
}
// GetTriggerListsDiff returns the members of the set resulting from the difference between the first set and all the successive lists.
func GetTriggerListsDiff(triggerLists ...[]*Trigger) []*Trigger {
if len(triggerLists) == 0 {
return []*Trigger{}
}
leftValues := make(map[string]bool)
for _, value := range triggerLists[0] {
if value != nil {
leftValues[value.ID] = true
}
}
for _, triggerList := range triggerLists[1:] {
for _, trigger := range triggerList {
if trigger != nil {
delete(leftValues, trigger.ID)
}
}
}
result := make([]*Trigger, 0)
for _, value := range triggerLists[0] {
if value == nil {
continue
}
if _, ok := leftValues[value.ID]; ok {
result = append(result, value)
}
}
return result
}
// ChunkSlice gets slice of strings and chunks it to a given size. It returns a batch of chunked lists
func ChunkSlice(original []string, chunkSize int) (divided [][]string) {
if chunkSize < 1 {
return
}
for i := 0; i < len(original); i += chunkSize {
end := i + chunkSize
if end > len(original) {
end = len(original)
}
divided = append(divided, original[i:end])
}
return
}