model.ts 17 KB

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