scripted_async.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Global accessable 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 dasboard object
  14. */
  15. 'use strict';
  16. // accessable variables in this scope
  17. var window, document, ARGS, $, jQuery, moment, kbn;
  18. return function(callback) {
  19. // Setup some variables
  20. var dashboard, timspan;
  21. // Set a default timespan if one isn't specified
  22. timspan = '1d';
  23. // Intialize a skeleton with nothing but a rows array and service object
  24. dashboard = {
  25. rows : [],
  26. services : {}
  27. };
  28. // Set a title
  29. dashboard.title = 'Scripted dash';
  30. dashboard.time = {
  31. from: "now-" + (ARGS.from || timspan),
  32. to: "now"
  33. };
  34. var rows = 1;
  35. var seriesName = 'argName';
  36. if(!_.isUndefined(ARGS.rows)) {
  37. rows = parseInt(ARGS.rows, 10);
  38. }
  39. if(!_.isUndefined(ARGS.name)) {
  40. seriesName = ARGS.name;
  41. }
  42. $.ajax({
  43. method: 'GET',
  44. url: '/'
  45. })
  46. .done(function(result) {
  47. dashboard.rows.push({
  48. title: 'Chart',
  49. height: '300px',
  50. panels: [
  51. {
  52. title: 'Async dashboard test',
  53. type: 'text',
  54. span: 12,
  55. fill: 1,
  56. content: '# Async test'
  57. }
  58. ]
  59. });
  60. // when dashboard is composed call the callback
  61. // function and pass the dashboard
  62. callback(dashboard);
  63. });
  64. }