From 6f6fa6afe9a2502c9f68404633cd7fe133b43ede Mon Sep 17 00:00:00 2001 From: Tony Koh Date: Wed, 3 Mar 2021 11:39:32 +0800 Subject: [PATCH] add support for gauge --- board_visual_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ panel.go | 19 +++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/board_visual_test.go b/board_visual_test.go index 58910828..be2da79b 100644 --- a/board_visual_test.go +++ b/board_visual_test.go @@ -2,6 +2,7 @@ package sdk_test import ( "context" + "encoding/json" "fmt" "log" "testing" @@ -101,3 +102,53 @@ func TestSinglestatPanel(t *testing.T) { t.Fatalf("expected single-stat panel to have some value") } } + +// Smoke tests for Grafana's singlestat panel. +func TestGaugePanel(t *testing.T) { + // Creating a dashboard using json model + dashboardModelStr := "{\n \"annotations\": {\n \"list\": [\n {\n \"builtIn\": 1,\n \"datasource\": \"-- Grafana --\",\n \"enable\": true,\n \"hide\": true,\n \"iconColor\": \"rgba(0, 211, 255, 1)\",\n \"name\": \"Annotations & Alerts\",\n \"type\": \"dashboard\"\n }\n ]\n },\n \"editable\": true,\n \"gnetId\": null,\n \"graphTooltip\": 0,\n \"id\": 2,\n \"links\": [],\n \"panels\": [\n {\n \"datasource\": \"-- Grafana --\",\n \"fieldConfig\": {\n \"defaults\": {\n \"custom\": {},\n \"mappings\": [],\n \"thresholds\": {\n \"mode\": \"absolute\",\n \"steps\": [\n {\n \"color\": \"green\",\n \"value\": null\n },\n {\n \"color\": \"red\",\n \"value\": 80\n }\n ]\n }\n },\n \"overrides\": []\n },\n \"gridPos\": {\n \"h\": 9,\n \"w\": 12,\n \"x\": 0,\n \"y\": 0\n },\n \"id\": 2,\n \"options\": {\n \"reduceOptions\": {\n \"calcs\": [\n \"mean\"\n ],\n \"fields\": \"\",\n \"values\": false\n },\n \"showThresholdLabels\": false,\n \"showThresholdMarkers\": true\n },\n \"pluginVersion\": \"7.3.6\",\n \"targets\": [\n {\n \"queryType\": \"randomWalk\",\n \"refId\": \"A\"\n }\n ],\n \"timeFrom\": null,\n \"timeShift\": null,\n \"title\": \"Panel Title\",\n \"type\": \"gauge\"\n }\n ],\n \"schemaVersion\": 26,\n \"style\": \"dark\",\n \"tags\": [],\n \"templating\": {\n \"list\": []\n },\n \"time\": {\n \"from\": \"now-6h\",\n \"to\": \"now\"\n },\n \"timepicker\": {},\n \"timezone\": \"\",\n \"title\": \"gaugetest\",\n \"uid\": \"gauge\",\n \"version\": 1\n}" + var board sdk.Board + if err := json.Unmarshal([]byte(dashboardModelStr), &board); err != nil { + t.Fatal("unable to unmarshal gauge panel") + } + + cl := getClient(t) + cl.DeleteDashboard(context.TODO(),"gaugetest") + db, err := cl.SetDashboard(context.TODO(), board, sdk.SetDashboardParams{ + FolderID: sdk.DefaultFolderId, + Overwrite: false, + }) + if err != nil { + t.Fatalf("failed setting dashboard: %v", err) + } + + durl := getDebugURL(t) + + t.Logf("Got Chrome's URL: %s", durl) + actxt, cancelActxt := chromedp.NewRemoteAllocator(context.Background(), durl) + defer cancelActxt() + + ctx, cancel := chromedp.NewContext( + actxt, + chromedp.WithLogf(log.Printf), + ) + defer cancel() + + var res string + + fullAddr := fmt.Sprintf("http://%s%s", "grafana:3000", *db.URL) + t.Logf("Got Grafana's URL: %s", fullAddr) + + err = chromedp.Run(ctx, + chromedp.Navigate(fullAddr), + chromedp.WaitReady(`grafana-app`), + chromedp.TextContent(`span.gauge-panel-value`, &res, chromedp.NodeVisible, chromedp.ByQuery), + ) + if err != nil { + t.Fatalf("running chromedp has failed: %v", err) + } + + if res == "" { + t.Fatalf("expected gauge panel to have some value") + } +} \ No newline at end of file diff --git a/panel.go b/panel.go index 3f38b74d..99714a98 100644 --- a/panel.go +++ b/panel.go @@ -38,6 +38,7 @@ const ( RowType BarGaugeType HeatmapType + GaugeType ) const MixedSource = "-- Mixed --" @@ -59,6 +60,7 @@ type ( *AlertlistPanel *BarGaugePanel *HeatmapPanel + *GaugePanel *CustomPanel } panelType int8 @@ -358,6 +360,11 @@ type ( YBucketNumber *float64 `json:"yBucketNumber"` YBucketSize *float64 `json:"yBucketSize"` } + GaugePanel struct { + Options Options `json:"options"` + Targets []Target `json:"targets,omitempty"` + FieldConfig FieldConfig `json:"fieldConfig"` + } CustomPanel map[string]interface{} ) @@ -971,6 +978,12 @@ func (p *Panel) UnmarshalJSON(b []byte) (err error) { if err = json.Unmarshal(b, &rowpanel); err == nil { p.RowPanel = &rowpanel } + case "gauge": + var gauge GaugePanel + p.OfType = GaugeType + if err = json.Unmarshal(b, &gauge); err == nil { + p.GaugePanel = &gauge + } default: var custom = make(CustomPanel) p.OfType = CustomType @@ -1050,6 +1063,12 @@ func (p *Panel) MarshalJSON() ([]byte, error) { HeatmapPanel }{p.CommonPanel, *p.HeatmapPanel} return json.Marshal(outHeatmap) + case GaugeType: + var outGauge = struct { + CommonPanel + GaugePanel + }{p.CommonPanel, *p.GaugePanel} + return json.Marshal(outGauge) case CustomType: var outCustom = struct { CommonPanel