-
Notifications
You must be signed in to change notification settings - Fork 18
/
search.go
39 lines (36 loc) · 1001 Bytes
/
search.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
package dptest
import (
"fmt"
"github.com/signalfx/golib/datapoint"
)
// ExactlyOne returns the datapoint with metricName and panics if there is not exactly one in dps
func ExactlyOne(dps []*datapoint.Datapoint, metricName string) *datapoint.Datapoint {
return ExactlyOneDims(dps, metricName, nil)
}
// ExactlyOneDims returns the datapoint with metricName and dims, and panics if there is not exactly one
func ExactlyOneDims(dps []*datapoint.Datapoint, metricName string, dims map[string]string) *datapoint.Datapoint {
var found *datapoint.Datapoint
for _, dp := range dps {
if dp.Metric == metricName {
dimsMatch := true
for k, v := range dims {
v2, exists := dp.Dimensions[k]
if !exists || v2 != v {
dimsMatch = false
break
}
}
if !dimsMatch {
continue
}
if found != nil {
panic(fmt.Sprintf("Found metric name twice %s vs %s", dp, found))
}
found = dp
}
}
if found == nil {
panic("could not find metric name")
}
return found
}