scripted_async.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }).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. };