forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
107 lines (95 loc) · 2.49 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
package moira
import "time"
// 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
}
// 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 {
if _, ok := leftValues[value]; ok {
delete(leftValues, value)
}
}
}
result := make([]string, 0)
for _, value := range stringLists[0] {
if _, ok := leftValues[value]; ok {
result = append(result, value)
}
}
return result
}
// 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] {
leftValues[value.ID] = true
}
for _, triggerList := range triggerLists[1:] {
for _, trigger := range triggerList {
if _, ok := leftValues[trigger.ID]; ok {
delete(leftValues, trigger.ID)
}
}
}
result := make([]*Trigger, 0)
for _, value := range triggerLists[0] {
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
}