model.ts 17 KB

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