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

Example code for creating new dashboard with row #212

Open
ykyuen opened this issue Jun 8, 2022 · 2 comments
Open

Example code for creating new dashboard with row #212

ykyuen opened this issue Jun 8, 2022 · 2 comments

Comments

@ykyuen
Copy link

ykyuen commented Jun 8, 2022

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

@GiedriusS
Copy link
Collaborator

What is the resulting JSON of this dashboard? 🤔

@ykyuen
Copy link
Author

ykyuen commented Jun 16, 2022

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants