dashboardSrv.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'kbn',
  5. 'lodash',
  6. '../timer',
  7. ],
  8. function (angular, $, kbn, _) {
  9. 'use strict';
  10. var module = angular.module('grafana.services');
  11. module.service('dashboardSrv', function(timer, $rootScope, $timeout) {
  12. function DashboardModel (data) {
  13. if (!data) {
  14. data = {};
  15. }
  16. this.title = data.title || 'No Title';
  17. this.tags = data.tags || [];
  18. this.style = data.style || "dark";
  19. this.timezone = data.timezone || 'browser';
  20. this.editable = data.editble || true;
  21. this.rows = data.rows || [];
  22. this.pulldowns = data.pulldowns || [];
  23. this.nav = data.nav || [];
  24. this.time = data.time || { from: 'now-6h', to: 'now' };
  25. this.templating = data.templating || { list: [] };
  26. this.refresh = data.refresh;
  27. if (this.nav.length === 0) {
  28. this.nav.push({ type: 'timepicker' });
  29. }
  30. if (!_.findWhere(this.pulldowns, {type: 'filtering'})) {
  31. this.pulldowns.push({ type: 'filtering', enable: false });
  32. }
  33. if (!_.findWhere(this.pulldowns, {type: 'annotations'})) {
  34. this.pulldowns.push({ type: 'annotations', enable: false });
  35. }
  36. this.updateSchema(data);
  37. }
  38. var p = DashboardModel.prototype;
  39. p.emit_refresh = function() {
  40. $rootScope.$broadcast('refresh');
  41. };
  42. p.start_scheduled_refresh = function (after_ms) {
  43. this.cancel_scheduled_refresh();
  44. this.refresh_timer = timer.register($timeout(function () {
  45. this.start_scheduled_refresh(after_ms);
  46. this.emit_refresh();
  47. }.bind(this), after_ms));
  48. };
  49. p.cancel_scheduled_refresh = function () {
  50. timer.cancel(this.refresh_timer);
  51. };
  52. p.set_interval = function (interval) {
  53. this.refresh = interval;
  54. if (interval) {
  55. var _i = kbn.interval_to_ms(interval);
  56. this.start_scheduled_refresh(_i);
  57. } else {
  58. this.cancel_scheduled_refresh();
  59. }
  60. };
  61. p.updateSchema = function(old) {
  62. var i, j, row, panel;
  63. var isChanged = false;
  64. if (this.version === 2) {
  65. return;
  66. }
  67. if (old.services) {
  68. if (old.services.filter) {
  69. this.time = old.services.filter.time;
  70. this.templating.list = old.services.filter.list;
  71. }
  72. delete this.services;
  73. }
  74. for (i = 0; i < this.rows.length; i++) {
  75. row = this.rows[i];
  76. for (j = 0; j < row.panels.length; j++) {
  77. panel = row.panels[j];
  78. if (panel.type === 'graphite') {
  79. panel.type = 'graph';
  80. isChanged = true;
  81. }
  82. if (panel.type === 'graph') {
  83. if (_.isBoolean(panel.legend)) {
  84. panel.legend = { show: panel.legend };
  85. }
  86. if (panel.grid) {
  87. if (panel.grid.min) {
  88. panel.grid.leftMin = panel.grid.min;
  89. delete panel.grid.min;
  90. }
  91. if (panel.grid.max) {
  92. panel.grid.leftMax = panel.grid.max;
  93. delete panel.grid.max;
  94. }
  95. }
  96. if (panel.y_format) {
  97. panel.y_formats[0] = panel.y_format;
  98. delete panel.y_format;
  99. }
  100. if (panel.y2_format) {
  101. panel.y_formats[1] = panel.y2_format;
  102. delete panel.y2_format;
  103. }
  104. }
  105. }
  106. }
  107. this.version = 2;
  108. };
  109. return {
  110. create: function(dashboard) {
  111. return new DashboardModel(dashboard);
  112. }
  113. };
  114. });
  115. });