model.ts 16 KB

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