model.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. setPanelFocus(id) {
  170. this.meta.focusPanelId = id;
  171. }
  172. updateSubmenuVisibility() {
  173. this.meta.submenuEnabled = (() => {
  174. if (this.links.length > 0) { return true; }
  175. var visibleVars = _.filter(this.templating.list, variable => variable.hide !== 2);
  176. if (visibleVars.length > 0) { return true; }
  177. var visibleAnnotations = _.filter(this.annotations.list, annotation => annotation.hide !== true);
  178. if (visibleAnnotations.length > 0) { return true; }
  179. return false;
  180. })();
  181. }
  182. getPanelInfoById(panelId) {
  183. var result: any = {};
  184. _.each(this.rows, function(row) {
  185. _.each(row.panels, function(panel, index) {
  186. if (panel.id === panelId) {
  187. result.panel = panel;
  188. result.row = row;
  189. result.index = index;
  190. }
  191. });
  192. });
  193. if (!result.panel) {
  194. return null;
  195. }
  196. return result;
  197. }
  198. duplicatePanel(panel, row) {
  199. var newPanel = angular.copy(panel);
  200. newPanel.id = this.getNextPanelId();
  201. delete newPanel.repeat;
  202. delete newPanel.repeatIteration;
  203. delete newPanel.repeatPanelId;
  204. delete newPanel.scopedVars;
  205. delete newPanel.alert;
  206. row.addPanel(newPanel);
  207. return newPanel;
  208. }
  209. formatDate(date, format) {
  210. date = moment.isMoment(date) ? date : moment(date);
  211. format = format || 'YYYY-MM-DD HH:mm:ss';
  212. this.timezone = this.getTimezone();
  213. return this.timezone === 'browser' ?
  214. moment(date).format(format) :
  215. moment.utc(date).format(format);
  216. }
  217. destroy() {
  218. this.events.removeAllListeners();
  219. for (let row of this.rows) {
  220. row.destroy();
  221. }
  222. }
  223. cycleGraphTooltip() {
  224. this.graphTooltip = (this.graphTooltip + 1) % 3;
  225. }
  226. sharedTooltipModeEnabled() {
  227. return this.graphTooltip > 0;
  228. }
  229. sharedCrosshairModeOnly() {
  230. return this.graphTooltip === 1;
  231. }
  232. getRelativeTime(date) {
  233. date = moment.isMoment(date) ? date : moment(date);
  234. return this.timezone === 'browser' ?
  235. moment(date).fromNow() :
  236. moment.utc(date).fromNow();
  237. }
  238. getNextQueryLetter(panel) {
  239. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  240. return _.find(letters, function(refId) {
  241. return _.every(panel.targets, function(other) {
  242. return other.refId !== refId;
  243. });
  244. });
  245. }
  246. isTimezoneUtc() {
  247. return this.getTimezone() === 'utc';
  248. }
  249. getTimezone() {
  250. return this.timezone ? this.timezone : contextSrv.user.timezone;
  251. }
  252. private updateSchema(old) {
  253. var i, j, k;
  254. var oldVersion = this.schemaVersion;
  255. var panelUpgrades = [];
  256. this.schemaVersion = 14;
  257. if (oldVersion === this.schemaVersion) {
  258. return;
  259. }
  260. // version 2 schema changes
  261. if (oldVersion < 2) {
  262. if (old.services) {
  263. if (old.services.filter) {
  264. this.time = old.services.filter.time;
  265. this.templating.list = old.services.filter.list || [];
  266. }
  267. }
  268. panelUpgrades.push(function(panel) {
  269. // rename panel type
  270. if (panel.type === 'graphite') {
  271. panel.type = 'graph';
  272. }
  273. if (panel.type !== 'graph') {
  274. return;
  275. }
  276. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  277. if (panel.grid) {
  278. if (panel.grid.min) {
  279. panel.grid.leftMin = panel.grid.min;
  280. delete panel.grid.min;
  281. }
  282. if (panel.grid.max) {
  283. panel.grid.leftMax = panel.grid.max;
  284. delete panel.grid.max;
  285. }
  286. }
  287. if (panel.y_format) {
  288. panel.y_formats[0] = panel.y_format;
  289. delete panel.y_format;
  290. }
  291. if (panel.y2_format) {
  292. panel.y_formats[1] = panel.y2_format;
  293. delete panel.y2_format;
  294. }
  295. });
  296. }
  297. // schema version 3 changes
  298. if (oldVersion < 3) {
  299. // ensure panel ids
  300. var maxId = this.getNextPanelId();
  301. panelUpgrades.push(function(panel) {
  302. if (!panel.id) {
  303. panel.id = maxId;
  304. maxId += 1;
  305. }
  306. });
  307. }
  308. // schema version 4 changes
  309. if (oldVersion < 4) {
  310. // move aliasYAxis changes
  311. panelUpgrades.push(function(panel) {
  312. if (panel.type !== 'graph') { return; }
  313. _.each(panel.aliasYAxis, function(value, key) {
  314. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  315. });
  316. delete panel.aliasYAxis;
  317. });
  318. }
  319. if (oldVersion < 6) {
  320. // move pulldowns to new schema
  321. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  322. if (annotations) {
  323. this.annotations = {
  324. list: annotations.annotations || [],
  325. };
  326. }
  327. // update template variables
  328. for (i = 0 ; i < this.templating.list.length; i++) {
  329. var variable = this.templating.list[i];
  330. if (variable.datasource === void 0) { variable.datasource = null; }
  331. if (variable.type === 'filter') { variable.type = 'query'; }
  332. if (variable.type === void 0) { variable.type = 'query'; }
  333. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  334. }
  335. }
  336. if (oldVersion < 7) {
  337. if (old.nav && old.nav.length) {
  338. this.timepicker = old.nav[0];
  339. }
  340. // ensure query refIds
  341. panelUpgrades.push(function(panel) {
  342. _.each(panel.targets, function(target) {
  343. if (!target.refId) {
  344. target.refId = this.getNextQueryLetter(panel);
  345. }
  346. }.bind(this));
  347. });
  348. }
  349. if (oldVersion < 8) {
  350. panelUpgrades.push(function(panel) {
  351. _.each(panel.targets, function(target) {
  352. // update old influxdb query schema
  353. if (target.fields && target.tags && target.groupBy) {
  354. if (target.rawQuery) {
  355. delete target.fields;
  356. delete target.fill;
  357. } else {
  358. target.select = _.map(target.fields, function(field) {
  359. var parts = [];
  360. parts.push({type: 'field', params: [field.name]});
  361. parts.push({type: field.func, params: []});
  362. if (field.mathExpr) {
  363. parts.push({type: 'math', params: [field.mathExpr]});
  364. }
  365. if (field.asExpr) {
  366. parts.push({type: 'alias', params: [field.asExpr]});
  367. }
  368. return parts;
  369. });
  370. delete target.fields;
  371. _.each(target.groupBy, function(part) {
  372. if (part.type === 'time' && part.interval) {
  373. part.params = [part.interval];
  374. delete part.interval;
  375. }
  376. if (part.type === 'tag' && part.key) {
  377. part.params = [part.key];
  378. delete part.key;
  379. }
  380. });
  381. if (target.fill) {
  382. target.groupBy.push({type: 'fill', params: [target.fill]});
  383. delete target.fill;
  384. }
  385. }
  386. }
  387. });
  388. });
  389. }
  390. // schema version 9 changes
  391. if (oldVersion < 9) {
  392. // move aliasYAxis changes
  393. panelUpgrades.push(function(panel) {
  394. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  395. if (panel.thresholds) {
  396. var k = panel.thresholds.split(",");
  397. if (k.length >= 3) {
  398. k.shift();
  399. panel.thresholds = k.join(",");
  400. }
  401. }
  402. });
  403. }
  404. // schema version 10 changes
  405. if (oldVersion < 10) {
  406. // move aliasYAxis changes
  407. panelUpgrades.push(function(panel) {
  408. if (panel.type !== 'table') { return; }
  409. _.each(panel.styles, function(style) {
  410. if (style.thresholds && style.thresholds.length >= 3) {
  411. var k = style.thresholds;
  412. k.shift();
  413. style.thresholds = k;
  414. }
  415. });
  416. });
  417. }
  418. if (oldVersion < 12) {
  419. // update template variables
  420. _.each(this.templating.list, function(templateVariable) {
  421. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  422. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  423. if (templateVariable.hideVariable) {
  424. templateVariable.hide = 2;
  425. } else if (templateVariable.hideLabel) {
  426. templateVariable.hide = 1;
  427. }
  428. });
  429. }
  430. if (oldVersion < 12) {
  431. // update graph yaxes changes
  432. panelUpgrades.push(function(panel) {
  433. if (panel.type !== 'graph') { return; }
  434. if (!panel.grid) { return; }
  435. if (!panel.yaxes) {
  436. panel.yaxes = [
  437. {
  438. show: panel['y-axis'],
  439. min: panel.grid.leftMin,
  440. max: panel.grid.leftMax,
  441. logBase: panel.grid.leftLogBase,
  442. format: panel.y_formats[0],
  443. label: panel.leftYAxisLabel,
  444. },
  445. {
  446. show: panel['y-axis'],
  447. min: panel.grid.rightMin,
  448. max: panel.grid.rightMax,
  449. logBase: panel.grid.rightLogBase,
  450. format: panel.y_formats[1],
  451. label: panel.rightYAxisLabel,
  452. }
  453. ];
  454. panel.xaxis = {
  455. show: panel['x-axis'],
  456. };
  457. delete panel.grid.leftMin;
  458. delete panel.grid.leftMax;
  459. delete panel.grid.leftLogBase;
  460. delete panel.grid.rightMin;
  461. delete panel.grid.rightMax;
  462. delete panel.grid.rightLogBase;
  463. delete panel.y_formats;
  464. delete panel.leftYAxisLabel;
  465. delete panel.rightYAxisLabel;
  466. delete panel['y-axis'];
  467. delete panel['x-axis'];
  468. }
  469. });
  470. }
  471. if (oldVersion < 13) {
  472. // update graph yaxes changes
  473. panelUpgrades.push(function(panel) {
  474. if (panel.type !== 'graph') { return; }
  475. if (!panel.grid) { return; }
  476. panel.thresholds = [];
  477. var t1: any = {}, t2: any = {};
  478. if (panel.grid.threshold1 !== null) {
  479. t1.value = panel.grid.threshold1;
  480. if (panel.grid.thresholdLine) {
  481. t1.line = true;
  482. t1.lineColor = panel.grid.threshold1Color;
  483. t1.colorMode = 'custom';
  484. } else {
  485. t1.fill = true;
  486. t1.fillColor = panel.grid.threshold1Color;
  487. t1.colorMode = 'custom';
  488. }
  489. }
  490. if (panel.grid.threshold2 !== null) {
  491. t2.value = panel.grid.threshold2;
  492. if (panel.grid.thresholdLine) {
  493. t2.line = true;
  494. t2.lineColor = panel.grid.threshold2Color;
  495. t2.colorMode = 'custom';
  496. } else {
  497. t2.fill = true;
  498. t2.fillColor = panel.grid.threshold2Color;
  499. t2.colorMode = 'custom';
  500. }
  501. }
  502. if (_.isNumber(t1.value)) {
  503. if (_.isNumber(t2.value)) {
  504. if (t1.value > t2.value) {
  505. t1.op = t2.op = 'lt';
  506. panel.thresholds.push(t1);
  507. panel.thresholds.push(t2);
  508. } else {
  509. t1.op = t2.op = 'gt';
  510. panel.thresholds.push(t1);
  511. panel.thresholds.push(t2);
  512. }
  513. } else {
  514. t1.op = 'gt';
  515. panel.thresholds.push(t1);
  516. }
  517. }
  518. delete panel.grid.threshold1;
  519. delete panel.grid.threshold1Color;
  520. delete panel.grid.threshold2;
  521. delete panel.grid.threshold2Color;
  522. delete panel.grid.thresholdLine;
  523. });
  524. }
  525. if (oldVersion < 14) {
  526. this.graphTooltip = old.sharedCrosshair ? 1 : 0;
  527. }
  528. if (panelUpgrades.length === 0) {
  529. return;
  530. }
  531. for (i = 0; i < this.rows.length; i++) {
  532. var row = this.rows[i];
  533. for (j = 0; j < row.panels.length; j++) {
  534. for (k = 0; k < panelUpgrades.length; k++) {
  535. panelUpgrades[k].call(this, row.panels[j]);
  536. }
  537. }
  538. }
  539. }
  540. }