model.ts 16 KB

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