model.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 rowIndex = _.indexOf(this.rows, row);
  203. var newPanel = angular.copy(panel);
  204. newPanel.id = this.getNextPanelId();
  205. delete newPanel.repeat;
  206. delete newPanel.repeatIteration;
  207. delete newPanel.repeatPanelId;
  208. delete newPanel.scopedVars;
  209. var currentRow = this.rows[rowIndex];
  210. currentRow.panels.push(newPanel);
  211. return newPanel;
  212. }
  213. formatDate(date, format) {
  214. date = moment.isMoment(date) ? date : moment(date);
  215. format = format || 'YYYY-MM-DD HH:mm:ss';
  216. this.timezone = this.getTimezone();
  217. return this.timezone === 'browser' ?
  218. moment(date).format(format) :
  219. moment.utc(date).format(format);
  220. }
  221. destroy() {
  222. this.events.removeAllListeners();
  223. for (let row of this.rows) {
  224. row.destroy();
  225. }
  226. }
  227. getRelativeTime(date) {
  228. date = moment.isMoment(date) ? date : moment(date);
  229. return this.timezone === 'browser' ?
  230. moment(date).fromNow() :
  231. moment.utc(date).fromNow();
  232. }
  233. getNextQueryLetter(panel) {
  234. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  235. return _.find(letters, function(refId) {
  236. return _.every(panel.targets, function(other) {
  237. return other.refId !== refId;
  238. });
  239. });
  240. }
  241. isTimezoneUtc() {
  242. return this.getTimezone() === 'utc';
  243. }
  244. getTimezone() {
  245. return this.timezone ? this.timezone : contextSrv.user.timezone;
  246. }
  247. private updateSchema(old) {
  248. var i, j, k;
  249. var oldVersion = this.schemaVersion;
  250. var panelUpgrades = [];
  251. this.schemaVersion = 13;
  252. if (oldVersion === this.schemaVersion) {
  253. return;
  254. }
  255. // version 2 schema changes
  256. if (oldVersion < 2) {
  257. if (old.services) {
  258. if (old.services.filter) {
  259. this.time = old.services.filter.time;
  260. this.templating.list = old.services.filter.list || [];
  261. }
  262. }
  263. panelUpgrades.push(function(panel) {
  264. // rename panel type
  265. if (panel.type === 'graphite') {
  266. panel.type = 'graph';
  267. }
  268. if (panel.type !== 'graph') {
  269. return;
  270. }
  271. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  272. if (panel.grid) {
  273. if (panel.grid.min) {
  274. panel.grid.leftMin = panel.grid.min;
  275. delete panel.grid.min;
  276. }
  277. if (panel.grid.max) {
  278. panel.grid.leftMax = panel.grid.max;
  279. delete panel.grid.max;
  280. }
  281. }
  282. if (panel.y_format) {
  283. panel.y_formats[0] = panel.y_format;
  284. delete panel.y_format;
  285. }
  286. if (panel.y2_format) {
  287. panel.y_formats[1] = panel.y2_format;
  288. delete panel.y2_format;
  289. }
  290. });
  291. }
  292. // schema version 3 changes
  293. if (oldVersion < 3) {
  294. // ensure panel ids
  295. var maxId = this.getNextPanelId();
  296. panelUpgrades.push(function(panel) {
  297. if (!panel.id) {
  298. panel.id = maxId;
  299. maxId += 1;
  300. }
  301. });
  302. }
  303. // schema version 4 changes
  304. if (oldVersion < 4) {
  305. // move aliasYAxis changes
  306. panelUpgrades.push(function(panel) {
  307. if (panel.type !== 'graph') { return; }
  308. _.each(panel.aliasYAxis, function(value, key) {
  309. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  310. });
  311. delete panel.aliasYAxis;
  312. });
  313. }
  314. if (oldVersion < 6) {
  315. // move pulldowns to new schema
  316. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  317. if (annotations) {
  318. this.annotations = {
  319. list: annotations.annotations || [],
  320. };
  321. }
  322. // update template variables
  323. for (i = 0 ; i < this.templating.list.length; i++) {
  324. var variable = this.templating.list[i];
  325. if (variable.datasource === void 0) { variable.datasource = null; }
  326. if (variable.type === 'filter') { variable.type = 'query'; }
  327. if (variable.type === void 0) { variable.type = 'query'; }
  328. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  329. }
  330. }
  331. if (oldVersion < 7) {
  332. if (old.nav && old.nav.length) {
  333. this.timepicker = old.nav[0];
  334. }
  335. // ensure query refIds
  336. panelUpgrades.push(function(panel) {
  337. _.each(panel.targets, function(target) {
  338. if (!target.refId) {
  339. target.refId = this.getNextQueryLetter(panel);
  340. }
  341. }.bind(this));
  342. });
  343. }
  344. if (oldVersion < 8) {
  345. panelUpgrades.push(function(panel) {
  346. _.each(panel.targets, function(target) {
  347. // update old influxdb query schema
  348. if (target.fields && target.tags && target.groupBy) {
  349. if (target.rawQuery) {
  350. delete target.fields;
  351. delete target.fill;
  352. } else {
  353. target.select = _.map(target.fields, function(field) {
  354. var parts = [];
  355. parts.push({type: 'field', params: [field.name]});
  356. parts.push({type: field.func, params: []});
  357. if (field.mathExpr) {
  358. parts.push({type: 'math', params: [field.mathExpr]});
  359. }
  360. if (field.asExpr) {
  361. parts.push({type: 'alias', params: [field.asExpr]});
  362. }
  363. return parts;
  364. });
  365. delete target.fields;
  366. _.each(target.groupBy, function(part) {
  367. if (part.type === 'time' && part.interval) {
  368. part.params = [part.interval];
  369. delete part.interval;
  370. }
  371. if (part.type === 'tag' && part.key) {
  372. part.params = [part.key];
  373. delete part.key;
  374. }
  375. });
  376. if (target.fill) {
  377. target.groupBy.push({type: 'fill', params: [target.fill]});
  378. delete target.fill;
  379. }
  380. }
  381. }
  382. });
  383. });
  384. }
  385. // schema version 9 changes
  386. if (oldVersion < 9) {
  387. // move aliasYAxis changes
  388. panelUpgrades.push(function(panel) {
  389. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  390. if (panel.thresholds) {
  391. var k = panel.thresholds.split(",");
  392. if (k.length >= 3) {
  393. k.shift();
  394. panel.thresholds = k.join(",");
  395. }
  396. }
  397. });
  398. }
  399. // schema version 10 changes
  400. if (oldVersion < 10) {
  401. // move aliasYAxis changes
  402. panelUpgrades.push(function(panel) {
  403. if (panel.type !== 'table') { return; }
  404. _.each(panel.styles, function(style) {
  405. if (style.thresholds && style.thresholds.length >= 3) {
  406. var k = style.thresholds;
  407. k.shift();
  408. style.thresholds = k;
  409. }
  410. });
  411. });
  412. }
  413. if (oldVersion < 12) {
  414. // update template variables
  415. _.each(this.templating.list, function(templateVariable) {
  416. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  417. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  418. if (templateVariable.hideVariable) {
  419. templateVariable.hide = 2;
  420. } else if (templateVariable.hideLabel) {
  421. templateVariable.hide = 1;
  422. } else {
  423. templateVariable.hide = 0;
  424. }
  425. });
  426. }
  427. if (oldVersion < 12) {
  428. // update graph yaxes changes
  429. panelUpgrades.push(function(panel) {
  430. if (panel.type !== 'graph') { return; }
  431. if (!panel.grid) { return; }
  432. if (!panel.yaxes) {
  433. panel.yaxes = [
  434. {
  435. show: panel['y-axis'],
  436. min: panel.grid.leftMin,
  437. max: panel.grid.leftMax,
  438. logBase: panel.grid.leftLogBase,
  439. format: panel.y_formats[0],
  440. label: panel.leftYAxisLabel,
  441. },
  442. {
  443. show: panel['y-axis'],
  444. min: panel.grid.rightMin,
  445. max: panel.grid.rightMax,
  446. logBase: panel.grid.rightLogBase,
  447. format: panel.y_formats[1],
  448. label: panel.rightYAxisLabel,
  449. }
  450. ];
  451. panel.xaxis = {
  452. show: panel['x-axis'],
  453. };
  454. delete panel.grid.leftMin;
  455. delete panel.grid.leftMax;
  456. delete panel.grid.leftLogBase;
  457. delete panel.grid.rightMin;
  458. delete panel.grid.rightMax;
  459. delete panel.grid.rightLogBase;
  460. delete panel.y_formats;
  461. delete panel.leftYAxisLabel;
  462. delete panel.rightYAxisLabel;
  463. delete panel['y-axis'];
  464. delete panel['x-axis'];
  465. }
  466. });
  467. }
  468. if (oldVersion < 13) {
  469. // update graph yaxes changes
  470. panelUpgrades.push(function(panel) {
  471. if (panel.type !== 'graph') { return; }
  472. if (!panel.grid) { return; }
  473. panel.thresholds = [];
  474. var t1: any = {}, t2: any = {};
  475. if (panel.grid.threshold1 !== null) {
  476. t1.value = panel.grid.threshold1;
  477. if (panel.grid.thresholdLine) {
  478. t1.line = true;
  479. t1.lineColor = panel.grid.threshold1Color;
  480. t1.colorMode = 'custom';
  481. } else {
  482. t1.fill = true;
  483. t1.fillColor = panel.grid.threshold1Color;
  484. t1.colorMode = 'custom';
  485. }
  486. }
  487. if (panel.grid.threshold2 !== null) {
  488. t2.value = panel.grid.threshold2;
  489. if (panel.grid.thresholdLine) {
  490. t2.line = true;
  491. t2.lineColor = panel.grid.threshold2Color;
  492. t2.colorMode = 'custom';
  493. } else {
  494. t2.fill = true;
  495. t2.fillColor = panel.grid.threshold2Color;
  496. t2.colorMode = 'custom';
  497. }
  498. }
  499. if (_.isNumber(t1.value)) {
  500. if (_.isNumber(t2.value)) {
  501. if (t1.value > t2.value) {
  502. t1.op = t2.op = 'lt';
  503. panel.thresholds.push(t1);
  504. panel.thresholds.push(t2);
  505. } else {
  506. t1.op = t2.op = 'gt';
  507. panel.thresholds.push(t1);
  508. panel.thresholds.push(t2);
  509. }
  510. } else {
  511. t1.op = 'gt';
  512. panel.thresholds.push(t1);
  513. }
  514. }
  515. delete panel.grid.threshold1;
  516. delete panel.grid.threshold1Color;
  517. delete panel.grid.threshold2;
  518. delete panel.grid.threshold2Color;
  519. delete panel.grid.thresholdLine;
  520. });
  521. }
  522. if (panelUpgrades.length === 0) {
  523. return;
  524. }
  525. for (i = 0; i < this.rows.length; i++) {
  526. var row = this.rows[i];
  527. for (j = 0; j < row.panels.length; j++) {
  528. for (k = 0; k < panelUpgrades.length; k++) {
  529. panelUpgrades[k].call(this, row.panels[j]);
  530. }
  531. }
  532. }
  533. }
  534. }