dashboardSrv.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'kbn',
  5. 'lodash',
  6. 'moment',
  7. '../timer',
  8. ],
  9. function (angular, $, kbn, _, moment) {
  10. 'use strict';
  11. var module = angular.module('grafana.services');
  12. module.factory('dashboardSrv', function(timer, $rootScope, $timeout) {
  13. function DashboardModel (data) {
  14. if (!data) {
  15. data = {};
  16. }
  17. this.id = data.id || null;
  18. this.title = data.title || 'No Title';
  19. this.originalTitle = this.title;
  20. this.tags = data.tags || [];
  21. this.style = data.style || "dark";
  22. this.timezone = data.timezone || 'browser';
  23. this.editable = data.editble || true;
  24. this.rows = data.rows || [];
  25. this.pulldowns = data.pulldowns || [];
  26. this.nav = data.nav || [];
  27. this.time = data.time || { from: 'now-6h', to: 'now' };
  28. this.templating = data.templating || { list: [] };
  29. this.refresh = data.refresh;
  30. this.version = data.version || 0;
  31. if (this.nav.length === 0) {
  32. this.nav.push({ type: 'timepicker' });
  33. }
  34. if (!_.findWhere(this.pulldowns, {type: 'filtering'})) {
  35. this.pulldowns.push({ type: 'filtering', enable: false });
  36. }
  37. if (!_.findWhere(this.pulldowns, {type: 'annotations'})) {
  38. this.pulldowns.push({ type: 'annotations', enable: false });
  39. }
  40. this.updateSchema(data);
  41. }
  42. var p = DashboardModel.prototype;
  43. p.getNextPanelId = function() {
  44. var i, j, row, panel, max = 0;
  45. for (i = 0; i < this.rows.length; i++) {
  46. row = this.rows[i];
  47. for (j = 0; j < row.panels.length; j++) {
  48. panel = row.panels[j];
  49. if (panel.id > max) { max = panel.id; }
  50. }
  51. }
  52. return max + 1;
  53. };
  54. p.rowSpan = function(row) {
  55. return _.reduce(row.panels, function(p,v) {
  56. return p + v.span;
  57. },0);
  58. };
  59. p.add_panel = function(panel, row) {
  60. var rowSpan = this.rowSpan(row);
  61. var panelCount = row.panels.length;
  62. var space = (12 - rowSpan) - panel.span;
  63. panel.id = this.getNextPanelId();
  64. // try to make room of there is no space left
  65. if (space <= 0) {
  66. if (panelCount === 1) {
  67. row.panels[0].span = 6;
  68. panel.span = 6;
  69. }
  70. else if (panelCount === 2) {
  71. row.panels[0].span = 4;
  72. row.panels[1].span = 4;
  73. panel.span = 4;
  74. }
  75. }
  76. row.panels.push(panel);
  77. };
  78. p.duplicatePanel = function(panel, row) {
  79. var rowIndex = _.indexOf(this.rows, row);
  80. var newPanel = angular.copy(panel);
  81. newPanel.id = this.getNextPanelId();
  82. while(rowIndex < this.rows.length) {
  83. var currentRow = this.rows[rowIndex];
  84. if (this.rowSpan(currentRow) <= 9) {
  85. currentRow.panels.push(newPanel);
  86. return;
  87. }
  88. rowIndex++;
  89. }
  90. var newRow = angular.copy(row);
  91. newRow.panels = [newPanel];
  92. this.rows.push(newRow);
  93. };
  94. p.formatDate = function(date, format) {
  95. format = format || 'YYYY-MM-DD HH:mm:ss';
  96. return this.timezone === 'browser' ?
  97. moment(date).format(format) :
  98. moment.utc(date).format(format);
  99. };
  100. p.emit_refresh = function() {
  101. $rootScope.$broadcast('refresh');
  102. };
  103. p.start_scheduled_refresh = function (after_ms) {
  104. this.cancel_scheduled_refresh();
  105. this.refresh_timer = timer.register($timeout(function () {
  106. this.start_scheduled_refresh(after_ms);
  107. this.emit_refresh();
  108. }.bind(this), after_ms));
  109. };
  110. p.cancel_scheduled_refresh = function () {
  111. timer.cancel(this.refresh_timer);
  112. };
  113. p.set_interval = function (interval) {
  114. this.refresh = interval;
  115. if (interval) {
  116. var _i = kbn.interval_to_ms(interval);
  117. this.start_scheduled_refresh(_i);
  118. } else {
  119. this.cancel_scheduled_refresh();
  120. }
  121. };
  122. p.updateSchema = function(old) {
  123. var oldVersion = this.version;
  124. var panelUpgrades = [];
  125. this.version = 4;
  126. if (oldVersion === 4) {
  127. return;
  128. }
  129. // version 2 schema changes
  130. if (oldVersion < 2) {
  131. if (old.services) {
  132. if (old.services.filter) {
  133. this.time = old.services.filter.time;
  134. this.templating.list = old.services.filter.list;
  135. }
  136. delete this.services;
  137. }
  138. panelUpgrades.push(function(panel) {
  139. // rename panel type
  140. if (panel.type === 'graphite') {
  141. panel.type = 'graph';
  142. }
  143. if (panel.type !== 'graph') {
  144. return;
  145. }
  146. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  147. if (panel.grid) {
  148. if (panel.grid.min) {
  149. panel.grid.leftMin = panel.grid.min;
  150. delete panel.grid.min;
  151. }
  152. if (panel.grid.max) {
  153. panel.grid.leftMax = panel.grid.max;
  154. delete panel.grid.max;
  155. }
  156. }
  157. if (panel.y_format) {
  158. panel.y_formats[0] = panel.y_format;
  159. delete panel.y_format;
  160. }
  161. if (panel.y2_format) {
  162. panel.y_formats[1] = panel.y2_format;
  163. delete panel.y2_format;
  164. }
  165. });
  166. }
  167. // schema version 3 changes
  168. if (oldVersion < 3) {
  169. // ensure panel ids
  170. var maxId = this.getNextPanelId();
  171. panelUpgrades.push(function(panel) {
  172. if (!panel.id) {
  173. panel.id = maxId;
  174. maxId += 1;
  175. }
  176. });
  177. }
  178. // schema version 4 changes
  179. if (oldVersion < 4) {
  180. // move aliasYAxis changes
  181. panelUpgrades.push(function(panel) {
  182. if (panel.type !== 'graph') { return; }
  183. _.each(panel.aliasYAxis, function(value, key) {
  184. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  185. });
  186. delete panel.aliasYAxis;
  187. });
  188. }
  189. if (panelUpgrades.length === 0) {
  190. return;
  191. }
  192. for (var i = 0; i < this.rows.length; i++) {
  193. var row = this.rows[i];
  194. for (var j = 0; j < row.panels.length; j++) {
  195. for (var k = 0; k < panelUpgrades.length; k++) {
  196. panelUpgrades[k](row.panels[j]);
  197. }
  198. }
  199. }
  200. };
  201. return {
  202. create: function(dashboard) {
  203. return new DashboardModel(dashboard);
  204. }
  205. };
  206. });
  207. });