model.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import _ from 'lodash';
  6. import $ from 'jquery';
  7. import {Emitter, contextSrv, appEvents} from 'app/core/core';
  8. import {DashboardRow} from './row/row_model';
  9. export class DashboardModel {
  10. id: any;
  11. title: any;
  12. autoUpdate: any;
  13. description: any;
  14. tags: any;
  15. style: any;
  16. timezone: any;
  17. editable: any;
  18. graphTooltip: any;
  19. rows: DashboardRow[];
  20. time: any;
  21. timepicker: any;
  22. hideControls: any;
  23. templating: any;
  24. annotations: any;
  25. refresh: any;
  26. snapshot: any;
  27. schemaVersion: number;
  28. version: number;
  29. revision: number;
  30. links: any;
  31. gnetId: any;
  32. meta: any;
  33. events: any;
  34. editMode: boolean;
  35. constructor(data, meta) {
  36. if (!data) {
  37. data = {};
  38. }
  39. this.events = new Emitter();
  40. this.id = data.id || null;
  41. this.revision = data.revision;
  42. this.title = data.title || 'No Title';
  43. this.autoUpdate = data.autoUpdate;
  44. this.description = data.description;
  45. this.tags = data.tags || [];
  46. this.style = data.style || "dark";
  47. this.timezone = data.timezone || '';
  48. this.editable = data.editable !== false;
  49. this.graphTooltip = data.graphTooltip || false;
  50. this.hideControls = data.hideControls || false;
  51. this.time = data.time || { from: 'now-6h', to: 'now' };
  52. this.timepicker = data.timepicker || {};
  53. this.templating = this.ensureListExist(data.templating);
  54. this.annotations = this.ensureListExist(data.annotations);
  55. this.refresh = data.refresh;
  56. this.snapshot = data.snapshot;
  57. this.schemaVersion = data.schemaVersion || 0;
  58. this.version = data.version || 0;
  59. this.links = data.links || [];
  60. this.gnetId = data.gnetId || null;
  61. this.rows = [];
  62. if (data.rows) {
  63. for (let row of data.rows) {
  64. this.rows.push(new DashboardRow(row));
  65. }
  66. }
  67. this.updateSchema(data);
  68. this.initMeta(meta);
  69. }
  70. private initMeta(meta) {
  71. meta = meta || {};
  72. meta.canShare = meta.canShare !== false;
  73. meta.canSave = meta.canSave !== false;
  74. meta.canStar = meta.canStar !== false;
  75. meta.canEdit = meta.canEdit !== false;
  76. if (!this.editable) {
  77. meta.canEdit = false;
  78. meta.canDelete = false;
  79. meta.canSave = false;
  80. }
  81. this.meta = meta;
  82. }
  83. // cleans meta data and other non peristent state
  84. getSaveModelClone() {
  85. // temp remove stuff
  86. var events = this.events;
  87. var meta = this.meta;
  88. var rows = this.rows;
  89. var variables = this.templating.list;
  90. delete this.events;
  91. delete this.meta;
  92. // prepare save model
  93. this.rows = _.map(rows, row => row.getSaveModel());
  94. this.templating.list = _.map(variables, variable => variable.getSaveModel ? variable.getSaveModel() : variable);
  95. var copy = $.extend(true, {}, this);
  96. // restore properties
  97. this.events = events;
  98. this.meta = meta;
  99. this.rows = rows;
  100. this.templating.list = variables;
  101. return copy;
  102. }
  103. addEmptyRow() {
  104. this.rows.push(new DashboardRow({isNew: true}));
  105. }
  106. private ensureListExist(data) {
  107. if (!data) { data = {}; }
  108. if (!data.list) { data.list = []; }
  109. return data;
  110. }
  111. getNextPanelId() {
  112. var i, j, row, panel, max = 0;
  113. for (i = 0; i < this.rows.length; i++) {
  114. row = this.rows[i];
  115. for (j = 0; j < row.panels.length; j++) {
  116. panel = row.panels[j];
  117. if (panel.id > max) { max = panel.id; }
  118. }
  119. }
  120. return max + 1;
  121. }
  122. forEachPanel(callback) {
  123. var i, j, row;
  124. for (i = 0; i < this.rows.length; i++) {
  125. row = this.rows[i];
  126. for (j = 0; j < row.panels.length; j++) {
  127. callback(row.panels[j], j, row, i);
  128. }
  129. }
  130. }
  131. getPanelById(id) {
  132. for (var i = 0; i < this.rows.length; i++) {
  133. var row = this.rows[i];
  134. for (var j = 0; j < row.panels.length; j++) {
  135. var panel = row.panels[j];
  136. if (panel.id === id) {
  137. return panel;
  138. }
  139. }
  140. }
  141. return null;
  142. }
  143. addPanel(panel, row) {
  144. panel.id = this.getNextPanelId();
  145. row.addPanel(panel);
  146. }
  147. removeRow(row, force?) {
  148. var index = _.indexOf(this.rows, row);
  149. if (!row.panels.length || force) {
  150. this.rows.splice(index, 1);
  151. row.destroy();
  152. return;
  153. }
  154. appEvents.emit('confirm-modal', {
  155. title: 'Remove Row',
  156. text: 'Are you sure you want to remove this row?',
  157. icon: 'fa-trash',
  158. yesText: 'Delete',
  159. onConfirm: () => {
  160. this.rows.splice(index, 1);
  161. row.destroy();
  162. }
  163. });
  164. }
  165. toggleEditMode() {
  166. if (!this.meta.canEdit) {
  167. console.log('Not allowed to edit dashboard');
  168. return;
  169. }
  170. this.editMode = !this.editMode;
  171. this.updateSubmenuVisibility();
  172. this.events.emit('edit-mode-changed', this.editMode);
  173. }
  174. setPanelFocus(id) {
  175. this.meta.focusPanelId = id;
  176. }
  177. updateSubmenuVisibility() {
  178. if (this.editMode) {
  179. this.meta.submenuEnabled = true;
  180. return;
  181. }
  182. var visibleVars = _.filter(this.templating.list, function(template) {
  183. return template.hide !== 2;
  184. });
  185. this.meta.submenuEnabled = visibleVars.length > 0 || this.annotations.list.length > 0 || this.links.length > 0;
  186. }
  187. getPanelInfoById(panelId) {
  188. var result: any = {};
  189. _.each(this.rows, function(row) {
  190. _.each(row.panels, function(panel, index) {
  191. if (panel.id === panelId) {
  192. result.panel = panel;
  193. result.row = row;
  194. result.index = index;
  195. }
  196. });
  197. });
  198. if (!result.panel) {
  199. return null;
  200. }
  201. return result;
  202. }
  203. duplicatePanel(panel, row) {
  204. var newPanel = angular.copy(panel);
  205. newPanel.id = this.getNextPanelId();
  206. delete newPanel.repeat;
  207. delete newPanel.repeatIteration;
  208. delete newPanel.repeatPanelId;
  209. delete newPanel.scopedVars;
  210. delete newPanel.alert;
  211. row.addPanel(newPanel);
  212. return newPanel;
  213. }
  214. formatDate(date, format) {
  215. date = moment.isMoment(date) ? date : moment(date);
  216. format = format || 'YYYY-MM-DD HH:mm:ss';
  217. this.timezone = this.getTimezone();
  218. return this.timezone === 'browser' ?
  219. moment(date).format(format) :
  220. moment.utc(date).format(format);
  221. }
  222. destroy() {
  223. this.events.removeAllListeners();
  224. for (let row of this.rows) {
  225. row.destroy();
  226. }
  227. }
  228. cycleGraphTooltip() {
  229. this.graphTooltip = (this.graphTooltip + 1) % 3;
  230. }
  231. sharedTooltipModeEnabled() {
  232. return this.graphTooltip !== 0;
  233. }
  234. sharedCrosshairModeOnly() {
  235. return this.graphTooltip === 1;
  236. }
  237. getRelativeTime(date) {
  238. date = moment.isMoment(date) ? date : moment(date);
  239. return this.timezone === 'browser' ?
  240. moment(date).fromNow() :
  241. moment.utc(date).fromNow();
  242. }
  243. getNextQueryLetter(panel) {
  244. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  245. return _.find(letters, function(refId) {
  246. return _.every(panel.targets, function(other) {
  247. return other.refId !== refId;
  248. });
  249. });
  250. }
  251. isTimezoneUtc() {
  252. return this.getTimezone() === 'utc';
  253. }
  254. getTimezone() {
  255. return this.timezone ? this.timezone : contextSrv.user.timezone;
  256. }
  257. private updateSchema(old) {
  258. var i, j, k;
  259. var oldVersion = this.schemaVersion;
  260. var panelUpgrades = [];
  261. this.schemaVersion = 14;
  262. if (oldVersion === this.schemaVersion) {
  263. return;
  264. }
  265. // version 2 schema changes
  266. if (oldVersion < 2) {
  267. if (old.services) {
  268. if (old.services.filter) {
  269. this.time = old.services.filter.time;
  270. this.templating.list = old.services.filter.list || [];
  271. }
  272. }
  273. panelUpgrades.push(function(panel) {
  274. // rename panel type
  275. if (panel.type === 'graphite') {
  276. panel.type = 'graph';
  277. }
  278. if (panel.type !== 'graph') {
  279. return;
  280. }
  281. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  282. if (panel.grid) {
  283. if (panel.grid.min) {
  284. panel.grid.leftMin = panel.grid.min;
  285. delete panel.grid.min;
  286. }
  287. if (panel.grid.max) {
  288. panel.grid.leftMax = panel.grid.max;
  289. delete panel.grid.max;
  290. }
  291. }
  292. if (panel.y_format) {
  293. panel.y_formats[0] = panel.y_format;
  294. delete panel.y_format;
  295. }
  296. if (panel.y2_format) {
  297. panel.y_formats[1] = panel.y2_format;
  298. delete panel.y2_format;
  299. }
  300. });
  301. }
  302. // schema version 3 changes
  303. if (oldVersion < 3) {
  304. // ensure panel ids
  305. var maxId = this.getNextPanelId();
  306. panelUpgrades.push(function(panel) {
  307. if (!panel.id) {
  308. panel.id = maxId;
  309. maxId += 1;
  310. }
  311. });
  312. }
  313. // schema version 4 changes
  314. if (oldVersion < 4) {
  315. // move aliasYAxis changes
  316. panelUpgrades.push(function(panel) {
  317. if (panel.type !== 'graph') { return; }
  318. _.each(panel.aliasYAxis, function(value, key) {
  319. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  320. });
  321. delete panel.aliasYAxis;
  322. });
  323. }
  324. if (oldVersion < 6) {
  325. // move pulldowns to new schema
  326. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  327. if (annotations) {
  328. this.annotations = {
  329. list: annotations.annotations || [],
  330. };
  331. }
  332. // update template variables
  333. for (i = 0 ; i < this.templating.list.length; i++) {
  334. var variable = this.templating.list[i];
  335. if (variable.datasource === void 0) { variable.datasource = null; }
  336. if (variable.type === 'filter') { variable.type = 'query'; }
  337. if (variable.type === void 0) { variable.type = 'query'; }
  338. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  339. }
  340. }
  341. if (oldVersion < 7) {
  342. if (old.nav && old.nav.length) {
  343. this.timepicker = old.nav[0];
  344. }
  345. // ensure query refIds
  346. panelUpgrades.push(function(panel) {
  347. _.each(panel.targets, function(target) {
  348. if (!target.refId) {
  349. target.refId = this.getNextQueryLetter(panel);
  350. }
  351. }.bind(this));
  352. });
  353. }
  354. if (oldVersion < 8) {
  355. panelUpgrades.push(function(panel) {
  356. _.each(panel.targets, function(target) {
  357. // update old influxdb query schema
  358. if (target.fields && target.tags && target.groupBy) {
  359. if (target.rawQuery) {
  360. delete target.fields;
  361. delete target.fill;
  362. } else {
  363. target.select = _.map(target.fields, function(field) {
  364. var parts = [];
  365. parts.push({type: 'field', params: [field.name]});
  366. parts.push({type: field.func, params: []});
  367. if (field.mathExpr) {
  368. parts.push({type: 'math', params: [field.mathExpr]});
  369. }
  370. if (field.asExpr) {
  371. parts.push({type: 'alias', params: [field.asExpr]});
  372. }
  373. return parts;
  374. });
  375. delete target.fields;
  376. _.each(target.groupBy, function(part) {
  377. if (part.type === 'time' && part.interval) {
  378. part.params = [part.interval];
  379. delete part.interval;
  380. }
  381. if (part.type === 'tag' && part.key) {
  382. part.params = [part.key];
  383. delete part.key;
  384. }
  385. });
  386. if (target.fill) {
  387. target.groupBy.push({type: 'fill', params: [target.fill]});
  388. delete target.fill;
  389. }
  390. }
  391. }
  392. });
  393. });
  394. }
  395. // schema version 9 changes
  396. if (oldVersion < 9) {
  397. // move aliasYAxis changes
  398. panelUpgrades.push(function(panel) {
  399. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  400. if (panel.thresholds) {
  401. var k = panel.thresholds.split(",");
  402. if (k.length >= 3) {
  403. k.shift();
  404. panel.thresholds = k.join(",");
  405. }
  406. }
  407. });
  408. }
  409. // schema version 10 changes
  410. if (oldVersion < 10) {
  411. // move aliasYAxis changes
  412. panelUpgrades.push(function(panel) {
  413. if (panel.type !== 'table') { return; }
  414. _.each(panel.styles, function(style) {
  415. if (style.thresholds && style.thresholds.length >= 3) {
  416. var k = style.thresholds;
  417. k.shift();
  418. style.thresholds = k;
  419. }
  420. });
  421. });
  422. }
  423. if (oldVersion < 12) {
  424. // update template variables
  425. _.each(this.templating.list, function(templateVariable) {
  426. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  427. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  428. if (templateVariable.hideVariable) {
  429. templateVariable.hide = 2;
  430. } else if (templateVariable.hideLabel) {
  431. templateVariable.hide = 1;
  432. }
  433. });
  434. }
  435. if (oldVersion < 12) {
  436. // update graph yaxes changes
  437. panelUpgrades.push(function(panel) {
  438. if (panel.type !== 'graph') { return; }
  439. if (!panel.grid) { return; }
  440. if (!panel.yaxes) {
  441. panel.yaxes = [
  442. {
  443. show: panel['y-axis'],
  444. min: panel.grid.leftMin,
  445. max: panel.grid.leftMax,
  446. logBase: panel.grid.leftLogBase,
  447. format: panel.y_formats[0],
  448. label: panel.leftYAxisLabel,
  449. },
  450. {
  451. show: panel['y-axis'],
  452. min: panel.grid.rightMin,
  453. max: panel.grid.rightMax,
  454. logBase: panel.grid.rightLogBase,
  455. format: panel.y_formats[1],
  456. label: panel.rightYAxisLabel,
  457. }
  458. ];
  459. panel.xaxis = {
  460. show: panel['x-axis'],
  461. };
  462. delete panel.grid.leftMin;
  463. delete panel.grid.leftMax;
  464. delete panel.grid.leftLogBase;
  465. delete panel.grid.rightMin;
  466. delete panel.grid.rightMax;
  467. delete panel.grid.rightLogBase;
  468. delete panel.y_formats;
  469. delete panel.leftYAxisLabel;
  470. delete panel.rightYAxisLabel;
  471. delete panel['y-axis'];
  472. delete panel['x-axis'];
  473. }
  474. });
  475. }
  476. if (oldVersion < 13) {
  477. // update graph yaxes changes
  478. panelUpgrades.push(function(panel) {
  479. if (panel.type !== 'graph') { return; }
  480. if (!panel.grid) { return; }
  481. panel.thresholds = [];
  482. var t1: any = {}, t2: any = {};
  483. if (panel.grid.threshold1 !== null) {
  484. t1.value = panel.grid.threshold1;
  485. if (panel.grid.thresholdLine) {
  486. t1.line = true;
  487. t1.lineColor = panel.grid.threshold1Color;
  488. t1.colorMode = 'custom';
  489. } else {
  490. t1.fill = true;
  491. t1.fillColor = panel.grid.threshold1Color;
  492. t1.colorMode = 'custom';
  493. }
  494. }
  495. if (panel.grid.threshold2 !== null) {
  496. t2.value = panel.grid.threshold2;
  497. if (panel.grid.thresholdLine) {
  498. t2.line = true;
  499. t2.lineColor = panel.grid.threshold2Color;
  500. t2.colorMode = 'custom';
  501. } else {
  502. t2.fill = true;
  503. t2.fillColor = panel.grid.threshold2Color;
  504. t2.colorMode = 'custom';
  505. }
  506. }
  507. if (_.isNumber(t1.value)) {
  508. if (_.isNumber(t2.value)) {
  509. if (t1.value > t2.value) {
  510. t1.op = t2.op = 'lt';
  511. panel.thresholds.push(t1);
  512. panel.thresholds.push(t2);
  513. } else {
  514. t1.op = t2.op = 'gt';
  515. panel.thresholds.push(t1);
  516. panel.thresholds.push(t2);
  517. }
  518. } else {
  519. t1.op = 'gt';
  520. panel.thresholds.push(t1);
  521. }
  522. }
  523. delete panel.grid.threshold1;
  524. delete panel.grid.threshold1Color;
  525. delete panel.grid.threshold2;
  526. delete panel.grid.threshold2Color;
  527. delete panel.grid.thresholdLine;
  528. });
  529. }
  530. if (oldVersion < 14) {
  531. this.graphTooltip = old.sharedCrosshair ? 1 : 0;
  532. }
  533. if (panelUpgrades.length === 0) {
  534. return;
  535. }
  536. for (i = 0; i < this.rows.length; i++) {
  537. var row = this.rows[i];
  538. for (j = 0; j < row.panels.length; j++) {
  539. for (k = 0; k < panelUpgrades.length; k++) {
  540. panelUpgrades[k].call(this, row.panels[j]);
  541. }
  542. }
  543. }
  544. }
  545. }