dashboardSrv.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'kbn',
  5. 'lodash',
  6. 'moment',
  7. ],
  8. function (angular, $, kbn, _, moment) {
  9. 'use strict';
  10. var module = angular.module('grafana.services');
  11. module.factory('dashboardSrv', function() {
  12. function DashboardModel (data, meta) {
  13. if (!data) {
  14. data = {};
  15. }
  16. if (!data.id && data.version) {
  17. data.schemaVersion = data.version;
  18. }
  19. this.id = data.id || null;
  20. this.title = data.title || 'No Title';
  21. this.originalTitle = this.title;
  22. this.tags = data.tags || [];
  23. this.style = data.style || "dark";
  24. this.timezone = data.timezone || 'browser';
  25. this.editable = data.editable === false ? false : true;
  26. this.hideControls = data.hideControls || false;
  27. this.sharedCrosshair = data.sharedCrosshair || false;
  28. this.rows = data.rows || [];
  29. this.nav = data.nav || [];
  30. this.time = data.time || { from: 'now-6h', to: 'now' };
  31. this.templating = this._ensureListExist(data.templating);
  32. this.annotations = this._ensureListExist(data.annotations);
  33. this.refresh = data.refresh;
  34. this.snapshot = data.snapshot;
  35. this.schemaVersion = data.schemaVersion || 0;
  36. this.version = data.version || 0;
  37. this.links = data.links || [];
  38. if (this.nav.length === 0) {
  39. this.nav.push({ type: 'timepicker' });
  40. }
  41. this._updateSchema(data);
  42. this._initMeta(meta);
  43. }
  44. var p = DashboardModel.prototype;
  45. p._initMeta = function(meta) {
  46. meta = meta || {};
  47. meta.canShare = meta.canShare === false ? false : true;
  48. meta.canSave = meta.canSave === false ? false : true;
  49. meta.canStar = meta.canStar === false ? false : true;
  50. meta.canEdit = meta.canEdit === false ? false : true;
  51. if (!this.editable) {
  52. meta.canEdit = false;
  53. meta.canDelete = false;
  54. meta.canSave = false;
  55. this.hideControls = true;
  56. }
  57. this.meta = meta;
  58. };
  59. // cleans meta data and other non peristent state
  60. p.getSaveModelClone = function() {
  61. var copy = angular.copy(this);
  62. delete copy.meta;
  63. return copy;
  64. };
  65. p._ensureListExist = function (data) {
  66. if (!data) { data = {}; }
  67. if (!data.list) { data.list = []; }
  68. return data;
  69. };
  70. p.getNextPanelId = function() {
  71. var i, j, row, panel, max = 0;
  72. for (i = 0; i < this.rows.length; i++) {
  73. row = this.rows[i];
  74. for (j = 0; j < row.panels.length; j++) {
  75. panel = row.panels[j];
  76. if (panel.id > max) { max = panel.id; }
  77. }
  78. }
  79. return max + 1;
  80. };
  81. p.forEachPanel = function(callback) {
  82. var i, j, row;
  83. for (i = 0; i < this.rows.length; i++) {
  84. row = this.rows[i];
  85. for (j = 0; j < row.panels.length; j++) {
  86. callback(row.panels[j], j, row, i);
  87. }
  88. }
  89. };
  90. p.getPanelById = function(id) {
  91. for (var i = 0; i < this.rows.length; i++) {
  92. var row = this.rows[i];
  93. for (var j = 0; j < row.panels.length; j++) {
  94. var panel = row.panels[j];
  95. if (panel.id === id) {
  96. return panel;
  97. }
  98. }
  99. }
  100. return null;
  101. };
  102. p.rowSpan = function(row) {
  103. return _.reduce(row.panels, function(p,v) {
  104. return p + v.span;
  105. },0);
  106. };
  107. p.add_panel = function(panel, row) {
  108. var rowSpan = this.rowSpan(row);
  109. var panelCount = row.panels.length;
  110. var space = (12 - rowSpan) - panel.span;
  111. panel.id = this.getNextPanelId();
  112. // try to make room of there is no space left
  113. if (space <= 0) {
  114. if (panelCount === 1) {
  115. row.panels[0].span = 6;
  116. panel.span = 6;
  117. }
  118. else if (panelCount === 2) {
  119. row.panels[0].span = 4;
  120. row.panels[1].span = 4;
  121. panel.span = 4;
  122. }
  123. }
  124. row.panels.push(panel);
  125. };
  126. p.isSubmenuFeaturesEnabled = function() {
  127. return this.templating.list.length > 0 || this.annotations.list.length > 0 || this.links.length > 0;
  128. };
  129. p.getPanelInfoById = function(panelId) {
  130. var result = {};
  131. _.each(this.rows, function(row) {
  132. _.each(row.panels, function(panel, index) {
  133. if (panel.id === panelId) {
  134. result.panel = panel;
  135. result.row = row;
  136. result.index = index;
  137. return;
  138. }
  139. });
  140. });
  141. if (!result.panel) {
  142. return null;
  143. }
  144. return result;
  145. };
  146. p.duplicatePanel = function(panel, row) {
  147. var rowIndex = _.indexOf(this.rows, row);
  148. var newPanel = angular.copy(panel);
  149. newPanel.id = this.getNextPanelId();
  150. delete newPanel.repeat;
  151. delete newPanel.repeatIteration;
  152. delete newPanel.repeatPanelId;
  153. delete newPanel.scopedVars;
  154. var currentRow = this.rows[rowIndex];
  155. currentRow.panels.push(newPanel);
  156. return newPanel;
  157. };
  158. p.formatDate = function(date, format) {
  159. format = format || 'YYYY-MM-DD HH:mm:ss';
  160. return this.timezone === 'browser' ?
  161. moment(date).format(format) :
  162. moment.utc(date).format(format);
  163. };
  164. p._updateSchema = function(old) {
  165. var i, j, k;
  166. var oldVersion = this.schemaVersion;
  167. var panelUpgrades = [];
  168. this.schemaVersion = 6;
  169. if (oldVersion === 6) {
  170. return;
  171. }
  172. // version 2 schema changes
  173. if (oldVersion < 2) {
  174. if (old.services) {
  175. if (old.services.filter) {
  176. this.time = old.services.filter.time;
  177. this.templating.list = old.services.filter.list || [];
  178. }
  179. delete this.services;
  180. }
  181. panelUpgrades.push(function(panel) {
  182. // rename panel type
  183. if (panel.type === 'graphite') {
  184. panel.type = 'graph';
  185. }
  186. if (panel.type !== 'graph') {
  187. return;
  188. }
  189. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  190. if (panel.grid) {
  191. if (panel.grid.min) {
  192. panel.grid.leftMin = panel.grid.min;
  193. delete panel.grid.min;
  194. }
  195. if (panel.grid.max) {
  196. panel.grid.leftMax = panel.grid.max;
  197. delete panel.grid.max;
  198. }
  199. }
  200. if (panel.y_format) {
  201. panel.y_formats[0] = panel.y_format;
  202. delete panel.y_format;
  203. }
  204. if (panel.y2_format) {
  205. panel.y_formats[1] = panel.y2_format;
  206. delete panel.y2_format;
  207. }
  208. });
  209. }
  210. // schema version 3 changes
  211. if (oldVersion < 3) {
  212. // ensure panel ids
  213. var maxId = this.getNextPanelId();
  214. panelUpgrades.push(function(panel) {
  215. if (!panel.id) {
  216. panel.id = maxId;
  217. maxId += 1;
  218. }
  219. });
  220. }
  221. // schema version 4 changes
  222. if (oldVersion < 4) {
  223. // move aliasYAxis changes
  224. panelUpgrades.push(function(panel) {
  225. if (panel.type !== 'graph') { return; }
  226. _.each(panel.aliasYAxis, function(value, key) {
  227. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  228. });
  229. delete panel.aliasYAxis;
  230. });
  231. }
  232. if (oldVersion < 6) {
  233. // move pulldowns to new schema
  234. var annotations = _.findWhere(old.pulldowns, { type: 'annotations' });
  235. if (annotations) {
  236. this.annotations = {
  237. list: annotations.annotations || [],
  238. };
  239. }
  240. // update template variables
  241. for (i = 0 ; i < this.templating.list.length; i++) {
  242. var variable = this.templating.list[i];
  243. if (variable.datasource === void 0) { variable.datasource = null; }
  244. if (variable.type === 'filter') { variable.type = 'query'; }
  245. if (variable.type === void 0) { variable.type = 'query'; }
  246. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  247. }
  248. }
  249. if (panelUpgrades.length === 0) {
  250. return;
  251. }
  252. for (i = 0; i < this.rows.length; i++) {
  253. var row = this.rows[i];
  254. for (j = 0; j < row.panels.length; j++) {
  255. for (k = 0; k < panelUpgrades.length; k++) {
  256. panelUpgrades[k](row.panels[j]);
  257. }
  258. }
  259. }
  260. };
  261. return {
  262. create: function(dashboard, meta) {
  263. return new DashboardModel(dashboard, meta);
  264. },
  265. setCurrent: function(dashboard) {
  266. this.currentDashboard = dashboard;
  267. },
  268. getCurrent: function() {
  269. return this.currentDashboard;
  270. },
  271. };
  272. });
  273. });