-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
utils.go
64 lines (53 loc) · 1.11 KB
/
utils.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
package query
import (
"github.com/grafana/loki/pkg/loghttp"
)
// return commonLabels labels between given labels set
func commonLabels(streams loghttp.Streams) loghttp.LabelSet {
if len(streams) == 0 {
return nil
}
result := streams[0].Labels
for i := 1; i < len(streams); i++ {
result = intersect(result, streams[i].Labels)
}
return result
}
// intersect two labels set
func intersect(a, b loghttp.LabelSet) loghttp.LabelSet {
set := loghttp.LabelSet{}
for ka, va := range a {
if vb, ok := b[ka]; ok {
if vb == va {
set[ka] = va
}
}
}
return set
}
// subtract labels set b from labels set a
func subtract(a, b loghttp.LabelSet) loghttp.LabelSet {
set := loghttp.LabelSet{}
for ka, va := range a {
if vb, ok := b[ka]; ok {
if vb == va {
continue
}
}
set[ka] = va
}
return set
}
func matchLabels(on bool, l loghttp.LabelSet, names []string) loghttp.LabelSet {
ret := loghttp.LabelSet{}
nameSet := map[string]struct{}{}
for _, n := range names {
nameSet[n] = struct{}{}
}
for k, v := range l {
if _, ok := nameSet[k]; on == ok {
ret[k] = v
}
}
return ret
}