sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Example code for creating new dashboard with row

Open ykyuen opened this issue 3 years ago • 2 comments
trafficstars

Hi all,

i follow the example code and try creating a new dashboard.

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/grafana-tools/sdk"
)

func main() {
	const GRAFANA_URL = "http://localhost:3000"
	const GRAFANA_API_KEY = "<GRAFANA_API_KEY>"

	// Initialize the grafana client
	grafanaClient, err := sdk.NewClient(GRAFANA_URL, GRAFANA_API_KEY, sdk.DefaultHTTPClient)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create a client: %s\n", err)
		os.Exit(1)
	}

	// Create dashboard
	board := sdk.NewBoard("Sample dashboard title")
	board.ID = 1
	board.Time.From = "now-30m"
	board.Time.To = "now"
	row1 := board.AddRow("Sample row title")
	row1.Add(sdk.NewGraph("Sample graph"))
	graph := sdk.NewGraph("Sample graph 2")
	target := sdk.Target{
		RefID:      "A",
		Datasource: "Sample Source 1",
		Expr:       "sample request 1"}
	graph.AddTarget(&target)
	row1.Add(graph)
	response, err := grafanaClient.SetDashboard(context.TODO() ,*board, sdk.SetDashboardParams{
		Overwrite: false,
	})
	if err != nil {
		fmt.Printf("error on uploading dashboard %s", board.Title)
	} else {
		fmt.Printf("dashboard URL: %v", GRAFANA_URL + *response.URL)
	}
}

The dashboard is successfully created but there is no row with name "Sample row title". sc-2022-06-08-23-55-25

What i suppose to have... sc-2022-06-09-00-02-09

What did i miss?

Thanks for developing this project.

Regards, Kit

ykyuen avatar Jun 08 '22 16:06 ykyuen

What is the resulting JSON of this dashboard? :thinking:

GiedriusS avatar Jun 16 '22 07:06 GiedriusS

Thanks for the follow up. here is the exported json.

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": 34,
  "links": [],
  "panels": [
    {
      "aliasColors": null,
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": null,
      "editable": false,
      "error": false,
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fill": 0,
      "fillGradient": 0,
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "hiddenSeries": false,
      "id": 1,
      "isNew": true,
      "legend": {
        "alignAsTable": false,
        "avg": false,
        "current": false,
        "hideEmpty": false,
        "hideZero": false,
        "max": false,
        "min": false,
        "rightSide": false,
        "show": false,
        "total": false,
        "values": false
      },
      "lines": false,
      "linewidth": 0,
      "nullPointMode": "connected",
      "options": {
        "alertThreshold": true
      },
      "percentage": false,
      "pluginVersion": "7.3.1",
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "Sample graph",
      "tooltip": {
        "shared": false,
        "sort": 0,
        "value_type": ""
      },
      "type": "graph",
      "x-axis": true,
      "xaxis": {
        "buckets": null,
        "format": "",
        "logBase": 0,
        "mode": "time",
        "name": null,
        "show": false,
        "values": []
      },
      "y-axis": true,
      "yaxes": null,
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    },
    {
      "aliasColors": null,
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": null,
      "editable": false,
      "error": false,
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fill": 0,
      "fillGradient": 0,
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 7
      },
      "hiddenSeries": false,
      "id": 2,
      "isNew": true,
      "legend": {
        "alignAsTable": false,
        "avg": false,
        "current": false,
        "hideEmpty": false,
        "hideZero": false,
        "max": false,
        "min": false,
        "rightSide": false,
        "show": false,
        "total": false,
        "values": false
      },
      "lines": false,
      "linewidth": 0,
      "nullPointMode": "connected",
      "options": {
        "alertThreshold": true
      },
      "percentage": false,
      "pluginVersion": "7.3.1",
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "datasource": "Sample Source 1",
          "expr": "sample request 1",
          "refId": "A"
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "Sample graph 2",
      "tooltip": {
        "shared": false,
        "sort": 0,
        "value_type": ""
      },
      "type": "graph",
      "x-axis": true,
      "xaxis": {
        "buckets": null,
        "format": "",
        "logBase": 0,
        "mode": "time",
        "name": null,
        "show": false,
        "values": []
      },
      "y-axis": true,
      "yaxes": null,
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }
  ],
  "schemaVersion": 26,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-30m",
    "to": "now"
  },
  "timepicker": {
    "refresh_intervals": null,
    "time_options": null
  },
  "timezone": "browser",
  "title": "Sample dashboard title",
  "uid": "2PfuKQCnk",
  "version": 1
}

ykyuen avatar Jun 16 '22 15:06 ykyuen