dashboardSrv.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'lodash',
  5. 'moment',
  6. ],
  7. function (angular, $, _, moment) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.factory('dashboardSrv', function(contextSrv) {
  11. function DashboardModel (data, meta) {
  12. if (!data) {
  13. data = {};
  14. }
  15. this.id = data.id || null;
  16. this.title = data.title || 'No Title';
  17. this.autoUpdate = data.autoUpdate;
  18. this.description = data.description;
  19. this.tags = data.tags || [];
  20. this.style = data.style || "dark";
  21. this.timezone = data.timezone || '';
  22. this.editable = data.editable !== false;
  23. this.hideControls = data.hideControls || false;
  24. this.sharedCrosshair = data.sharedCrosshair || false;
  25. this.rows = data.rows || [];
  26. this.time = data.time || { from: 'now-6h', to: 'now' };
  27. this.timepicker = data.timepicker || {};
  28. this.templating = this._ensureListExist(data.templating);
  29. this.annotations = this._ensureListExist(data.annotations);
  30. this.refresh = data.refresh;
  31. this.snapshot = data.snapshot;
  32. this.schemaVersion = data.schemaVersion || 0;
  33. this.version = data.version || 0;
  34. this.links = data.links || [];
  35. this.gnetId = data.gnetId || null;
  36. this._updateSchema(data);
  37. this._initMeta(meta);
  38. }
  39. var p = DashboardModel.prototype;
  40. p._initMeta = function(meta) {
  41. meta = meta || {};
  42. meta.canShare = meta.canShare !== false;
  43. meta.canSave = meta.canSave !== false;
  44. meta.canStar = meta.canStar !== false;
  45. meta.canEdit = meta.canEdit !== false;
  46. if (!this.editable) {
  47. meta.canEdit = false;
  48. meta.canDelete = false;
  49. meta.canSave = false;
  50. this.hideControls = true;
  51. }
  52. this.meta = meta;
  53. };
  54. // cleans meta data and other non peristent state
  55. p.getSaveModelClone = function() {
  56. var copy = $.extend(true, {}, this);
  57. delete copy.meta;
  58. return copy;
  59. };
  60. p._ensureListExist = function (data) {
  61. if (!data) { data = {}; }
  62. if (!data.list) { data.list = []; }
  63. return data;
  64. };
  65. p.getNextPanelId = function() {
  66. var i, j, row, panel, max = 0;
  67. for (i = 0; i < this.rows.length; i++) {
  68. row = this.rows[i];
  69. for (j = 0; j < row.panels.length; j++) {
  70. panel = row.panels[j];
  71. if (panel.id > max) { max = panel.id; }
  72. }
  73. }
  74. return max + 1;
  75. };
  76. p.forEachPanel = function(callback) {
  77. var i, j, row;
  78. for (i = 0; i < this.rows.length; i++) {
  79. row = this.rows[i];
  80. for (j = 0; j < row.panels.length; j++) {
  81. callback(row.panels[j], j, row, i);
  82. }
  83. }
  84. };
  85. p.getPanelById = function(id) {
  86. for (var i = 0; i < this.rows.length; i++) {
  87. var row = this.rows[i];
  88. for (var j = 0; j < row.panels.length; j++) {
  89. var panel = row.panels[j];
  90. if (panel.id === id) {
  91. return panel;
  92. }
  93. }
  94. }
  95. return null;
  96. };
  97. p.rowSpan = function(row) {
  98. return _.reduce(row.panels, function(p,v) {
  99. return p + v.span;
  100. },0);
  101. };
  102. p.addPanel = function(panel, row) {
  103. var rowSpan = this.rowSpan(row);
  104. var panelCount = row.panels.length;
  105. var space = (12 - rowSpan) - panel.span;
  106. panel.id = this.getNextPanelId();
  107. // try to make room of there is no space left
  108. if (space <= 0) {
  109. if (panelCount === 1) {
  110. row.panels[0].span = 6;
  111. panel.span = 6;
  112. }
  113. else if (panelCount === 2) {
  114. row.panels[0].span = 4;
  115. row.panels[1].span = 4;
  116. panel.span = 4;
  117. }
  118. }
  119. row.panels.push(panel);
  120. };
  121. p.isSubmenuFeaturesEnabled = function() {
  122. var visableTemplates = _.filter(this.templating.list, function(template) {
  123. return template.hideVariable === undefined || template.hideVariable === false;
  124. });
  125. return visableTemplates.length > 0 || this.annotations.list.length > 0 || this.links.length > 0;
  126. };
  127. p.getPanelInfoById = function(panelId) {
  128. var result = {};
  129. _.each(this.rows, function(row) {
  130. _.each(row.panels, function(panel, index) {
  131. if (panel.id === panelId) {
  132. result.panel = panel;
  133. result.row = row;
  134. result.index = index;
  135. }
  136. });
  137. });
  138. if (!result.panel) {
  139. return null;
  140. }
  141. return result;
  142. };
  143. p.duplicatePanel = function(panel, row) {
  144. var rowIndex = _.indexOf(this.rows, row);
  145. var newPanel = angular.copy(panel);
  146. newPanel.id = this.getNextPanelId();
  147. delete newPanel.repeat;
  148. delete newPanel.repeatIteration;
  149. delete newPanel.repeatPanelId;
  150. delete newPanel.scopedVars;
  151. var currentRow = this.rows[rowIndex];
  152. currentRow.panels.push(newPanel);
  153. return newPanel;
  154. };
  155. p.formatDate = function(date, format) {
  156. date = moment.isMoment(date) ? date : moment(date);
  157. format = format || 'YYYY-MM-DD HH:mm:ss';
  158. this.timezone = this.getTimezone();
  159. return this.timezone === 'browser' ?
  160. moment(date).format(format) :
  161. moment.utc(date).format(format);
  162. };
  163. p.getRelativeTime = function(date) {
  164. date = moment.isMoment(date) ? date : moment(date);
  165. return this.timezone === 'browser' ?
  166. moment(date).fromNow() :
  167. moment.utc(date).fromNow();
  168. };
  169. p.getNextQueryLetter = function(panel) {
  170. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  171. return _.find(letters, function(refId) {
  172. return _.every(panel.targets, function(other) {
  173. return other.refId !== refId;
  174. });
  175. });
  176. };
  177. p.isTimezoneUtc = function() {
  178. return this.getTimezone() === 'utc';
  179. };
  180. p.getTimezone = function() {
  181. return this.timezone ? this.timezone : contextSrv.user.timezone;
  182. };
  183. p._updateSchema = function(old) {
  184. var i, j, k;
  185. var oldVersion = this.schemaVersion;
  186. var panelUpgrades = [];
  187. this.schemaVersion = 13;
  188. if (oldVersion === this.schemaVersion) {
  189. return;
  190. }
  191. // version 2 schema changes
  192. if (oldVersion < 2) {
  193. if (old.services) {
  194. if (old.services.filter) {
  195. this.time = old.services.filter.time;
  196. this.templating.list = old.services.filter.list || [];
  197. }
  198. delete this.services;
  199. }
  200. panelUpgrades.push(function(panel) {
  201. // rename panel type
  202. if (panel.type === 'graphite') {
  203. panel.type = 'graph';
  204. }
  205. if (panel.type !== 'graph') {
  206. return;
  207. }
  208. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  209. if (panel.grid) {
  210. if (panel.grid.min) {
  211. panel.grid.leftMin = panel.grid.min;
  212. delete panel.grid.min;
  213. }
  214. if (panel.grid.max) {
  215. panel.grid.leftMax = panel.grid.max;
  216. delete panel.grid.max;
  217. }
  218. }
  219. if (panel.y_format) {
  220. panel.y_formats[0] = panel.y_format;
  221. delete panel.y_format;
  222. }
  223. if (panel.y2_format) {
  224. panel.y_formats[1] = panel.y2_format;
  225. delete panel.y2_format;
  226. }
  227. });
  228. }
  229. // schema version 3 changes
  230. if (oldVersion < 3) {
  231. // ensure panel ids
  232. var maxId = this.getNextPanelId();
  233. panelUpgrades.push(function(panel) {
  234. if (!panel.id) {
  235. panel.id = maxId;
  236. maxId += 1;
  237. }
  238. });
  239. }
  240. // schema version 4 changes
  241. if (oldVersion < 4) {
  242. // move aliasYAxis changes
  243. panelUpgrades.push(function(panel) {
  244. if (panel.type !== 'graph') { return; }
  245. _.each(panel.aliasYAxis, function(value, key) {
  246. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  247. });
  248. delete panel.aliasYAxis;
  249. });
  250. }
  251. if (oldVersion < 6) {
  252. // move pulldowns to new schema
  253. var annotations = _.findWhere(old.pulldowns, { type: 'annotations' });
  254. if (annotations) {
  255. this.annotations = {
  256. list: annotations.annotations || [],
  257. };
  258. }
  259. // update template variables
  260. for (i = 0 ; i < this.templating.list.length; i++) {
  261. var variable = this.templating.list[i];
  262. if (variable.datasource === void 0) { variable.datasource = null; }
  263. if (variable.type === 'filter') { variable.type = 'query'; }
  264. if (variable.type === void 0) { variable.type = 'query'; }
  265. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  266. }
  267. }
  268. if (oldVersion < 7) {
  269. if (old.nav && old.nav.length) {
  270. this.timepicker = old.nav[0];
  271. delete this.nav;
  272. }
  273. // ensure query refIds
  274. panelUpgrades.push(function(panel) {
  275. _.each(panel.targets, function(target) {
  276. if (!target.refId) {
  277. target.refId = this.getNextQueryLetter(panel);
  278. }
  279. }, this);
  280. });
  281. }
  282. if (oldVersion < 8) {
  283. panelUpgrades.push(function(panel) {
  284. _.each(panel.targets, function(target) {
  285. // update old influxdb query schema
  286. if (target.fields && target.tags && target.groupBy) {
  287. if (target.rawQuery) {
  288. delete target.fields;
  289. delete target.fill;
  290. } else {
  291. target.select = _.map(target.fields, function(field) {
  292. var parts = [];
  293. parts.push({type: 'field', params: [field.name]});
  294. parts.push({type: field.func, params: []});
  295. if (field.mathExpr) {
  296. parts.push({type: 'math', params: [field.mathExpr]});
  297. }
  298. if (field.asExpr) {
  299. parts.push({type: 'alias', params: [field.asExpr]});
  300. }
  301. return parts;
  302. });
  303. delete target.fields;
  304. _.each(target.groupBy, function(part) {
  305. if (part.type === 'time' && part.interval) {
  306. part.params = [part.interval];
  307. delete part.interval;
  308. }
  309. if (part.type === 'tag' && part.key) {
  310. part.params = [part.key];
  311. delete part.key;
  312. }
  313. });
  314. if (target.fill) {
  315. target.groupBy.push({type: 'fill', params: [target.fill]});
  316. delete target.fill;
  317. }
  318. }
  319. }
  320. });
  321. });
  322. }
  323. // schema version 9 changes
  324. if (oldVersion < 9) {
  325. // move aliasYAxis changes
  326. panelUpgrades.push(function(panel) {
  327. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  328. if (panel.thresholds) {
  329. var k = panel.thresholds.split(",");
  330. if (k.length >= 3) {
  331. k.shift();
  332. panel.thresholds = k.join(",");
  333. }
  334. }
  335. });
  336. }
  337. // schema version 10 changes
  338. if (oldVersion < 10) {
  339. // move aliasYAxis changes
  340. panelUpgrades.push(function(panel) {
  341. if (panel.type !== 'table') { return; }
  342. _.each(panel.styles, function(style) {
  343. if (style.thresholds && style.thresholds.length >= 3) {
  344. var k = style.thresholds;
  345. k.shift();
  346. style.thresholds = k;
  347. }
  348. });
  349. });
  350. }
  351. if (oldVersion < 12) {
  352. // update template variables
  353. _.each(this.templating.list, function(templateVariable) {
  354. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  355. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  356. if (templateVariable.hideVariable) {
  357. templateVariable.hide = 2;
  358. } else if (templateVariable.hideLabel) {
  359. templateVariable.hide = 1;
  360. } else {
  361. templateVariable.hide = 0;
  362. }
  363. });
  364. }
  365. if (oldVersion < 12) {
  366. // update graph yaxes changes
  367. panelUpgrades.push(function(panel) {
  368. if (panel.type !== 'graph') { return; }
  369. if (!panel.grid) { return; }
  370. if (!panel.yaxes) {
  371. panel.yaxes = [
  372. {
  373. show: panel['y-axis'],
  374. min: panel.grid.leftMin,
  375. max: panel.grid.leftMax,
  376. logBase: panel.grid.leftLogBase,
  377. format: panel.y_formats[0],
  378. label: panel.leftYAxisLabel,
  379. },
  380. {
  381. show: panel['y-axis'],
  382. min: panel.grid.rightMin,
  383. max: panel.grid.rightMax,
  384. logBase: panel.grid.rightLogBase,
  385. format: panel.y_formats[1],
  386. label: panel.rightYAxisLabel,
  387. }
  388. ];
  389. panel.xaxis = {
  390. show: panel['x-axis'],
  391. };
  392. delete panel.grid.leftMin;
  393. delete panel.grid.leftMax;
  394. delete panel.grid.leftLogBase;
  395. delete panel.grid.rightMin;
  396. delete panel.grid.rightMax;
  397. delete panel.grid.rightLogBase;
  398. delete panel.y_formats;
  399. delete panel.leftYAxisLabel;
  400. delete panel.rightYAxisLabel;
  401. delete panel['y-axis'];
  402. delete panel['x-axis'];
  403. }
  404. });
  405. }
  406. if (oldVersion < 13) {
  407. // update graph yaxes changes
  408. panelUpgrades.push(function(panel) {
  409. if (panel.type !== 'graph') { return; }
  410. panel.thresholds = [];
  411. var t1 = {}, t2 = {};
  412. if (panel.grid.threshold1 !== null) {
  413. t1.value = panel.grid.threshold1;
  414. if (panel.grid.thresholdLine) {
  415. t1.line = true;
  416. t1.lineColor = panel.grid.threshold1Color;
  417. } else {
  418. t1.fill = true;
  419. t1.fillColor = panel.grid.threshold1Color;
  420. }
  421. }
  422. if (panel.grid.threshold2 !== null) {
  423. t2.value = panel.grid.threshold2;
  424. if (panel.grid.thresholdLine) {
  425. t2.line = true;
  426. t2.lineColor = panel.grid.threshold2Color;
  427. } else {
  428. t2.fill = true;
  429. t2.fillColor = panel.grid.threshold2Color;
  430. }
  431. }
  432. if (_.isNumber(t1.value)) {
  433. if (_.isNumber(t2.value)) {
  434. if (t1.value > t2.value) {
  435. t1.op = t2.op = '<';
  436. panel.thresholds.push(t2);
  437. panel.thresholds.push(t1);
  438. } else {
  439. t1.op = t2.op = '>';
  440. panel.thresholds.push(t2);
  441. panel.thresholds.push(t1);
  442. }
  443. } else {
  444. t1.op = '>';
  445. panel.thresholds.push(t1);
  446. }
  447. }
  448. delete panel.grid.threshold1;
  449. delete panel.grid.threshold1Color;
  450. delete panel.grid.threshold2;
  451. delete panel.grid.threshold2Color;
  452. delete panel.grid.thresholdLine;
  453. });
  454. }
  455. if (panelUpgrades.length === 0) {
  456. return;
  457. }
  458. for (i = 0; i < this.rows.length; i++) {
  459. var row = this.rows[i];
  460. for (j = 0; j < row.panels.length; j++) {
  461. for (k = 0; k < panelUpgrades.length; k++) {
  462. panelUpgrades[k].call(this, row.panels[j]);
  463. }
  464. }
  465. }
  466. };
  467. return {
  468. create: function(dashboard, meta) {
  469. return new DashboardModel(dashboard, meta);
  470. },
  471. setCurrent: function(dashboard) {
  472. this.currentDashboard = dashboard;
  473. },
  474. getCurrent: function() {
  475. return this.currentDashboard;
  476. },
  477. };
  478. });
  479. });