model.ts 16 KB

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