model.ts 18 KB

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