model.ts 18 KB

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