forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
panel.go
1249 lines (1194 loc) · 38.3 KB
/
panel.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sdk
/*
Copyright 2016 Alexander I.Grafov <[email protected]>
Copyright 2016-2019 The Grafana SDK authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sort"
)
// Each panel may be one of these types.
const (
CustomType panelType = iota
DashlistType
GraphType
TableType
TextType
PluginlistType
AlertlistType
SinglestatType
StatType
RowType
BarGaugeType
HeatmapType
TimeseriesType
)
const MixedSource = "-- Mixed --"
type (
// Panel represents panels of different types defined in Grafana.
Panel struct {
CommonPanel
// Should be initialized only one type of panels.
// OfType field defines which of types below will be used.
*GraphPanel
*TablePanel
*TextPanel
*SinglestatPanel
*StatPanel
*DashlistPanel
*PluginlistPanel
*RowPanel
*AlertlistPanel
*BarGaugePanel
*HeatmapPanel
*TimeseriesPanel
*CustomPanel
}
panelType int8
CommonPanel struct {
Datasource interface{} `json:"datasource,omitempty"` // metrics
Editable bool `json:"editable"`
Error bool `json:"error"`
GridPos struct {
H *int `json:"h,omitempty"`
W *int `json:"w,omitempty"`
X *int `json:"x,omitempty"`
Y *int `json:"y,omitempty"`
} `json:"gridPos,omitempty"`
Height interface{} `json:"height,omitempty"` // general
HideTimeOverride *bool `json:"hideTimeOverride,omitempty"`
ID uint `json:"id"`
IsNew bool `json:"isNew"`
Links []Link `json:"links,omitempty"` // general
MinSpan *float32 `json:"minSpan,omitempty"` // templating options
OfType panelType `json:"-"` // it required for defining type of the panel
Renderer *string `json:"renderer,omitempty"` // display styles
Repeat *string `json:"repeat,omitempty"` // templating options
// RepeatIteration *int64 `json:"repeatIteration,omitempty"`
RepeatPanelID *uint `json:"repeatPanelId,omitempty"`
ScopedVars map[string]struct {
Selected bool `json:"selected"`
Text string `json:"text"`
Value string `json:"value"`
} `json:"scopedVars,omitempty"`
Span float32 `json:"span"` // general
Title string `json:"title"` // general
Description *string `json:"description,omitempty"` // general
Transparent bool `json:"transparent"`
Type string `json:"type"`
Alert *Alert `json:"alert,omitempty"`
}
AlertEvaluator struct {
Params []float64 `json:"params,omitempty"`
Type string `json:"type,omitempty"`
}
AlertOperator struct {
Type string `json:"type,omitempty"`
}
AlertQuery struct {
Params []string `json:"params,omitempty"`
}
AlertReducer struct {
Params []string `json:"params,omitempty"`
Type string `json:"type,omitempty"`
}
AlertCondition struct {
Evaluator AlertEvaluator `json:"evaluator,omitempty"`
Operator AlertOperator `json:"operator,omitempty"`
Query AlertQuery `json:"query,omitempty"`
Reducer AlertReducer `json:"reducer,omitempty"`
Type string `json:"type,omitempty"`
}
Alert struct {
AlertRuleTags map[string]string `json:"alertRuleTags,omitempty"`
Conditions []AlertCondition `json:"conditions,omitempty"`
ExecutionErrorState string `json:"executionErrorState,omitempty"`
Frequency string `json:"frequency,omitempty"`
Handler int `json:"handler,omitempty"`
Name string `json:"name,omitempty"`
NoDataState string `json:"noDataState,omitempty"`
Notifications []AlertNotification `json:"notifications,omitempty"`
Message string `json:"message,omitempty"`
For string `json:"for,omitempty"`
}
GraphPanel struct {
AliasColors interface{} `json:"aliasColors"` // XXX
Bars bool `json:"bars"`
DashLength *uint `json:"dashLength,omitempty"`
Dashes *bool `json:"dashes,omitempty"`
Decimals *int `json:"decimals,omitempty"`
Fill int `json:"fill"`
// Grid grid `json:"grid"` obsoleted in 4.1 by xaxis and yaxis
Legend Legend `json:"legend,omitempty"`
LeftYAxisLabel *string `json:"leftYAxisLabel,omitempty"`
Lines bool `json:"lines"`
Linewidth uint `json:"linewidth"`
NullPointMode string `json:"nullPointMode"`
Percentage bool `json:"percentage"`
Pointradius float32 `json:"pointradius"`
Points bool `json:"points"`
RightYAxisLabel *string `json:"rightYAxisLabel,omitempty"`
SeriesOverrides []SeriesOverride `json:"seriesOverrides,omitempty"`
SpaceLength *uint `json:"spaceLength,omitempty"`
Stack bool `json:"stack"`
SteppedLine bool `json:"steppedLine"`
Targets []Target `json:"targets,omitempty"`
Thresholds []Threshold `json:"thresholds,omitempty"`
TimeFrom *string `json:"timeFrom,omitempty"`
TimeShift *string `json:"timeShift,omitempty"`
Tooltip Tooltip `json:"tooltip"`
XAxis bool `json:"x-axis,omitempty"`
YAxis bool `json:"y-axis,omitempty"`
YFormats []string `json:"y_formats,omitempty"`
Xaxis Axis `json:"xaxis"` // was added in Grafana 4.x?
Yaxes []Axis `json:"yaxes"` // was added in Grafana 4.x?
FieldConfig *FieldConfig `json:"fieldConfig,omitempty"`
}
FieldConfig struct {
Defaults FieldConfigDefaults `json:"defaults"`
}
Options struct {
Orientation string `json:"orientation"`
TextMode string `json:"textMode"`
ColorMode string `json:"colorMode"`
GraphMode string `json:"graphMode"`
JustifyMode string `json:"justifyMode"`
DisplayMode string `json:"displayMode"`
Content string `json:"content"`
Mode string `json:"mode"`
ReduceOptions struct {
Values bool `json:"values"`
Fields string `json:"fields"`
Calcs []string `json:"calcs"`
} `json:"reduceOptions"`
}
Threshold struct {
// the alert threshold value, we do not omitempty, since 0 is a valid
// threshold
Value float32 `json:"value"`
// critical, warning, ok, custom
ColorMode string `json:"colorMode,omitempty"`
// gt or lt
Op string `json:"op,omitempty"`
Fill bool `json:"fill"`
Line bool `json:"line"`
// hexadecimal color (e.g. #629e51, only when ColorMode is "custom")
FillColor string `json:"fillColor,omitempty"`
// hexadecimal color (e.g. #629e51, only when ColorMode is "custom")
LineColor string `json:"lineColor,omitempty"`
// left or right
Yaxis string `json:"yaxis,omitempty"`
}
Tooltip struct {
Shared bool `json:"shared"`
ValueType string `json:"value_type"`
MsResolution bool `json:"msResolution,omitempty"` // was added in Grafana 3.x
Sort int `json:"sort,omitempty"`
}
TablePanel struct {
Columns []Column `json:"columns"`
Sort *Sort `json:"sort,omitempty"`
Styles []ColumnStyle `json:"styles"`
Transform string `json:"transform"`
Targets []Target `json:"targets,omitempty"`
Scroll bool `json:"scroll"` // from grafana 3.x
}
TextPanel struct {
Content string `json:"content"`
Mode string `json:"mode"`
PageSize uint `json:"pageSize"`
Scroll bool `json:"scroll"`
ShowHeader bool `json:"showHeader"`
Sort Sort `json:"sort"`
Styles []ColumnStyle `json:"styles"`
FieldConfig FieldConfig `json:"fieldConfig"`
Options struct {
Content string `json:"content"`
Mode string `json:"mode"`
} `json:"options"`
}
SinglestatPanel struct {
Colors []string `json:"colors"`
ColorValue bool `json:"colorValue"`
ColorBackground bool `json:"colorBackground"`
Decimals int `json:"decimals"`
Format string `json:"format"`
Gauge Gauge `json:"gauge,omitempty"`
MappingType *uint `json:"mappingType,omitempty"`
MappingTypes []*MapType `json:"mappingTypes,omitempty"`
MaxDataPoints *IntString `json:"maxDataPoints,omitempty"`
NullPointMode string `json:"nullPointMode"`
Postfix *string `json:"postfix,omitempty"`
PostfixFontSize *string `json:"postfixFontSize,omitempty"`
Prefix *string `json:"prefix,omitempty"`
PrefixFontSize *string `json:"prefixFontSize,omitempty"`
RangeMaps []*RangeMap `json:"rangeMaps,omitempty"`
SparkLine SparkLine `json:"sparkline,omitempty"`
Targets []Target `json:"targets,omitempty"`
Thresholds string `json:"thresholds"`
ValueFontSize string `json:"valueFontSize"`
ValueMaps []ValueMap `json:"valueMaps"`
ValueName string `json:"valueName"`
}
StatPanel struct {
Colors []string `json:"colors"`
ColorValue bool `json:"colorValue"`
ColorBackground bool `json:"colorBackground"`
Decimals int `json:"decimals"`
Format string `json:"format"`
FieldConfig *FieldConfig `json:"fieldConfig,omitempty"`
Gauge Gauge `json:"gauge,omitempty"`
MappingType *uint `json:"mappingType,omitempty"`
MappingTypes []*MapType `json:"mappingTypes,omitempty"`
MaxDataPoints *IntString `json:"maxDataPoints,omitempty"`
NullPointMode string `json:"nullPointMode"`
Postfix *string `json:"postfix,omitempty"`
PostfixFontSize *string `json:"postfixFontSize,omitempty"`
Prefix *string `json:"prefix,omitempty"`
PrefixFontSize *string `json:"prefixFontSize,omitempty"`
RangeMaps []*RangeMap `json:"rangeMaps,omitempty"`
SparkLine SparkLine `json:"sparkline,omitempty"`
Targets []Target `json:"targets,omitempty"`
Thresholds string `json:"thresholds"`
ValueFontSize string `json:"valueFontSize"`
ValueMaps []ValueMap `json:"valueMaps"`
ValueName string `json:"valueName"`
Options Options `json:"options"`
}
DashlistPanel struct {
Mode string `json:"mode"`
Query string `json:"query"`
Tags []string `json:"tags"`
FolderID int `json:"folderId"`
Limit int `json:"limit"`
Headings bool `json:"headings"`
Recent bool `json:"recent"`
Search bool `json:"search"`
Starred bool `json:"starred"`
}
PluginlistPanel struct {
Limit int `json:"limit,omitempty"`
}
AlertlistPanel struct {
OnlyAlertsOnDashboard bool `json:"onlyAlertsOnDashboard"`
Show string `json:"show"`
SortOrder int `json:"sortOrder"`
Limit int `json:"limit"`
StateFilter []string `json:"stateFilter"`
NameFilter string `json:"nameFilter,omitempty"`
DashboardTags []string `json:"dashboardTags,omitempty"`
}
BarGaugePanel struct {
Options Options `json:"options"`
Targets []Target `json:"targets,omitempty"`
FieldConfig FieldConfig `json:"fieldConfig"`
}
RowPanel struct {
Panels []Panel `json:"panels"`
Collapsed bool `json:"collapsed"`
}
HeatmapPanel struct {
Cards struct {
CardPadding *float64 `json:"cardPadding"`
CardRound *float64 `json:"cardRound"`
} `json:"cards"`
Color struct {
CardColor string `json:"cardColor"`
ColorScale string `json:"colorScale"`
ColorScheme string `json:"colorScheme"`
Exponent float64 `json:"exponent"`
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
Mode string `json:"mode"`
} `json:"color"`
DataFormat string `json:"dataFormat"`
HideZeroBuckets bool `json:"hideZeroBuckets"`
HighlightCards bool `json:"highlightCards"`
Legend struct {
Show bool `json:"show"`
} `json:"legend"`
ReverseYBuckets bool `json:"reverseYBuckets"`
Targets []Target `json:"targets,omitempty"`
Tooltip struct {
Show bool `json:"show"`
ShowHistogram bool `json:"showHistogram"`
} `json:"tooltip"`
TooltipDecimals int `json:"tooltipDecimals"`
XAxis struct {
Show bool `json:"show"`
} `json:"xAxis"`
XBucketNumber *float64 `json:"xBucketNumber"`
XBucketSize *string `json:"xBucketSize"`
YAxis struct {
Decimals *int `json:"decimals"`
Format string `json:"format"`
LogBase int `json:"logBase"`
Show bool `json:"show"`
Max *string `json:"max"`
Min *string `json:"min"`
SplitFactor *float64 `json:"splitFactor"`
} `json:"yAxis"`
YBucketBound string `json:"yBucketBound"`
YBucketNumber *float64 `json:"yBucketNumber"`
YBucketSize *float64 `json:"yBucketSize"`
}
TimeseriesPanel struct {
Targets []Target `json:"targets,omitempty"`
Options TimeseriesOptions `json:"options"`
FieldConfig FieldConfig `json:"fieldConfig"`
}
TimeseriesOptions struct {
Legend TimeseriesLegendOptions `json:"legend,omitempty"`
Tooltip TimeseriesTooltipOptions `json:"tooltip,omitempty"`
}
TimeseriesLegendOptions struct {
Calcs []string `json:"calcs"`
DisplayMode string `json:"displayMode"`
Placement string `json:"placement"`
}
TimeseriesTooltipOptions struct {
Mode string `json:"mode"`
}
FieldConfigDefaults struct {
Unit string `json:"unit"`
Decimals *int `json:"decimals,omitempty"`
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
Color FieldConfigColor `json:"color"`
Thresholds Thresholds `json:"thresholds"`
Custom FieldConfigCustom `json:"custom"`
Links []Link `json:"links,omitempty"`
}
FieldConfigCustom struct {
AxisLabel string `json:"axisLabel,omitempty"`
AxisPlacement string `json:"axisPlacement"`
AxisSoftMin *int `json:"axisSoftMin,omitempty"`
AxisSoftMax *int `json:"axisSoftMax,omitempty"`
BarAlignment int `json:"barAlignment"`
DrawStyle string `json:"drawStyle"`
FillOpacity int `json:"fillOpacity"`
GradientMode string `json:"gradientMode"`
LineInterpolation string `json:"lineInterpolation"`
LineWidth int `json:"lineWidth"`
PointSize int `json:"pointSize"`
ShowPoints string `json:"showPoints"`
SpanNulls bool `json:"spanNulls"`
HideFrom struct {
Legend bool `json:"legend"`
Tooltip bool `json:"tooltip"`
Viz bool `json:"viz"`
} `json:"hideFrom"`
LineStyle struct {
Fill string `json:"fill"`
} `json:"lineStyle"`
ScaleDistribution struct {
Type string `json:"type"`
Log int `json:"log,omitempty"`
} `json:"scaleDistribution"`
Stacking struct {
Group string `json:"group"`
Mode string `json:"mode"`
} `json:"stacking"`
ThresholdsStyle struct {
Mode string `json:"mode"`
} `json:"thresholdsStyle"`
}
Thresholds struct {
Mode string `json:"mode"`
Steps []ThresholdStep `json:"steps"`
}
ThresholdStep struct {
Color string `json:"color"`
Value *float64 `json:"value"`
}
FieldConfigColor struct {
Mode string `json:"mode"`
FixedColor string `json:"fixedColor,omitempty"`
SeriesBy string `json:"seriesBy,omitempty"`
}
CustomPanel map[string]interface{}
)
// for a graph panel
type (
// TODO look at schema versions carefully
// grid was obsoleted by xaxis and yaxes
grid struct { //nolint: unused,deadcode
LeftLogBase *int `json:"leftLogBase"`
LeftMax *int `json:"leftMax"`
LeftMin *int `json:"leftMin"`
RightLogBase *int `json:"rightLogBase"`
RightMax *int `json:"rightMax"`
RightMin *int `json:"rightMin"`
Threshold1 *float64 `json:"threshold1"`
Threshold1Color string `json:"threshold1Color"`
Threshold2 *float64 `json:"threshold2"`
Threshold2Color string `json:"threshold2Color"`
ThresholdLine bool `json:"thresholdLine"`
}
xaxis struct { //nolint:unused,deadcode
Mode string `json:"mode"`
Name interface{} `json:"name"` // TODO what is this?
Show bool `json:"show"`
Values *[]string `json:"values,omitempty"`
}
Axis struct {
Format string `json:"format"`
LogBase int `json:"logBase"`
Decimals int `json:"decimals,omitempty"`
Max *FloatString `json:"max,omitempty"`
Min *FloatString `json:"min,omitempty"`
Show bool `json:"show"`
Label string `json:"label,omitempty"`
}
SeriesOverride struct {
Alias string `json:"alias"`
Bars *bool `json:"bars,omitempty"`
Color *string `json:"color,omitempty"`
Dashes *bool `json:"dashes,omitempty"`
Fill *int `json:"fill,omitempty"`
FillBelowTo *string `json:"fillBelowTo,omitempty"`
Legend *bool `json:"legend,omitempty"`
Lines *bool `json:"lines,omitempty"`
LineWidth *int `json:"linewidth,omitempty"`
Stack *BoolString `json:"stack,omitempty"`
Transform *string `json:"transform,omitempty"`
YAxis *int `json:"yaxis,omitempty"`
ZIndex *int `json:"zindex,omitempty"`
NullPointMode *string `json:"nullPointMode,omitempty"`
}
Sort struct {
Col int `json:"col"`
Desc bool `json:"desc"`
}
Legend struct {
AlignAsTable bool `json:"alignAsTable"`
Avg bool `json:"avg"`
Current bool `json:"current"`
HideEmpty bool `json:"hideEmpty"`
HideZero bool `json:"hideZero"`
Max bool `json:"max"`
Min bool `json:"min"`
RightSide bool `json:"rightSide"`
Show bool `json:"show"`
SideWidth *uint `json:"sideWidth,omitempty"`
Total bool `json:"total"`
Values bool `json:"values"`
}
)
// for a table
type (
Column struct {
TextType string `json:"text"`
Value string `json:"value"`
}
ColumnStyle struct {
Alias *string `json:"alias"`
DateFormat *string `json:"dateFormat,omitempty"`
Pattern string `json:"pattern"`
Type string `json:"type"`
ColorMode *string `json:"colorMode,omitempty"`
Colors *[]string `json:"colors,omitempty"`
Decimals *int `json:"decimals,omitempty"`
Thresholds *[]string `json:"thresholds,omitempty"`
Unit *string `json:"unit,omitempty"`
MappingType int `json:"mappingType,omitempty"`
ValueMaps []ValueMap `json:"valueMaps,omitempty"`
Link bool `json:"link,omitempty"`
LinkTooltip *string `json:"linkTooltip,omitempty"`
LinkUrl *string `json:"linkUrl,omitempty"`
LinkTargetBlank bool `json:"linkTargetBlank,omitempty"`
}
)
// for a stat
type (
ValueMap struct {
Op string `json:"op"`
TextType string `json:"text"`
Value string `json:"value"`
}
Gauge struct {
MaxValue float32 `json:"maxValue"`
MinValue float32 `json:"minValue"`
Show bool `json:"show"`
ThresholdLabels bool `json:"thresholdLabels"`
ThresholdMarkers bool `json:"thresholdMarkers"`
}
SparkLine struct {
FillColor *string `json:"fillColor,omitempty"`
Full bool `json:"full,omitempty"`
LineColor *string `json:"lineColor,omitempty"`
Show bool `json:"show,omitempty"`
YMin *float64 `json:"ymin,omitempty"`
YMax *float64 `json:"ymax,omitempty"`
}
)
// for an any panel
type Target struct {
RefID string `json:"refId"`
Datasource interface{} `json:"datasource,omitempty"`
Hide bool `json:"hide,omitempty"`
// For PostgreSQL
Table string `json:"table,omitempty"`
TimeColumn string `json:"timeColumn,omitempty"`
MetricColumn string `json:"metricColumn,omitempty"`
RawSql string `json:"rawSql,omitempty"`
Select [][]struct {
Params []string `json:"params,omitempty"`
Type string `json:"type,omitempty"`
} `json:"select,omitempty"`
Where []struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Params []string `json:"params,omitempty"`
Datatype string `json:"datatype,omitempty"`
} `json:"where,omitempty"`
Group []struct {
Type string `json:"type,omitempty"`
Params []string `json:"params,omitempty"`
} `json:"group,omitempty"`
// For Prometheus
Expr string `json:"expr,omitempty"`
IntervalFactor int `json:"intervalFactor,omitempty"`
Interval string `json:"interval,omitempty"`
Step int `json:"step,omitempty"`
LegendFormat string `json:"legendFormat,omitempty"`
Instant bool `json:"instant,omitempty"`
Format string `json:"format,omitempty"`
// For InfluxDB
Measurement string `json:"measurement,omitempty"`
// For Elasticsearch
DsType *string `json:"dsType,omitempty"`
Metrics []struct {
ID string `json:"id"`
Field string `json:"field"`
Type string `json:"type"`
} `json:"metrics,omitempty"`
Query string `json:"query,omitempty"`
Alias string `json:"alias,omitempty"`
RawQuery bool `json:"rawQuery,omitempty"`
TimeField string `json:"timeField,omitempty"`
BucketAggs []struct {
ID string `json:"id"`
Field string `json:"field"`
Type string `json:"type"`
Settings struct {
Interval string `json:"interval,omitempty"`
MinDocCount interface{} `json:"min_doc_count"`
Order string `json:"order,omitempty"`
OrderBy string `json:"orderBy,omitempty"`
Size string `json:"size,omitempty"`
} `json:"settings"`
} `json:"bucketAggs,omitempty"`
// For Graphite
Target string `json:"target,omitempty"`
// For CloudWatch
Namespace string `json:"namespace,omitempty"`
MetricName string `json:"metricName,omitempty"`
Statistics []string `json:"statistics,omitempty"`
Dimensions map[string]string `json:"dimensions,omitempty"`
Period string `json:"period,omitempty"`
Region string `json:"region,omitempty"`
// For the Stackdriver data source. Find out more information at
// https:/grafana.com/docs/grafana/v6.0/features/datasources/stackdriver/
ProjectName string `json:"projectName,omitempty"`
AlignOptions []StackdriverAlignOptions `json:"alignOptions,omitempty"`
AliasBy string `json:"aliasBy,omitempty"`
MetricType string `json:"metricType,omitempty"`
MetricKind string `json:"metricKind,omitempty"`
Filters []string `json:"filters,omitempty"`
AlignmentPeriod string `json:"alignmentPeriod,omitempty"`
CrossSeriesReducer string `json:"crossSeriesReducer,omitempty"`
PerSeriesAligner string `json:"perSeriesAligner,omitempty"`
ValueType string `json:"valueType,omitempty"`
GroupBy []string `json:"groupBy,omitempty"`
Tags []struct {
Key string `json:"key,omitempty"`
Operator string `json:"operator,omitempty"`
Value string `json:"value,omitempty"`
} `json:"tags,omitempty"`
}
// StackdriverAlignOptions defines the list of alignment options shown in
// Grafana during query configuration.
type StackdriverAlignOptions struct {
Expanded bool `json:"expanded"`
Label string `json:"label"`
Options []StackdriverAlignOption `json:"options"`
}
// StackdriverAlignOption defines a single alignment option shown in Grafana
// during query configuration.
type StackdriverAlignOption struct {
Label string `json:"label"`
MetricKinds []string `json:"metricKinds"`
Text string `json:"text"`
Value string `json:"value"`
ValueTypes []string `json:"valueTypes"`
}
type MapType struct {
Name *string `json:"name,omitempty"`
Value *int `json:"value,omitempty"`
}
type RangeMap struct {
From *string `json:"from,omitempty"`
Text *string `json:"text,omitempty"`
To *string `json:"to,omitempty"`
}
// NewDashlist initializes panel with a dashlist panel.
func NewDashlist(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: DashlistType,
Title: title,
Type: "dashlist",
Renderer: &render,
IsNew: true},
DashlistPanel: &DashlistPanel{}}
}
// NewGraph initializes panel with a graph panel.
func NewGraph(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: GraphType,
Title: title,
Type: "graph",
Renderer: &render,
Span: 12,
IsNew: true},
GraphPanel: &GraphPanel{
NullPointMode: "connected",
Pointradius: 5,
XAxis: true,
YAxis: true,
}}
}
// NewTimeseries initializes panel with a timeseries panel.
func NewTimeseries(title string) *Panel {
if title == "" {
title = "Panel Title"
}
return &Panel{
CommonPanel: CommonPanel{
OfType: TimeseriesType,
Title: title,
Type: "timeseries",
Span: 12,
IsNew: true,
},
TimeseriesPanel: &TimeseriesPanel{
FieldConfig: FieldConfig{
Defaults: FieldConfigDefaults{
Color: FieldConfigColor{
Mode: "palette-classic",
FixedColor: "green",
SeriesBy: "last",
},
},
},
},
}
}
// NewTable initializes panel with a table panel.
func NewTable(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: TableType,
Title: title,
Type: "table",
Renderer: &render,
IsNew: true},
TablePanel: &TablePanel{}}
}
// NewText initializes panel with a text panel.
func NewText(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: TextType,
Title: title,
Type: "text",
Renderer: &render,
IsNew: true},
TextPanel: &TextPanel{}}
}
// NewSinglestat initializes panel with a singlestat panel.
func NewSinglestat(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: SinglestatType,
Title: title,
Type: "singlestat",
Renderer: &render,
IsNew: true},
SinglestatPanel: &SinglestatPanel{}}
}
// NewStat initializes panel with a stat panel.
func NewStat(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: StatType,
Title: title,
Type: "stat",
Renderer: &render,
IsNew: true},
StatPanel: &StatPanel{
FieldConfig: &FieldConfig{
Defaults: FieldConfigDefaults{
Color: FieldConfigColor{
Mode: "thresholds",
},
},
},
},
}
}
// NewPluginlist initializes panel with a stat panel.
func NewPluginlist(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: PluginlistType,
Title: title,
Type: "pluginlist",
Renderer: &render,
IsNew: true},
PluginlistPanel: &PluginlistPanel{}}
}
func NewAlertlist(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: AlertlistType,
Title: title,
Type: "alertlist",
Renderer: &render,
IsNew: true},
AlertlistPanel: &AlertlistPanel{}}
}
func NewHeatmap(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: HeatmapType,
Title: title,
Type: "heatmap",
Renderer: &render,
IsNew: true},
HeatmapPanel: &HeatmapPanel{}}
}
// NewCustom initializes panel with a stat panel.
func NewCustom(title string) *Panel {
if title == "" {
title = "Panel Title"
}
render := "flot"
return &Panel{
CommonPanel: CommonPanel{
OfType: CustomType,
Title: title,
Type: "singlestat",
Renderer: &render,
IsNew: true},
CustomPanel: &CustomPanel{}}
}
// ResetTargets delete all targets defined for a panel.
func (p *Panel) ResetTargets() {
switch p.OfType {
case GraphType:
p.GraphPanel.Targets = nil
case SinglestatType:
p.SinglestatPanel.Targets = nil
case StatType:
p.StatPanel.Targets = nil
case TableType:
p.TablePanel.Targets = nil
case BarGaugeType:
p.BarGaugePanel.Targets = nil
case HeatmapType:
p.HeatmapPanel.Targets = nil
case TimeseriesType:
p.TimeseriesPanel.Targets = nil
}
}
// AddTarget adds a new target as defined in the argument
// but with refId letter incremented. Value of refID from
// the argument will be used only if no target with such
// value already exists.
func (p *Panel) AddTarget(t *Target) {
switch p.OfType {
case GraphType:
p.GraphPanel.Targets = append(p.GraphPanel.Targets, *t)
case SinglestatType:
p.SinglestatPanel.Targets = append(p.SinglestatPanel.Targets, *t)
case StatType:
p.StatPanel.Targets = append(p.StatPanel.Targets, *t)
case TableType:
p.TablePanel.Targets = append(p.TablePanel.Targets, *t)
case HeatmapType:
p.HeatmapPanel.Targets = append(p.HeatmapPanel.Targets, *t)
case TimeseriesType:
p.TimeseriesPanel.Targets = append(p.TimeseriesPanel.Targets, *t)
}
// TODO check for existing refID
}
// SetTarget updates a target if target with such refId exists
// or creates a new one.
func (p *Panel) SetTarget(t *Target) {
setTarget := func(t *Target, targets *[]Target) {
for i, target := range *targets {
if t.RefID == target.RefID {
(*targets)[i] = *t
return
}
}
(*targets) = append((*targets), *t)
}
switch p.OfType {
case GraphType:
setTarget(t, &p.GraphPanel.Targets)
case SinglestatType:
setTarget(t, &p.SinglestatPanel.Targets)
case StatType:
setTarget(t, &p.StatPanel.Targets)
case TableType:
setTarget(t, &p.TablePanel.Targets)
case HeatmapType:
setTarget(t, &p.HeatmapPanel.Targets)
case TimeseriesType:
setTarget(t, &p.TimeseriesPanel.Targets)
}
}
// MapDatasources on all existing targets for the panel.
func (p *Panel) RepeatDatasourcesForEachTarget(dsNames ...string) {
repeatDS := func(dsNames []string, targets *[]Target) {
var refID = "A"
originalTargets := *targets
cleanedTargets := make([]Target, 0, len(originalTargets)*len(dsNames))
*targets = cleanedTargets
for _, target := range originalTargets {
for _, ds := range dsNames {
newTarget := target
newTarget.RefID = refID
newTarget.Datasource = ds
refID = incRefID(refID)
*targets = append(*targets, newTarget)
}
}
}
switch p.OfType {
case GraphType:
repeatDS(dsNames, &p.GraphPanel.Targets)
case SinglestatType:
repeatDS(dsNames, &p.SinglestatPanel.Targets)
case StatType:
repeatDS(dsNames, &p.StatPanel.Targets)
case TableType:
repeatDS(dsNames, &p.TablePanel.Targets)
case HeatmapType:
repeatDS(dsNames, &p.HeatmapPanel.Targets)
case TimeseriesType:
repeatDS(dsNames, &p.TimeseriesPanel.Targets)
}
}
// RepeatTargetsForDatasources repeats all existing targets for a panel
// for all provided in the argument datasources. Existing datasources of
// targets are ignored.
func (p *Panel) RepeatTargetsForDatasources(dsNames ...string) {
repeatTarget := func(dsNames []string, targets *[]Target) {
var lastRefID string
lenTargets := len(*targets)
for i, name := range dsNames {
if i < lenTargets {
(*targets)[i].Datasource = name
lastRefID = (*targets)[i].RefID
} else {
newTarget := (*targets)[i%lenTargets]
lastRefID = incRefID(lastRefID)
newTarget.RefID = lastRefID
newTarget.Datasource = name
*targets = append(*targets, newTarget)
}
}
}
switch p.OfType {
case GraphType: