DashboardModel.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. _.each(this.panels, function(panel, index) {
  218. if (panel.id === panelId) {
  219. result.panel = panel;
  220. result.index = index;
  221. }
  222. });
  223. if (!result.panel) {
  224. return null;
  225. }
  226. return result;
  227. }
  228. duplicatePanel(panel, row) {
  229. var newPanel = angular.copy(panel);
  230. newPanel.id = this.getNextPanelId();
  231. delete newPanel.repeat;
  232. delete newPanel.repeatIteration;
  233. delete newPanel.repeatPanelId;
  234. delete newPanel.scopedVars;
  235. if (newPanel.alert) {
  236. delete newPanel.thresholds;
  237. }
  238. delete newPanel.alert;
  239. row.addPanel(newPanel);
  240. return newPanel;
  241. }
  242. formatDate(date, format?) {
  243. date = moment.isMoment(date) ? date : moment(date);
  244. format = format || 'YYYY-MM-DD HH:mm:ss';
  245. let timezone = this.getTimezone();
  246. return timezone === 'browser' ?
  247. moment(date).format(format) :
  248. moment.utc(date).format(format);
  249. }
  250. destroy() {
  251. this.events.removeAllListeners();
  252. for (let row of this.rows) {
  253. row.destroy();
  254. }
  255. }
  256. on(eventName, callback) {
  257. this.events.on(eventName, callback);
  258. }
  259. off(eventName, callback?) {
  260. this.events.off(eventName, callback);
  261. }
  262. cycleGraphTooltip() {
  263. this.graphTooltip = (this.graphTooltip + 1) % 3;
  264. }
  265. sharedTooltipModeEnabled() {
  266. return this.graphTooltip > 0;
  267. }
  268. sharedCrosshairModeOnly() {
  269. return this.graphTooltip === 1;
  270. }
  271. getRelativeTime(date) {
  272. date = moment.isMoment(date) ? date : moment(date);
  273. return this.timezone === 'browser' ?
  274. moment(date).fromNow() :
  275. moment.utc(date).fromNow();
  276. }
  277. getNextQueryLetter(panel) {
  278. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  279. return _.find(letters, function(refId) {
  280. return _.every(panel.targets, function(other) {
  281. return other.refId !== refId;
  282. });
  283. });
  284. }
  285. isTimezoneUtc() {
  286. return this.getTimezone() === 'utc';
  287. }
  288. getTimezone() {
  289. return this.timezone ? this.timezone : contextSrv.user.timezone;
  290. }
  291. private updateSchema(old) {
  292. var i, j, k;
  293. var oldVersion = this.schemaVersion;
  294. var panelUpgrades = [];
  295. this.schemaVersion = 16;
  296. if (oldVersion === this.schemaVersion) {
  297. return;
  298. }
  299. // version 2 schema changes
  300. if (oldVersion < 2) {
  301. if (old.services) {
  302. if (old.services.filter) {
  303. this.time = old.services.filter.time;
  304. this.templating.list = old.services.filter.list || [];
  305. }
  306. }
  307. panelUpgrades.push(function(panel) {
  308. // rename panel type
  309. if (panel.type === 'graphite') {
  310. panel.type = 'graph';
  311. }
  312. if (panel.type !== 'graph') {
  313. return;
  314. }
  315. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  316. if (panel.grid) {
  317. if (panel.grid.min) {
  318. panel.grid.leftMin = panel.grid.min;
  319. delete panel.grid.min;
  320. }
  321. if (panel.grid.max) {
  322. panel.grid.leftMax = panel.grid.max;
  323. delete panel.grid.max;
  324. }
  325. }
  326. if (panel.y_format) {
  327. panel.y_formats[0] = panel.y_format;
  328. delete panel.y_format;
  329. }
  330. if (panel.y2_format) {
  331. panel.y_formats[1] = panel.y2_format;
  332. delete panel.y2_format;
  333. }
  334. });
  335. }
  336. // schema version 3 changes
  337. if (oldVersion < 3) {
  338. // ensure panel ids
  339. var maxId = this.getNextPanelId();
  340. panelUpgrades.push(function(panel) {
  341. if (!panel.id) {
  342. panel.id = maxId;
  343. maxId += 1;
  344. }
  345. });
  346. }
  347. // schema version 4 changes
  348. if (oldVersion < 4) {
  349. // move aliasYAxis changes
  350. panelUpgrades.push(function(panel) {
  351. if (panel.type !== 'graph') { return; }
  352. _.each(panel.aliasYAxis, function(value, key) {
  353. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  354. });
  355. delete panel.aliasYAxis;
  356. });
  357. }
  358. if (oldVersion < 6) {
  359. // move pulldowns to new schema
  360. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  361. if (annotations) {
  362. this.annotations = {
  363. list: annotations.annotations || [],
  364. };
  365. }
  366. // update template variables
  367. for (i = 0 ; i < this.templating.list.length; i++) {
  368. var variable = this.templating.list[i];
  369. if (variable.datasource === void 0) { variable.datasource = null; }
  370. if (variable.type === 'filter') { variable.type = 'query'; }
  371. if (variable.type === void 0) { variable.type = 'query'; }
  372. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  373. }
  374. }
  375. if (oldVersion < 7) {
  376. if (old.nav && old.nav.length) {
  377. this.timepicker = old.nav[0];
  378. }
  379. // ensure query refIds
  380. panelUpgrades.push(function(panel) {
  381. _.each(panel.targets, function(target) {
  382. if (!target.refId) {
  383. target.refId = this.getNextQueryLetter(panel);
  384. }
  385. }.bind(this));
  386. });
  387. }
  388. if (oldVersion < 8) {
  389. panelUpgrades.push(function(panel) {
  390. _.each(panel.targets, function(target) {
  391. // update old influxdb query schema
  392. if (target.fields && target.tags && target.groupBy) {
  393. if (target.rawQuery) {
  394. delete target.fields;
  395. delete target.fill;
  396. } else {
  397. target.select = _.map(target.fields, function(field) {
  398. var parts = [];
  399. parts.push({type: 'field', params: [field.name]});
  400. parts.push({type: field.func, params: []});
  401. if (field.mathExpr) {
  402. parts.push({type: 'math', params: [field.mathExpr]});
  403. }
  404. if (field.asExpr) {
  405. parts.push({type: 'alias', params: [field.asExpr]});
  406. }
  407. return parts;
  408. });
  409. delete target.fields;
  410. _.each(target.groupBy, function(part) {
  411. if (part.type === 'time' && part.interval) {
  412. part.params = [part.interval];
  413. delete part.interval;
  414. }
  415. if (part.type === 'tag' && part.key) {
  416. part.params = [part.key];
  417. delete part.key;
  418. }
  419. });
  420. if (target.fill) {
  421. target.groupBy.push({type: 'fill', params: [target.fill]});
  422. delete target.fill;
  423. }
  424. }
  425. }
  426. });
  427. });
  428. }
  429. // schema version 9 changes
  430. if (oldVersion < 9) {
  431. // move aliasYAxis changes
  432. panelUpgrades.push(function(panel) {
  433. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  434. if (panel.thresholds) {
  435. var k = panel.thresholds.split(",");
  436. if (k.length >= 3) {
  437. k.shift();
  438. panel.thresholds = k.join(",");
  439. }
  440. }
  441. });
  442. }
  443. // schema version 10 changes
  444. if (oldVersion < 10) {
  445. // move aliasYAxis changes
  446. panelUpgrades.push(function(panel) {
  447. if (panel.type !== 'table') { return; }
  448. _.each(panel.styles, function(style) {
  449. if (style.thresholds && style.thresholds.length >= 3) {
  450. var k = style.thresholds;
  451. k.shift();
  452. style.thresholds = k;
  453. }
  454. });
  455. });
  456. }
  457. if (oldVersion < 12) {
  458. // update template variables
  459. _.each(this.templating.list, function(templateVariable) {
  460. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  461. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  462. if (templateVariable.hideVariable) {
  463. templateVariable.hide = 2;
  464. } else if (templateVariable.hideLabel) {
  465. templateVariable.hide = 1;
  466. }
  467. });
  468. }
  469. if (oldVersion < 12) {
  470. // update graph yaxes changes
  471. panelUpgrades.push(function(panel) {
  472. if (panel.type !== 'graph') { return; }
  473. if (!panel.grid) { return; }
  474. if (!panel.yaxes) {
  475. panel.yaxes = [
  476. {
  477. show: panel['y-axis'],
  478. min: panel.grid.leftMin,
  479. max: panel.grid.leftMax,
  480. logBase: panel.grid.leftLogBase,
  481. format: panel.y_formats[0],
  482. label: panel.leftYAxisLabel,
  483. },
  484. {
  485. show: panel['y-axis'],
  486. min: panel.grid.rightMin,
  487. max: panel.grid.rightMax,
  488. logBase: panel.grid.rightLogBase,
  489. format: panel.y_formats[1],
  490. label: panel.rightYAxisLabel,
  491. }
  492. ];
  493. panel.xaxis = {
  494. show: panel['x-axis'],
  495. };
  496. delete panel.grid.leftMin;
  497. delete panel.grid.leftMax;
  498. delete panel.grid.leftLogBase;
  499. delete panel.grid.rightMin;
  500. delete panel.grid.rightMax;
  501. delete panel.grid.rightLogBase;
  502. delete panel.y_formats;
  503. delete panel.leftYAxisLabel;
  504. delete panel.rightYAxisLabel;
  505. delete panel['y-axis'];
  506. delete panel['x-axis'];
  507. }
  508. });
  509. }
  510. if (oldVersion < 13) {
  511. // update graph yaxes changes
  512. panelUpgrades.push(function(panel) {
  513. if (panel.type !== 'graph') { return; }
  514. if (!panel.grid) { return; }
  515. panel.thresholds = [];
  516. var t1: any = {}, t2: any = {};
  517. if (panel.grid.threshold1 !== null) {
  518. t1.value = panel.grid.threshold1;
  519. if (panel.grid.thresholdLine) {
  520. t1.line = true;
  521. t1.lineColor = panel.grid.threshold1Color;
  522. t1.colorMode = 'custom';
  523. } else {
  524. t1.fill = true;
  525. t1.fillColor = panel.grid.threshold1Color;
  526. t1.colorMode = 'custom';
  527. }
  528. }
  529. if (panel.grid.threshold2 !== null) {
  530. t2.value = panel.grid.threshold2;
  531. if (panel.grid.thresholdLine) {
  532. t2.line = true;
  533. t2.lineColor = panel.grid.threshold2Color;
  534. t2.colorMode = 'custom';
  535. } else {
  536. t2.fill = true;
  537. t2.fillColor = panel.grid.threshold2Color;
  538. t2.colorMode = 'custom';
  539. }
  540. }
  541. if (_.isNumber(t1.value)) {
  542. if (_.isNumber(t2.value)) {
  543. if (t1.value > t2.value) {
  544. t1.op = t2.op = 'lt';
  545. panel.thresholds.push(t1);
  546. panel.thresholds.push(t2);
  547. } else {
  548. t1.op = t2.op = 'gt';
  549. panel.thresholds.push(t1);
  550. panel.thresholds.push(t2);
  551. }
  552. } else {
  553. t1.op = 'gt';
  554. panel.thresholds.push(t1);
  555. }
  556. }
  557. delete panel.grid.threshold1;
  558. delete panel.grid.threshold1Color;
  559. delete panel.grid.threshold2;
  560. delete panel.grid.threshold2Color;
  561. delete panel.grid.thresholdLine;
  562. });
  563. }
  564. if (oldVersion < 14) {
  565. this.graphTooltip = old.sharedCrosshair ? 1 : 0;
  566. }
  567. if (oldVersion < 16) {
  568. this.upgradeToGridLayout(old);
  569. }
  570. if (panelUpgrades.length === 0) {
  571. return;
  572. }
  573. for (j = 0; j < this.panels.length; j++) {
  574. for (k = 0; k < panelUpgrades.length; k++) {
  575. panelUpgrades[k].call(this, this.panels[j]);
  576. }
  577. }
  578. }
  579. upgradeToGridLayout(old) {
  580. let yPos = 0;
  581. //let rowIds = 1000;
  582. //
  583. if (!old.rows) {
  584. return;
  585. }
  586. for (let row of old.rows) {
  587. let xPos = 0;
  588. let height: any = row.height || 250;
  589. // if (this.meta.keepRows) {
  590. // this.panels.push({
  591. // id: rowIds++,
  592. // type: 'row',
  593. // title: row.title,
  594. // x: 0,
  595. // y: yPos,
  596. // height: 1,
  597. // width: 12
  598. // });
  599. //
  600. // yPos += 1;
  601. // }
  602. if (_.isString(height)) {
  603. height = parseInt(height.replace('px', ''), 10);
  604. }
  605. const rowGridHeight = Math.ceil(height / CELL_HEIGHT);
  606. for (let panel of row.panels) {
  607. // should wrap to next row?
  608. if (xPos + panel.span >= 12) {
  609. yPos += rowGridHeight;
  610. }
  611. panel.gridPos = { x: xPos, y: yPos, w: panel.span, h: rowGridHeight };
  612. delete panel.span;
  613. xPos += panel.gridPos.w;
  614. this.panels.push(new PanelModel(panel));
  615. }
  616. yPos += rowGridHeight;
  617. }
  618. console.log('panels', this.panels);
  619. }
  620. }