scripted_templated.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* global _ */
  2. /*
  3. * Complex scripted dashboard
  4. * This script generates a dashboard object that Grafana can load. It also takes a number of user
  5. * supplied URL parameters (int ARGS variable)
  6. *
  7. * Return a dashboard object, or a function
  8. *
  9. * For async scripts, return a function, this function must take a single callback function as argument,
  10. * call this callback function with the dashboard object (look at scripted_async.js for an example)
  11. */
  12. 'use strict';
  13. // accessable variables in this scope
  14. var window, document, ARGS, $, jQuery, moment, kbn;
  15. // Setup some variables
  16. var dashboard;
  17. // All url parameters are available via the ARGS object
  18. var ARGS;
  19. // Intialize a skeleton with nothing but a rows array and service object
  20. dashboard = {
  21. rows : [],
  22. };
  23. // Set a title
  24. dashboard.title = 'Scripted and templated dash';
  25. // Set default time
  26. // time can be overriden in the url using from/to parameteres, but this is
  27. // handled automatically in grafana core during dashboard initialization
  28. dashboard.time = {
  29. from: "now-6h",
  30. to: "now"
  31. };
  32. dashboard.templating = {
  33. enable: true,
  34. list: [
  35. {
  36. name: 'test',
  37. query: 'apps.backend.*',
  38. refresh: true,
  39. options: [],
  40. current: null,
  41. },
  42. {
  43. name: 'test2',
  44. query: '*',
  45. refresh: true,
  46. options: [],
  47. current: null,
  48. }
  49. ]
  50. };
  51. var rows = 1;
  52. var seriesName = 'argName';
  53. if(!_.isUndefined(ARGS.rows)) {
  54. rows = parseInt(ARGS.rows, 10);
  55. }
  56. if(!_.isUndefined(ARGS.name)) {
  57. seriesName = ARGS.name;
  58. }
  59. for (var i = 0; i < rows; i++) {
  60. dashboard.rows.push({
  61. title: 'Chart',
  62. height: '300px',
  63. panels: [
  64. {
  65. title: 'Events',
  66. type: 'graph',
  67. span: 12,
  68. fill: 1,
  69. linewidth: 2,
  70. targets: [
  71. {
  72. 'target': "randomWalk('" + seriesName + "')"
  73. },
  74. {
  75. 'target': "randomWalk('[[test2]]')"
  76. }
  77. ],
  78. }
  79. ]
  80. });
  81. }
  82. return dashboard;