Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for gauge #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions board_visual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sdk_test

import (
"context"
"encoding/json"
"fmt"
"log"
"testing"
Expand Down Expand Up @@ -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}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I have some issues making my test case work with chromedp. Hope you could give some advice regarding that.

Were you talking about this part specifically? Is there any reason you've added the raw JSON here instead of using the functions to "construct" this board?

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")
}
}
19 changes: 19 additions & 0 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
RowType
BarGaugeType
HeatmapType
GaugeType
)

const MixedSource = "-- Mixed --"
Expand All @@ -59,6 +60,7 @@ type (
*AlertlistPanel
*BarGaugePanel
*HeatmapPanel
*GaugePanel
*CustomPanel
}
panelType int8
Expand Down Expand Up @@ -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{}
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down