scripted_templated.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // accessible 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. // eslint-disable-next-line no-redeclare
  19. var ARGS;
  20. // Initialize a skeleton with nothing but a rows array and service object
  21. dashboard = {
  22. rows: [],
  23. schemaVersion: 13,
  24. };
  25. // Set a title
  26. dashboard.title = 'Scripted and templated dash';
  27. // Set default time
  28. // time can be overridden in the url using from/to parameters, but this is
  29. // handled automatically in grafana core during dashboard initialization
  30. dashboard.time = {
  31. from: 'now-6h',
  32. to: 'now',
  33. };
  34. dashboard.templating = {
  35. list: [
  36. {
  37. name: 'test',
  38. query: 'apps.backend.*',
  39. refresh: 1,
  40. type: 'query',
  41. datasource: null,
  42. hide: 2,
  43. },
  44. {
  45. name: 'test2',
  46. query: '*',
  47. refresh: 1,
  48. type: 'query',
  49. datasource: null,
  50. hide: 2,
  51. },
  52. ],
  53. };
  54. var rows = 1;
  55. var seriesName = 'argName';
  56. if (!_.isUndefined(ARGS.rows)) {
  57. rows = parseInt(ARGS.rows, 10);
  58. }
  59. if (!_.isUndefined(ARGS.name)) {
  60. seriesName = ARGS.name;
  61. }
  62. for (var i = 0; i < rows; i++) {
  63. dashboard.rows.push({
  64. title: 'Chart',
  65. height: '300px',
  66. panels: [
  67. {
  68. title: 'Events',
  69. type: 'graph',
  70. span: 12,
  71. fill: 1,
  72. linewidth: 2,
  73. targets: [
  74. {
  75. target: "randomWalk('" + seriesName + "')",
  76. },
  77. {
  78. target: "randomWalk('[[test2]]')",
  79. },
  80. ],
  81. },
  82. ],
  83. });
  84. }
  85. return dashboard;