DashboardModel.ts 19 KB

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