model.ts 16 KB

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