forked from grafana/grafana-plugin-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
553 lines (502 loc) · 13.9 KB
/
frame.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Package data provides data structures that Grafana recognizes. The Frame
// object represents a Grafana Dataframe which can represent data such as tables
// and time series.
package data
import (
"fmt"
"math"
"sort"
"strings"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// Frame represents a columnar storage with optional labels.
type Frame struct {
// Name is used in some Grafana visualizations.
Name string
// Fields are the columns of a frame.
// All Fields must be of the same the length when marshalling the Frame for transmission.
Fields []*Field
// RefID is a property that can be set to match a Frame to its orginating query.
RefID string
Meta *QueryResultMeta
Warnings []Warning
}
// Frames is a slice of Frame pointers.
// It is the main data container within a backend.DataResponse.
type Frames []*Frame
// Field represents a column of data with a specific type.
type Field struct {
Name string
Config *FieldConfig
vector vector // TODO? in the frontend, the variable is called "Values"
Labels Labels
}
// Fields is a slice of Field pointers.
type Fields []*Field
// AppendRow adds a new row to the Frame by appending to each element of vals to
// the corresponding Field in the data.
// The Frame's Fields must be initalized or AppendRow will panic.
// The number of arguments must match the number of Fields in the Frame and each type must coorespond
// to the Field type or AppendRow will panic.
func (f *Frame) AppendRow(vals ...interface{}) {
for i, v := range vals {
f.Fields[i].vector.Append(v)
}
}
// RowCopy returns an interface slice that contains the values of each Field for the given rowIdx.
func (f *Frame) RowCopy(rowIdx int) []interface{} {
vals := make([]interface{}, len(f.Fields))
for i := range f.Fields {
vals[i] = f.CopyAt(i, rowIdx)
}
return vals
}
// AppendWarning adds warnings to the data frame.
func (f *Frame) AppendWarning(message string, details string) {
f.Warnings = append(f.Warnings, Warning{Message: message, Details: details})
}
// AppendRowSafe adds a new row to the Frame by appending to each each element of vals to
// the corresponding Field in the data. It has the some constraints as AppendRow but will
// return an error under those conditions instead of panicing.
func (f *Frame) AppendRowSafe(vals ...interface{}) error {
if len(vals) != len(f.Fields) {
return fmt.Errorf("failed to append vals to Frame. Frame has %v fields but was given %v to append", len(f.Fields), len(vals))
}
// check validity before any modification
for i, v := range vals {
if f.Fields[i] == nil || f.Fields[i].vector == nil {
return fmt.Errorf("can not append to uninitalized Field at field index %v", i)
}
dfPType := f.Fields[i].Type()
if v == nil {
if !dfPType.Nullable() {
return fmt.Errorf("can not append nil to non-nullable vector with underlying type %s at field index %v", dfPType, i)
}
}
if v != nil && fieldTypeFromVal(v) != dfPType {
return fmt.Errorf("invalid type appending row at index %v, got %T want %v", i, v, dfPType.ItemTypeString())
}
f.Fields[i].vector.Append(v)
}
return nil
}
// FilterRowsByField returns a copy of frame f (as per EmptyCopy()) that includes rows
// where the filter returns true and no error. If filter returns an error, then an error is returned.
func (f *Frame) FilterRowsByField(fieldIdx int, filter func(i interface{}) (bool, error)) (*Frame, error) {
filteredFrame := f.EmptyCopy()
rowLen, err := f.RowLen()
if err != nil {
return nil, err
}
for inRowIdx := 0; inRowIdx < rowLen; inRowIdx++ {
match, err := filter(f.At(fieldIdx, inRowIdx))
if err != nil {
return nil, err
}
if !match {
continue
}
filteredFrame.AppendRow(f.RowCopy(inRowIdx)...)
}
return filteredFrame, nil
}
// EmptyCopy returns a copy of Frame f but with Fields of zero length, and no copy of the FieldConfigs, Metadata, or Warnings.
func (f *Frame) EmptyCopy() *Frame {
newFrame := &Frame{
Name: f.Name,
RefID: f.RefID,
Fields: make(Fields, 0, len(f.Fields)),
}
for _, field := range f.Fields {
copy := NewFieldFromFieldType(field.Type(), 0)
copy.Name = field.Name
copy.Labels = field.Labels.Copy()
newFrame.Fields = append(newFrame.Fields, copy)
}
return newFrame
}
// TypeIndices returns a slice of Field index positions for the given pTypes.
func (f *Frame) TypeIndices(pTypes ...FieldType) []int {
indices := []int{}
if f.Fields == nil {
return indices
}
for fieldIdx, f := range f.Fields {
vecType := f.Type()
for _, pType := range pTypes {
if pType == vecType {
indices = append(indices, fieldIdx)
break
}
}
}
return indices
}
// NewField returns a new instance of Field.
func NewField(name string, labels Labels, values interface{}) *Field {
var vec vector
switch v := values.(type) {
case []int8:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*int8:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []int16:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*int16:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []int32:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*int32:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []int64:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*int64:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []uint8:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*uint8:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []uint16:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*uint16:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []uint32:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*uint32:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []uint64:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*uint64:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []float32:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*float32:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []float64:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*float64:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []string:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*string:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []bool:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*bool:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []time.Time:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
case []*time.Time:
vec = newVector(v, len(v))
for i := 0; i < len(v); i++ {
vec.Set(i, v[i])
}
default:
panic(fmt.Errorf("unsupported field type %T", v))
}
return &Field{
Name: name,
vector: vec,
Labels: labels,
}
}
// Set sets the Field's value at index idx to val.
// It will panic if idx is out of range.
func (f *Field) Set(idx int, val interface{}) {
f.vector.Set(idx, val)
}
// Append appends element i to the Field.
func (f *Field) Append(i interface{}) {
f.vector.Append(i)
}
// Extend extends the Field length by i.
func (f *Field) Extend(i int) {
f.vector.Extend(i)
}
// At returns the the element at index idx of the Field.
// It will panic if idx is out of range.
func (f *Field) At(idx int) interface{} {
return f.vector.At(idx)
}
// Len returns the number of elements in the Field.
func (f *Field) Len() int {
return f.vector.Len()
}
// Type returns the underlying primitive type of the Field.
func (f *Field) Type() FieldType {
return f.vector.Type()
}
// PointerAt returns a pointer to the value at idx of the Field.
// It will panic if idx is out of range.
func (f *Field) PointerAt(idx int) interface{} {
return f.vector.PointerAt(idx)
}
// CopyAt returns a copy of the value of the specified index idx.
// It will panic if idx is out of range.
func (f *Field) CopyAt(idx int) interface{} {
return f.vector.CopyAt(idx)
}
// ConcreteAt returns the concrete value at the specified index idx.
// A non-pointer type is returned regardless if the underlying vector is a pointer
// type or not. If the value is a pointer type, and is nil, then the zero value
// is returned and ok will be false.
func (f *Field) ConcreteAt(idx int) (val interface{}, ok bool) {
return f.vector.ConcreteAt(idx)
}
// Nullable returns if the the Field's elements are nullable.
func (f *Field) Nullable() bool {
return f.Type().Nullable()
}
// SetConfig modifies the Field's Config property to
// be set to conf and returns the Field.
func (f *Field) SetConfig(conf *FieldConfig) *Field {
f.Config = conf
return f
}
// Labels are used to add metadata to an object.
type Labels map[string]string
// Equals returns true if the argument has the same k=v pairs as the receiver.
func (l Labels) Equals(arg Labels) bool {
if len(l) != len(arg) {
return false
}
for k, v := range l {
if argVal, ok := arg[k]; !ok || argVal != v {
return false
}
}
return true
}
// Copy returns a copy of the labels.
func (l Labels) Copy() Labels {
c := make(Labels, len(l))
for k, v := range l {
c[k] = v
}
return c
}
// Contains returns true if all k=v pairs of the argument are in the receiver.
func (l Labels) Contains(arg Labels) bool {
if len(arg) > len(l) {
return false
}
for k, v := range arg {
if argVal, ok := l[k]; !ok || argVal != v {
return false
}
}
return true
}
func (l Labels) String() string {
// Better structure, should be sorted, copy prom probably
keys := make([]string, len(l))
i := 0
for k := range l {
keys[i] = k
i++
}
sort.Strings(keys)
var sb strings.Builder
i = 0
for _, k := range keys {
sb.WriteString(k)
sb.WriteString("=")
sb.WriteString(l[k])
if i != len(keys)-1 {
sb.WriteString(", ")
}
i++
}
return sb.String()
}
// LabelsFromString parses the output of Labels.String() into
// a Labels object. It probably has some flaws.
func LabelsFromString(s string) (Labels, error) {
if s == "" {
return nil, nil
}
labels := make(map[string]string)
for _, rawKV := range strings.Split(s, ", ") {
kV := strings.SplitN(rawKV, "=", 2)
if len(kV) != 2 {
return nil, fmt.Errorf(`invalid label key=value pair "%v"`, rawKV)
}
labels[kV[0]] = kV[1]
}
return labels, nil
}
// NewFrame returns a new instance of a Frame.
func NewFrame(name string, fields ...*Field) *Frame {
return &Frame{
Name: name,
Fields: fields,
}
}
// Rows returns the number of rows in the frame.
func (f *Frame) Rows() int {
if len(f.Fields) > 0 {
return f.Fields[0].Len()
}
return 0
}
// At returns the value of the specified fieldIdx and rowIdx.
// It will panic if either the fieldIdx or rowIdx are out of range.
func (f *Frame) At(fieldIdx int, rowIdx int) interface{} {
return f.Fields[fieldIdx].vector.At(rowIdx)
}
// CopyAt returns a copy of the value of the specified fieldIdx and rowIdx.
// It will panic if either the fieldIdx or rowIdx are out of range.
func (f *Frame) CopyAt(fieldIdx int, rowIdx int) interface{} {
return f.Fields[fieldIdx].vector.CopyAt(rowIdx)
}
// Set set the val to the specified fieldIdx and rowIdx.
// It will panic if either the fieldIdx or rowIdx are out of range.
func (f *Frame) Set(fieldIdx int, rowIdx int, val interface{}) {
f.Fields[fieldIdx].vector.Set(rowIdx, val)
}
// Extend extends all the Fields by length by i.
func (f *Frame) Extend(i int) {
for _, f := range f.Fields {
f.vector.Extend(i)
}
}
// ConcreteAt returns the concrete value at the specified fieldIdx and rowIdx.
// A non-pointer type is returned regardless if the underlying type is a pointer
// type or not. If the value is a pointer type, and is nil, then the zero value
// is returned and ok will be false.
func (f *Frame) ConcreteAt(fieldIdx int, rowIdx int) (val interface{}, ok bool) {
return f.Fields[fieldIdx].vector.ConcreteAt(rowIdx)
}
// RowLen returns the the length of the Frame Fields.
// If the Length of all the Fields is not the same then error is returned.
// If the Frame's Fields are nil an error is returned.
func (f *Frame) RowLen() (int, error) {
if f.Fields == nil || len(f.Fields) == 0 {
return 0, fmt.Errorf("frame's fields are nil or of zero length")
}
var l int
for i := 0; i < len(f.Fields); i++ {
if f.Fields[i].vector == nil {
return 0, fmt.Errorf("frame's field at index %v is nil", i)
}
if i == 0 {
l = f.Fields[i].Len()
continue
}
if l != f.Fields[i].Len() {
return 0, fmt.Errorf("frame has different field lengths, field 0 is len %v but field %v is len %v", l, i, f.Fields[i].vector.Len())
}
}
return l, nil
}
// FrameTestCompareOptions returns go-cmp testing options to allow testing of Frame equivelnce.
// The intent is to only use this for testing.
func FrameTestCompareOptions() []cmp.Option {
confFloats := cmp.Comparer(func(x, y *ConfFloat64) bool {
if x == nil && y == nil {
return true
}
if y == nil {
if math.IsNaN(float64(*x)) {
return true
}
if math.IsInf(float64(*x), 1) {
return true
}
if math.IsInf(float64(*x), -1) {
return true
}
}
if x == nil {
if math.IsNaN(float64(*y)) {
return true
}
if math.IsInf(float64(*y), 1) {
return true
}
if math.IsInf(float64(*y), -1) {
return true
}
}
return *x == *y
})
unexportedField := cmp.AllowUnexported(Field{})
return []cmp.Option{confFloats, unexportedField, cmpopts.EquateEmpty()}
}