model.ts 17 KB

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