Skip to content

Commit d7dc772

Browse files
authored
Adding support for stat and bar gauge types (#118)
* adding support for stat and bar gauge types * fixing tests * added missing case * adding fields to dashlist panel type * update template var current text type * added display mode and unit panel option fields
1 parent 5fb28a7 commit d7dc772

File tree

4 files changed

+169
-32
lines changed

4 files changed

+169
-32
lines changed

board.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ type (
107107
// for templateVar
108108
Current struct {
109109
Tags []*string `json:"tags,omitempty"`
110-
Text string `json:"text"`
110+
Text interface{} `json:"text"`
111111
Value interface{} `json:"value"` // TODO select more precise type
112112
}
113113
Annotation struct {

panel.go

Lines changed: 143 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ const (
3434
PluginlistType
3535
AlertlistType
3636
SinglestatType
37+
StatType
3738
RowType
39+
BarGaugeType
3840
)
3941

4042
const MixedSource = "-- Mixed --"
@@ -49,10 +51,12 @@ type (
4951
*TablePanel
5052
*TextPanel
5153
*SinglestatPanel
54+
*StatPanel
5255
*DashlistPanel
5356
*PluginlistPanel
5457
*RowPanel
5558
*AlertlistPanel
59+
*BarGaugePanel
5660
*CustomPanel
5761
}
5862
panelType int8
@@ -82,8 +86,9 @@ type (
8286
Text string `json:"text"`
8387
Value string `json:"value"`
8488
} `json:"scopedVars,omitempty"`
85-
Span float32 `json:"span"` // general
86-
Title string `json:"title"` // general
89+
Span float32 `json:"span"` // general
90+
Title string `json:"title"` // general
91+
Description *string `json:"description,omitempty"` // general
8792
Transparent bool `json:"transparent"`
8893
Type string `json:"type"`
8994
Alert *Alert `json:"alert,omitempty"`
@@ -127,7 +132,6 @@ type (
127132
DashLength *uint `json:"dashLength,omitempty"`
128133
Dashes *bool `json:"dashes,omitempty"`
129134
Decimals *uint `json:"decimals,omitempty"`
130-
Description *string `json:"description,omitempty"`
131135
Fill int `json:"fill"`
132136
// Grid grid `json:"grid"` obsoleted in 4.1 by xaxis and yaxis
133137

@@ -155,6 +159,33 @@ type (
155159
Xaxis Axis `json:"xaxis"` // was added in Grafana 4.x?
156160
Yaxes []Axis `json:"yaxes"` // was added in Grafana 4.x?
157161
}
162+
FieldConfig struct {
163+
Defaults struct {
164+
Unit string `json:"unit"`
165+
Threshold struct {
166+
Mode string `json:"mode"`
167+
Steps []struct {
168+
Color string `json:"color"`
169+
Value string `json:"value"`
170+
} `json:"steps"`
171+
} `json:"threshold"`
172+
} `json:"defaults"`
173+
}
174+
Options struct {
175+
Orientation string `json:"orientation"`
176+
TextMode string `json:"textMode"`
177+
ColorMode string `json:"colorMode"`
178+
GraphMode string `json:"graphMode"`
179+
JustifyMode string `json:"justifyMode"`
180+
DisplayMode string `json:"displayMode"`
181+
Content string `json:"content"`
182+
Mode string `json:"mode"`
183+
ReduceOptions struct {
184+
Values bool `json:"values"`
185+
Fields string `json:"fields"`
186+
Calcs []string `json:"calcs"`
187+
} `json:"reduceOptions"`
188+
}
158189
Threshold struct {
159190
// the alert threshold value, we do not omitempty, since 0 is a valid
160191
// threshold
@@ -188,13 +219,18 @@ type (
188219
Scroll bool `json:"scroll"` // from grafana 3.x
189220
}
190221
TextPanel struct {
191-
Content string `json:"content"`
192-
Mode string `json:"mode"`
193-
PageSize uint `json:"pageSize"`
194-
Scroll bool `json:"scroll"`
195-
ShowHeader bool `json:"showHeader"`
196-
Sort Sort `json:"sort"`
197-
Styles []ColumnStyle `json:"styles"`
222+
Content string `json:"content"`
223+
Mode string `json:"mode"`
224+
PageSize uint `json:"pageSize"`
225+
Scroll bool `json:"scroll"`
226+
ShowHeader bool `json:"showHeader"`
227+
Sort Sort `json:"sort"`
228+
Styles []ColumnStyle `json:"styles"`
229+
FieldConfig FieldConfig `json:"fieldConfig"`
230+
Options struct {
231+
Content string `json:"content"`
232+
Mode string `json:"mode"`
233+
} `json:"options"`
198234
}
199235
SinglestatPanel struct {
200236
Colors []string `json:"colors"`
@@ -219,11 +255,40 @@ type (
219255
ValueMaps []ValueMap `json:"valueMaps"`
220256
ValueName string `json:"valueName"`
221257
}
258+
StatPanel struct {
259+
Colors []string `json:"colors"`
260+
ColorValue bool `json:"colorValue"`
261+
ColorBackground bool `json:"colorBackground"`
262+
Decimals int `json:"decimals"`
263+
Format string `json:"format"`
264+
Gauge Gauge `json:"gauge,omitempty"`
265+
MappingType *uint `json:"mappingType,omitempty"`
266+
MappingTypes []*MapType `json:"mappingTypes,omitempty"`
267+
MaxDataPoints *IntString `json:"maxDataPoints,omitempty"`
268+
NullPointMode string `json:"nullPointMode"`
269+
Postfix *string `json:"postfix,omitempty"`
270+
PostfixFontSize *string `json:"postfixFontSize,omitempty"`
271+
Prefix *string `json:"prefix,omitempty"`
272+
PrefixFontSize *string `json:"prefixFontSize,omitempty"`
273+
RangeMaps []*RangeMap `json:"rangeMaps,omitempty"`
274+
SparkLine SparkLine `json:"sparkline,omitempty"`
275+
Targets []Target `json:"targets,omitempty"`
276+
Thresholds string `json:"thresholds"`
277+
ValueFontSize string `json:"valueFontSize"`
278+
ValueMaps []ValueMap `json:"valueMaps"`
279+
ValueName string `json:"valueName"`
280+
Options Options `json:"options"`
281+
}
222282
DashlistPanel struct {
223-
Mode string `json:"mode"`
224-
Limit uint `json:"limit"`
225-
Query string `json:"query"`
226-
Tags []string `json:"tags"`
283+
Mode string `json:"mode"`
284+
Query string `json:"query"`
285+
Tags []string `json:"tags"`
286+
FolderID int `json:"folderId"`
287+
Limit int `json:"limit"`
288+
Headings bool `json:"headings"`
289+
Recent bool `json:"recent"`
290+
Search bool `json:"search"`
291+
Starred bool `json:"starred"`
227292
}
228293
PluginlistPanel struct {
229294
Limit int `json:"limit,omitempty"`
@@ -237,6 +302,11 @@ type (
237302
NameFilter string `json:"nameFilter,omitempty"`
238303
DashboardTags []string `json:"dashboardTags,omitempty"`
239304
}
305+
BarGaugePanel struct {
306+
Options Options `json:"options"`
307+
Targets []Target `json:"targets,omitempty"`
308+
FieldConfig FieldConfig `json:"fieldConfig"`
309+
}
240310
RowPanel struct {
241311
Panels []Panel `json:"panels"`
242312
Collapsed bool `json:"collapsed"`
@@ -331,7 +401,7 @@ type (
331401
}
332402
)
333403

334-
// for a singlestat
404+
// for a stat
335405
type (
336406
ValueMap struct {
337407
Op string `json:"op"`
@@ -558,7 +628,23 @@ func NewSinglestat(title string) *Panel {
558628
SinglestatPanel: &SinglestatPanel{}}
559629
}
560630

561-
// NewPluginlist initializes panel with a singlestat panel.
631+
// NewStat initializes panel with a stat panel.
632+
func NewStat(title string) *Panel {
633+
if title == "" {
634+
title = "Panel Title"
635+
}
636+
render := "flot"
637+
return &Panel{
638+
CommonPanel: CommonPanel{
639+
OfType: StatType,
640+
Title: title,
641+
Type: "stat",
642+
Renderer: &render,
643+
IsNew: true},
644+
StatPanel: &StatPanel{}}
645+
}
646+
647+
// NewPluginlist initializes panel with a stat panel.
562648
func NewPluginlist(title string) *Panel {
563649
if title == "" {
564650
title = "Panel Title"
@@ -589,7 +675,7 @@ func NewAlertlist(title string) *Panel {
589675
AlertlistPanel: &AlertlistPanel{}}
590676
}
591677

592-
// NewCustom initializes panel with a singlestat panel.
678+
// NewCustom initializes panel with a stat panel.
593679
func NewCustom(title string) *Panel {
594680
if title == "" {
595681
title = "Panel Title"
@@ -612,8 +698,12 @@ func (p *Panel) ResetTargets() {
612698
p.GraphPanel.Targets = nil
613699
case SinglestatType:
614700
p.SinglestatPanel.Targets = nil
701+
case StatType:
702+
p.StatPanel.Targets = nil
615703
case TableType:
616704
p.TablePanel.Targets = nil
705+
case BarGaugeType:
706+
p.BarGaugePanel.Targets = nil
617707
}
618708
}
619709

@@ -627,6 +717,8 @@ func (p *Panel) AddTarget(t *Target) {
627717
p.GraphPanel.Targets = append(p.GraphPanel.Targets, *t)
628718
case SinglestatType:
629719
p.SinglestatPanel.Targets = append(p.SinglestatPanel.Targets, *t)
720+
case StatType:
721+
p.StatPanel.Targets = append(p.StatPanel.Targets, *t)
630722
case TableType:
631723
p.TablePanel.Targets = append(p.TablePanel.Targets, *t)
632724
}
@@ -650,6 +742,8 @@ func (p *Panel) SetTarget(t *Target) {
650742
setTarget(t, &p.GraphPanel.Targets)
651743
case SinglestatType:
652744
setTarget(t, &p.SinglestatPanel.Targets)
745+
case StatType:
746+
setTarget(t, &p.StatPanel.Targets)
653747
case TableType:
654748
setTarget(t, &p.TablePanel.Targets)
655749
}
@@ -677,6 +771,8 @@ func (p *Panel) RepeatDatasourcesForEachTarget(dsNames ...string) {
677771
repeatDS(dsNames, &p.GraphPanel.Targets)
678772
case SinglestatType:
679773
repeatDS(dsNames, &p.SinglestatPanel.Targets)
774+
case StatType:
775+
repeatDS(dsNames, &p.StatPanel.Targets)
680776
case TableType:
681777
repeatDS(dsNames, &p.TablePanel.Targets)
682778
}
@@ -707,6 +803,8 @@ func (p *Panel) RepeatTargetsForDatasources(dsNames ...string) {
707803
repeatTarget(dsNames, &p.GraphPanel.Targets)
708804
case SinglestatType:
709805
repeatTarget(dsNames, &p.SinglestatPanel.Targets)
806+
case StatType:
807+
repeatTarget(dsNames, &p.StatPanel.Targets)
710808
case TableType:
711809
repeatTarget(dsNames, &p.TablePanel.Targets)
712810
}
@@ -720,8 +818,12 @@ func (p *Panel) GetTargets() *[]Target {
720818
return &p.GraphPanel.Targets
721819
case SinglestatType:
722820
return &p.SinglestatPanel.Targets
821+
case StatType:
822+
return &p.StatPanel.Targets
723823
case TableType:
724824
return &p.TablePanel.Targets
825+
case BarGaugeType:
826+
return &p.BarGaugePanel.Targets
725827
default:
726828
return nil
727829
}
@@ -761,12 +863,24 @@ func (p *Panel) UnmarshalJSON(b []byte) (err error) {
761863
if err = json.Unmarshal(b, &singlestat); err == nil {
762864
p.SinglestatPanel = &singlestat
763865
}
866+
case "stat":
867+
var stat StatPanel
868+
p.OfType = StatType
869+
if err = json.Unmarshal(b, &stat); err == nil {
870+
p.StatPanel = &stat
871+
}
764872
case "dashlist":
765873
var dashlist DashlistPanel
766874
p.OfType = DashlistType
767875
if err = json.Unmarshal(b, &dashlist); err == nil {
768876
p.DashlistPanel = &dashlist
769877
}
878+
case "bargauge":
879+
var bargauge BarGaugePanel
880+
p.OfType = BarGaugeType
881+
if err = json.Unmarshal(b, &bargauge); err == nil {
882+
p.BarGaugePanel = &bargauge
883+
}
770884
case "row":
771885
var rowpanel RowPanel
772886
p.OfType = RowType
@@ -810,12 +924,24 @@ func (p *Panel) MarshalJSON() ([]byte, error) {
810924
SinglestatPanel
811925
}{p.CommonPanel, *p.SinglestatPanel}
812926
return json.Marshal(outSinglestat)
927+
case StatType:
928+
var outSinglestat = struct {
929+
CommonPanel
930+
StatPanel
931+
}{p.CommonPanel, *p.StatPanel}
932+
return json.Marshal(outSinglestat)
813933
case DashlistType:
814934
var outDashlist = struct {
815935
CommonPanel
816936
DashlistPanel
817937
}{p.CommonPanel, *p.DashlistPanel}
818938
return json.Marshal(outDashlist)
939+
case BarGaugeType:
940+
var outBarGauge = struct {
941+
CommonPanel
942+
BarGaugePanel
943+
}{p.CommonPanel, *p.BarGaugePanel}
944+
return json.Marshal(outBarGauge)
819945
case PluginlistType:
820946
var outPluginlist = struct {
821947
CommonPanel

rest-dashboard.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,23 @@ const DefaultFolderId = 0
3838

3939
// BoardProperties keeps metadata of a dashboard.
4040
type BoardProperties struct {
41-
IsStarred bool `json:"isStarred,omitempty"`
42-
IsHome bool `json:"isHome,omitempty"`
43-
IsSnapshot bool `json:"isSnapshot,omitempty"`
44-
Type string `json:"type,omitempty"`
45-
CanSave bool `json:"canSave"`
46-
CanEdit bool `json:"canEdit"`
47-
CanStar bool `json:"canStar"`
48-
Slug string `json:"slug"`
49-
Expires time.Time `json:"expires"`
50-
Created time.Time `json:"created"`
51-
Updated time.Time `json:"updated"`
52-
UpdatedBy string `json:"updatedBy"`
53-
CreatedBy string `json:"createdBy"`
54-
Version int `json:"version"`
41+
IsStarred bool `json:"isStarred,omitempty"`
42+
IsHome bool `json:"isHome,omitempty"`
43+
IsSnapshot bool `json:"isSnapshot,omitempty"`
44+
Type string `json:"type,omitempty"`
45+
CanSave bool `json:"canSave"`
46+
CanEdit bool `json:"canEdit"`
47+
CanStar bool `json:"canStar"`
48+
Slug string `json:"slug"`
49+
Expires time.Time `json:"expires"`
50+
Created time.Time `json:"created"`
51+
Updated time.Time `json:"updated"`
52+
UpdatedBy string `json:"updatedBy"`
53+
CreatedBy string `json:"createdBy"`
54+
Version int `json:"version"`
55+
FolderID int `json:"folderId"`
56+
FolderTitle string `json:"folderTitle"`
57+
FolderURL string `json:"folderUrl"`
5558
}
5659

5760
// GetDashboardByUID loads a dashboard and its metadata from Grafana by dashboard uid.

row.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ func (r *Row) AddText(data *TextPanel) {
7070
r.Panels = append(r.Panels, *panel)
7171
}
7272

73+
func (r *Row) AddStat(data *StatPanel) {
74+
lastPanelID++
75+
panel := NewStat("")
76+
panel.ID = lastPanelID
77+
panel.StatPanel = data
78+
r.Panels = append(r.Panels, *panel)
79+
}
80+
7381
func (r *Row) AddSinglestat(data *SinglestatPanel) {
7482
lastPanelID++
7583
panel := NewSinglestat("")

0 commit comments

Comments
 (0)