model.ts 17 KB

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