How-To: Create a Dashboard through GraphQL

You can create a dashboard using the following mutations:

  1. createDashboard mutation

  2. createDashboardFromTemplateV2 mutation

createDashboard Mutation

The mutation takes as an input the array of widgets that will be placed in it.

There is no way to create new widgets from the API, so you need to know which widgets are available in your LogScale instance and build your dashboard on top of those widgets.

You can still customize the widgets by changing the queries, position, and type of widget, and this change will be visible only in the current dashboard (what you need is only the widget ID to place a widget in a dashboard, and then you're free to go with changes on it)

Check the dashboards and widgets available in your repository:

graphql
query {
  repository(name: "<REPO_NAME>") {
    name
    dashboards {
      id
      name
      widgets {
        id
        width
        x
        y
        title
        width
        height
      }
    }
  }
}

Take notes of the available widgets; you will need their IDs later.

Create a new dashboard where you pass the available widget IDs, but you can customize the query, start, end times, position and height:

graphql
mutation {
  createDashboard(
    input: {
      searchDomainName: "<REPO_NAME>"
      name: "<DASHBOARD_NAME>"
      labels: []
      widgets: [
        {
          x: 0
          y: 0
          height: 4
          id: "<WIDGET_ID>"
          title: "Methods Widget"
          width: 4
          queryOptions: {
            start: "1619434800000" #UNIX TIME
            end: "1619607600000" #UNIX TIME
            queryString: "status=200 | method != HEAD | groupBy(\"method\")" #YOUR SEARCH HERE
            widgetType: "pie-chart"
          }
        }
      ]
      description: "New dashboard created via GraphQL API"
    }
  ) {
    dashboard {
      id
      name
    }
  }
}

The widgetType can be:

  • pie-chart

  • bar-chart

  • table-view

  • xy-chart

  • raw

  • simple-gauge

  • world-map

  • sankey

Note

Pay attention to the position (x,y values) of each widget if you put multiple widgets in the dashboards. If you don't change those values, you will see only the last widget of the list because they'll overlap.

createDashboardFromTemplateV2 mutation

Another simpler way to create a dashboard via GraphQL is to start from a YAML template; in this case, you need to pass the template as a JSON string in the parameters of createDashboardFromTemplateV2 mutation:

graphql
mutation {
  createDashboardFromTemplateV2(
    input: {
      searchDomainName: "<REPO_NAME>"
      overrideName: "graphql-dashboard-example"
      template: "{\n  \"name\": \"graphql-dashboard-example\",\n  \"timeSelector\": {},\n  \"sharedTimeInterval\": {\n    \"enabled\": false,\n    \"isLive\": false,\n    \"start\": \"1d\"\n  },\n  \"widgets\": {\n    \"87a17bec-1d9e-46ec-90dd-1011fbcb117b\": {\n      \"x\": 250,\n      \"y\": 0,\n      \"height\": 4,\n      \"queryString\": \"status=200 | method != HEAD | groupBy(\\\"method\\\")\",\n      \"visualization\": \"bar-chart\",\n      \"start\": \"2021-04-26T11:00:00Z\",\n      \"width\": 4,\n      \"title\": \"Methods Widget 2\",\n      \"type\": \"query\"\n    },\n    \"d16f0434-18c9-4b0a-a7b5-d85d66ea6f72\": {\n         \"x\": 0,\n      \"y\": 0,\n      \"height\": 4,\n      \"queryString\": \"status=200 | method = HEAD OR method=PUT| groupBy(\\\"method\\\")\",\n      \"visualization\": \"table-view\",\n      \"start\": \"2021-04-26T11:00:00Z\",\n      \"width\": 4,\n      \"title\": \"Methods Widget\",\n      \"type\": \"query\"\n    }\n  },\n  \"$schema\": \"https://schemas.humio.com/dashboard/v0.1.0\"\n}"
    }
  )
}

This is an example of dashboard template you can start from:

yaml
name: graphql-dashboard-example
timeSelector: {}
sharedTimeInterval:
  enabled: false
  isLive: false
  start: 1d
widgets:
  87a17bec-1d9e-46ec-90dd-1011fbcb117b:
    x: 250
    y: 0
    height: 4
    queryString: status=200 | method != HEAD | groupBy("method")
    visualization: bar-chart
    start: "2021-04-26T11:00:00Z"
    width: 4
    title: Methods Widget 2
    type: query
  d16f0434-18c9-4b0a-a7b5-d85d66ea6f72:
    x: 0
    y: 0
    height: 4
    queryString: status=200 | method = HEAD OR method=PUT| groupBy("method")
    visualization: table-view
    start: "2021-04-26T11:00:00Z"
    width: 4
    title: Methods Widget
    type: query
$schema: https://schemas.humio.com/dashboard/v0.1.0

You can use this website to transform the YAML template to JSON and then escape the JSON using Best Json Escape Characters, Double Quotes and Backslash tool.

You could get a YAML template from an existing dashboard in LogScale and start from it to build what you need in your new dashboard to be built through GraphQL. You can export a dashboard as a Template by choosing the appropriate option from the &vdots; button on the Dashboards page.

Exporting Dashboard

Figure 8. Exporting Dashboard