model.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. var variables = this.templating.list;
  90. delete this.events;
  91. delete this.meta;
  92. // prepare save model
  93. this.rows = _.map(rows, row => row.getSaveModel());
  94. this.templating.list = _.map(variables, variable => variable.getSaveModel ? variable.getSaveModel() : variable);
  95. var copy = $.extend(true, {}, this);
  96. // restore properties
  97. this.events = events;
  98. this.meta = meta;
  99. this.rows = rows;
  100. this.templating.list = variables;
  101. return copy;
  102. }
  103. addEmptyRow() {
  104. this.rows.push(new DashboardRow({isNew: true}));
  105. }
  106. private ensureListExist(data) {
  107. if (!data) { data = {}; }
  108. if (!data.list) { data.list = []; }
  109. return data;
  110. }
  111. getNextPanelId() {
  112. var i, j, row, panel, max = 0;
  113. for (i = 0; i < this.rows.length; i++) {
  114. row = this.rows[i];
  115. for (j = 0; j < row.panels.length; j++) {
  116. panel = row.panels[j];
  117. if (panel.id > max) { max = panel.id; }
  118. }
  119. }
  120. return max + 1;
  121. }
  122. forEachPanel(callback) {
  123. var i, j, row;
  124. for (i = 0; i < this.rows.length; i++) {
  125. row = this.rows[i];
  126. for (j = 0; j < row.panels.length; j++) {
  127. callback(row.panels[j], j, row, i);
  128. }
  129. }
  130. }
  131. getPanelById(id) {
  132. for (var i = 0; i < this.rows.length; i++) {
  133. var row = this.rows[i];
  134. for (var j = 0; j < row.panels.length; j++) {
  135. var panel = row.panels[j];
  136. if (panel.id === id) {
  137. return panel;
  138. }
  139. }
  140. }
  141. return null;
  142. }
  143. addPanel(panel, row) {
  144. panel.id = this.getNextPanelId();
  145. row.addPanel(panel);
  146. }
  147. removeRow(row, force?) {
  148. var index = _.indexOf(this.rows, row);
  149. if (!row.panels.length || force) {
  150. this.rows.splice(index, 1);
  151. row.destroy();
  152. return;
  153. }
  154. appEvents.emit('confirm-modal', {
  155. title: 'Remove Row',
  156. text: 'Are you sure you want to remove this row?',
  157. icon: 'fa-trash',
  158. yesText: 'Delete',
  159. onConfirm: () => {
  160. this.rows.splice(index, 1);
  161. row.destroy();
  162. }
  163. });
  164. }
  165. toggleEditMode() {
  166. if (!this.meta.canEdit) {
  167. console.log('Not allowed to edit dashboard');
  168. return;
  169. }
  170. this.editMode = !this.editMode;
  171. this.updateSubmenuVisibility();
  172. this.events.emit('edit-mode-changed', this.editMode);
  173. }
  174. setPanelFocus(id) {
  175. this.meta.focusPanelId = id;
  176. }
  177. updateSubmenuVisibility() {
  178. if (this.editMode) {
  179. this.meta.submenuEnabled = true;
  180. return;
  181. }
  182. var visibleVars = _.filter(this.templating.list, function(template) {
  183. return template.hide !== 2;
  184. });
  185. this.meta.submenuEnabled = visibleVars.length > 0 || this.annotations.list.length > 0 || this.links.length > 0;
  186. }
  187. getPanelInfoById(panelId) {
  188. var result: any = {};
  189. _.each(this.rows, function(row) {
  190. _.each(row.panels, function(panel, index) {
  191. if (panel.id === panelId) {
  192. result.panel = panel;
  193. result.row = row;
  194. result.index = index;
  195. }
  196. });
  197. });
  198. if (!result.panel) {
  199. return null;
  200. }
  201. return result;
  202. }
  203. duplicatePanel(panel, row) {
  204. var newPanel = angular.copy(panel);
  205. newPanel.id = this.getNextPanelId();
  206. delete newPanel.repeat;
  207. delete newPanel.repeatIteration;
  208. delete newPanel.repeatPanelId;
  209. delete newPanel.scopedVars;
  210. delete newPanel.alert;
  211. row.addPanel(newPanel);
  212. return newPanel;
  213. }
  214. formatDate(date, format) {
  215. date = moment.isMoment(date) ? date : moment(date);
  216. format = format || 'YYYY-MM-DD HH:mm:ss';
  217. this.timezone = this.getTimezone();
  218. return this.timezone === 'browser' ?
  219. moment(date).format(format) :
  220. moment.utc(date).format(format);
  221. }
  222. destroy() {
  223. this.events.removeAllListeners();
  224. for (let row of this.rows) {
  225. row.destroy();
  226. }
  227. }
  228. getRelativeTime(date) {
  229. date = moment.isMoment(date) ? date : moment(date);
  230. return this.timezone === 'browser' ?
  231. moment(date).fromNow() :
  232. moment.utc(date).fromNow();
  233. }
  234. getNextQueryLetter(panel) {
  235. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  236. return _.find(letters, function(refId) {
  237. return _.every(panel.targets, function(other) {
  238. return other.refId !== refId;
  239. });
  240. });
  241. }
  242. isTimezoneUtc() {
  243. return this.getTimezone() === 'utc';
  244. }
  245. getTimezone() {
  246. return this.timezone ? this.timezone : contextSrv.user.timezone;
  247. }
  248. private updateSchema(old) {
  249. var i, j, k;
  250. var oldVersion = this.schemaVersion;
  251. var panelUpgrades = [];
  252. this.schemaVersion = 13;
  253. if (oldVersion === this.schemaVersion) {
  254. return;
  255. }
  256. // version 2 schema changes
  257. if (oldVersion < 2) {
  258. if (old.services) {
  259. if (old.services.filter) {
  260. this.time = old.services.filter.time;
  261. this.templating.list = old.services.filter.list || [];
  262. }
  263. }
  264. panelUpgrades.push(function(panel) {
  265. // rename panel type
  266. if (panel.type === 'graphite') {
  267. panel.type = 'graph';
  268. }
  269. if (panel.type !== 'graph') {
  270. return;
  271. }
  272. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  273. if (panel.grid) {
  274. if (panel.grid.min) {
  275. panel.grid.leftMin = panel.grid.min;
  276. delete panel.grid.min;
  277. }
  278. if (panel.grid.max) {
  279. panel.grid.leftMax = panel.grid.max;
  280. delete panel.grid.max;
  281. }
  282. }
  283. if (panel.y_format) {
  284. panel.y_formats[0] = panel.y_format;
  285. delete panel.y_format;
  286. }
  287. if (panel.y2_format) {
  288. panel.y_formats[1] = panel.y2_format;
  289. delete panel.y2_format;
  290. }
  291. });
  292. }
  293. // schema version 3 changes
  294. if (oldVersion < 3) {
  295. // ensure panel ids
  296. var maxId = this.getNextPanelId();
  297. panelUpgrades.push(function(panel) {
  298. if (!panel.id) {
  299. panel.id = maxId;
  300. maxId += 1;
  301. }
  302. });
  303. }
  304. // schema version 4 changes
  305. if (oldVersion < 4) {
  306. // move aliasYAxis changes
  307. panelUpgrades.push(function(panel) {
  308. if (panel.type !== 'graph') { return; }
  309. _.each(panel.aliasYAxis, function(value, key) {
  310. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  311. });
  312. delete panel.aliasYAxis;
  313. });
  314. }
  315. if (oldVersion < 6) {
  316. // move pulldowns to new schema
  317. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  318. if (annotations) {
  319. this.annotations = {
  320. list: annotations.annotations || [],
  321. };
  322. }
  323. // update template variables
  324. for (i = 0 ; i < this.templating.list.length; i++) {
  325. var variable = this.templating.list[i];
  326. if (variable.datasource === void 0) { variable.datasource = null; }
  327. if (variable.type === 'filter') { variable.type = 'query'; }
  328. if (variable.type === void 0) { variable.type = 'query'; }
  329. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  330. }
  331. }
  332. if (oldVersion < 7) {
  333. if (old.nav && old.nav.length) {
  334. this.timepicker = old.nav[0];
  335. }
  336. // ensure query refIds
  337. panelUpgrades.push(function(panel) {
  338. _.each(panel.targets, function(target) {
  339. if (!target.refId) {
  340. target.refId = this.getNextQueryLetter(panel);
  341. }
  342. }.bind(this));
  343. });
  344. }
  345. if (oldVersion < 8) {
  346. panelUpgrades.push(function(panel) {
  347. _.each(panel.targets, function(target) {
  348. // update old influxdb query schema
  349. if (target.fields && target.tags && target.groupBy) {
  350. if (target.rawQuery) {
  351. delete target.fields;
  352. delete target.fill;
  353. } else {
  354. target.select = _.map(target.fields, function(field) {
  355. var parts = [];
  356. parts.push({type: 'field', params: [field.name]});
  357. parts.push({type: field.func, params: []});
  358. if (field.mathExpr) {
  359. parts.push({type: 'math', params: [field.mathExpr]});
  360. }
  361. if (field.asExpr) {
  362. parts.push({type: 'alias', params: [field.asExpr]});
  363. }
  364. return parts;
  365. });
  366. delete target.fields;
  367. _.each(target.groupBy, function(part) {
  368. if (part.type === 'time' && part.interval) {
  369. part.params = [part.interval];
  370. delete part.interval;
  371. }
  372. if (part.type === 'tag' && part.key) {
  373. part.params = [part.key];
  374. delete part.key;
  375. }
  376. });
  377. if (target.fill) {
  378. target.groupBy.push({type: 'fill', params: [target.fill]});
  379. delete target.fill;
  380. }
  381. }
  382. }
  383. });
  384. });
  385. }
  386. // schema version 9 changes
  387. if (oldVersion < 9) {
  388. // move aliasYAxis changes
  389. panelUpgrades.push(function(panel) {
  390. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  391. if (panel.thresholds) {
  392. var k = panel.thresholds.split(",");
  393. if (k.length >= 3) {
  394. k.shift();
  395. panel.thresholds = k.join(",");
  396. }
  397. }
  398. });
  399. }
  400. // schema version 10 changes
  401. if (oldVersion < 10) {
  402. // move aliasYAxis changes
  403. panelUpgrades.push(function(panel) {
  404. if (panel.type !== 'table') { return; }
  405. _.each(panel.styles, function(style) {
  406. if (style.thresholds && style.thresholds.length >= 3) {
  407. var k = style.thresholds;
  408. k.shift();
  409. style.thresholds = k;
  410. }
  411. });
  412. });
  413. }
  414. if (oldVersion < 12) {
  415. // update template variables
  416. _.each(this.templating.list, function(templateVariable) {
  417. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  418. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  419. if (templateVariable.hideVariable) {
  420. templateVariable.hide = 2;
  421. } else if (templateVariable.hideLabel) {
  422. templateVariable.hide = 1;
  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. }