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