scripted_async.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 (in the ARGS variable)
  6. *
  7. * Global accessible variables
  8. * window, document, $, jQuery, ARGS, moment
  9. *
  10. * Return a dashboard object, or a function
  11. *
  12. * For async scripts, return a function, this function must take a single callback function,
  13. * call this function with the dashboard object
  14. */
  15. 'use strict';
  16. // accessible variables in this scope
  17. var window, document, ARGS, $, jQuery, moment, kbn;
  18. return function(callback) {
  19. // Setup some variables
  20. var dashboard;
  21. // Initialize a skeleton with nothing but a rows array and service object
  22. dashboard = {
  23. rows : [],
  24. services : {}
  25. };
  26. // Set a title
  27. dashboard.title = 'Scripted dash';
  28. // Set default time
  29. // time can be overridden in the url using from/to parameters, but this is
  30. // handled automatically in grafana core during dashboard initialization
  31. dashboard.time = {
  32. from: "now-6h",
  33. to: "now"
  34. };
  35. var rows = 1;
  36. var seriesName = 'argName';
  37. if(!_.isUndefined(ARGS.rows)) {
  38. rows = parseInt(ARGS.rows, 10);
  39. }
  40. if(!_.isUndefined(ARGS.name)) {
  41. seriesName = ARGS.name;
  42. }
  43. $.ajax({
  44. method: 'GET',
  45. url: '/'
  46. })
  47. .done(function(result) {
  48. dashboard.rows.push({
  49. title: 'Chart',
  50. height: '300px',
  51. panels: [
  52. {
  53. title: 'Async dashboard test',
  54. type: 'text',
  55. span: 12,
  56. fill: 1,
  57. content: '# Async test'
  58. }
  59. ]
  60. });
  61. // when dashboard is composed call the callback
  62. // function and pass the dashboard
  63. callback(dashboard);
  64. });
  65. }