scripted_templated.js 1.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* global _ */

/*
 * Complex scripted dashboard
 * This script generates a dashboard object that Grafana can load. It also takes a number of user
 * supplied URL parameters (int ARGS variable)
 *
 * Return a dashboard object, or a function
 *
 * For async scripts, return a function, this function must take a single callback function as argument,
 * call this callback function with the dashboard object (look at scripted_async.js for an example)
 */

'use strict';

Cedric Cellier committed
16
// accessible variables in this scope
17 18 19
var window, document, ARGS, $, jQuery, moment, kbn;

// Setup some variables
20
var dashboard;
21 22 23 24

// All url parameters are available via the ARGS object
var ARGS;

25
// Initialize a skeleton with nothing but a rows array and service object
26 27
dashboard = {
  rows : [],
28
  schemaVersion: 13,
29 30
};

31

32
// Set a title
33 34 35
dashboard.title = 'Scripted and templated dash';

// Set default time
Cedric Cellier committed
36
// time can be overridden in the url using from/to parameters, but this is
37
// handled automatically in grafana core during dashboard initialization
38
dashboard.time = {
39
  from: "now-6h",
40 41
  to: "now"
};
42

43 44 45 46 47
dashboard.templating = {
  list: [
    {
      name: 'test',
      query: 'apps.backend.*',
48 49 50 51
      refresh: 1,
      type: 'query',
      datasource: null,
      hide: 2,
52 53 54 55
    },
    {
      name: 'test2',
      query: '*',
56 57 58 59
      refresh: 1,
      type: 'query',
      datasource: null,
      hide: 2,
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    }
  ]
};

var rows = 1;
var seriesName = 'argName';

if(!_.isUndefined(ARGS.rows)) {
  rows = parseInt(ARGS.rows, 10);
}

if(!_.isUndefined(ARGS.name)) {
  seriesName = ARGS.name;
}

for (var i = 0; i < rows; i++) {

  dashboard.rows.push({
    title: 'Chart',
    height: '300px',
    panels: [
      {
        title: 'Events',
        type: 'graph',
        span: 12,
        fill: 1,
        linewidth: 2,
        targets: [
          {
            'target': "randomWalk('" + seriesName + "')"
          },
          {
            'target': "randomWalk('[[test2]]')"
          }
        ],
      }
    ]
  });
}


return dashboard;