-
Notifications
You must be signed in to change notification settings - Fork 57
/
series.go
103 lines (87 loc) · 2.71 KB
/
series.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
package timeseries
import (
"fmt"
"sort"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/sdata"
)
type CollectionReader interface {
// GetMetricRefs runs validate without validateData. If the data is valid, then
// []TimeSeriesMetricRef is returned from reading as well as any ignored data. If invalid,
// then an error is returned, and no refs or ignoredFieldIndices are returned.
GetMetricRefs(validateData bool) (refs []MetricRef, ignoredFieldIndices []sdata.FrameFieldIndex, err error)
Frames() []*data.Frame // returns underlying frames
}
// MetricRef is for reading and contains the data for an individual
// time series. In the cases of the Multi and Wide formats, the Fields are pointers
// to the data in the original frame. In the case of Long new fields are constructed.
type MetricRef struct {
TimeField *data.Field
ValueField *data.Field
// TODO: RefID string
// TODO: Pointer to frame meta?
}
func CollectionReaderFromFrames(frames []*data.Frame) (CollectionReader, error) {
if len(frames) == 0 {
return nil, fmt.Errorf("must be at least one frame")
}
firstFrame := frames[0]
if firstFrame == nil {
return nil, fmt.Errorf("nil frames are invalid")
}
if firstFrame.Meta == nil {
return nil, fmt.Errorf("metadata missing from first frame, can not determine type")
}
mt := firstFrame.Meta.Type
var tcr CollectionReader
switch {
case mt == data.FrameTypeTimeSeriesMulti:
mfs := MultiFrame(frames)
tcr = &mfs
case mt == data.FrameTypeTimeSeriesLong:
ls := LongFrame(frames)
tcr = &ls // TODO change to Frames for extra/ignored data?
case mt == data.FrameTypeTimeSeriesWide:
wfs := WideFrame(frames)
tcr = &wfs
default:
return nil, fmt.Errorf("unsupported time series type %q", mt)
}
return tcr, nil
}
func (m MetricRef) GetMetricName() string {
if m.ValueField != nil {
return m.ValueField.Name
}
return ""
}
// TODO GetFQMetric (or something, Names + Labels)
func (m MetricRef) GetLabels() data.Labels {
if m.ValueField != nil {
return m.ValueField.Labels
}
return nil
}
func sortTimeSeriesMetricRef(refs []MetricRef) {
sort.SliceStable(refs, func(i, j int) bool {
iRef := refs[i]
jRef := refs[j]
if iRef.GetMetricName() < jRef.GetMetricName() {
return true
}
if iRef.GetMetricName() > jRef.GetMetricName() {
return false
}
// If here Names are equal, next sort based on if there are labels.
if iRef.GetLabels() == nil && jRef.GetLabels() == nil {
return true // no labels first
}
if iRef.GetLabels() == nil && jRef.GetLabels() != nil {
return true
}
if iRef.GetLabels() != nil && jRef.GetLabels() == nil {
return false
}
return iRef.GetLabels().String() < jRef.GetLabels().String()
})
}