scripted_templated.js 2.0 KB

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