model.ts 17 KB

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