model.ts 17 KB

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