dashboard_migration.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import _ from 'lodash';
  2. import {
  3. GRID_COLUMN_COUNT,
  4. GRID_CELL_HEIGHT,
  5. GRID_CELL_VMARGIN,
  6. DEFAULT_ROW_HEIGHT,
  7. MIN_PANEL_HEIGHT,
  8. DEFAULT_PANEL_SPAN,
  9. } from 'app/core/constants';
  10. import { PanelModel } from './panel_model';
  11. import { DashboardModel } from './dashboard_model';
  12. export class DashboardMigrator {
  13. dashboard: DashboardModel;
  14. constructor(dashboardModel: DashboardModel) {
  15. this.dashboard = dashboardModel;
  16. }
  17. updateSchema(old) {
  18. var i, j, k;
  19. var oldVersion = this.dashboard.schemaVersion;
  20. var panelUpgrades = [];
  21. this.dashboard.schemaVersion = 16;
  22. if (oldVersion === this.dashboard.schemaVersion) {
  23. return;
  24. }
  25. // version 2 schema changes
  26. if (oldVersion < 2) {
  27. if (old.services) {
  28. if (old.services.filter) {
  29. this.dashboard.time = old.services.filter.time;
  30. this.dashboard.templating.list = old.services.filter.list || [];
  31. }
  32. }
  33. panelUpgrades.push(function(panel) {
  34. // rename panel type
  35. if (panel.type === 'graphite') {
  36. panel.type = 'graph';
  37. }
  38. if (panel.type !== 'graph') {
  39. return;
  40. }
  41. if (_.isBoolean(panel.legend)) {
  42. panel.legend = { show: panel.legend };
  43. }
  44. if (panel.grid) {
  45. if (panel.grid.min) {
  46. panel.grid.leftMin = panel.grid.min;
  47. delete panel.grid.min;
  48. }
  49. if (panel.grid.max) {
  50. panel.grid.leftMax = panel.grid.max;
  51. delete panel.grid.max;
  52. }
  53. }
  54. if (panel.y_format) {
  55. panel.y_formats[0] = panel.y_format;
  56. delete panel.y_format;
  57. }
  58. if (panel.y2_format) {
  59. panel.y_formats[1] = panel.y2_format;
  60. delete panel.y2_format;
  61. }
  62. });
  63. }
  64. // schema version 3 changes
  65. if (oldVersion < 3) {
  66. // ensure panel ids
  67. var maxId = this.dashboard.getNextPanelId();
  68. panelUpgrades.push(function(panel) {
  69. if (!panel.id) {
  70. panel.id = maxId;
  71. maxId += 1;
  72. }
  73. });
  74. }
  75. // schema version 4 changes
  76. if (oldVersion < 4) {
  77. // move aliasYAxis changes
  78. panelUpgrades.push(function(panel) {
  79. if (panel.type !== 'graph') {
  80. return;
  81. }
  82. _.each(panel.aliasYAxis, function(value, key) {
  83. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  84. });
  85. delete panel.aliasYAxis;
  86. });
  87. }
  88. if (oldVersion < 6) {
  89. // move pulldowns to new schema
  90. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  91. if (annotations) {
  92. this.dashboard.annotations = {
  93. list: annotations.annotations || [],
  94. };
  95. }
  96. // update template variables
  97. for (i = 0; i < this.dashboard.templating.list.length; i++) {
  98. var variable = this.dashboard.templating.list[i];
  99. if (variable.datasource === void 0) {
  100. variable.datasource = null;
  101. }
  102. if (variable.type === 'filter') {
  103. variable.type = 'query';
  104. }
  105. if (variable.type === void 0) {
  106. variable.type = 'query';
  107. }
  108. if (variable.allFormat === void 0) {
  109. variable.allFormat = 'glob';
  110. }
  111. }
  112. }
  113. if (oldVersion < 7) {
  114. if (old.nav && old.nav.length) {
  115. this.dashboard.timepicker = old.nav[0];
  116. }
  117. // ensure query refIds
  118. panelUpgrades.push(function(panel) {
  119. _.each(
  120. panel.targets,
  121. function(target) {
  122. if (!target.refId) {
  123. target.refId = this.dashboard.getNextQueryLetter(panel);
  124. }
  125. }.bind(this)
  126. );
  127. });
  128. }
  129. if (oldVersion < 8) {
  130. panelUpgrades.push(function(panel) {
  131. _.each(panel.targets, function(target) {
  132. // update old influxdb query schema
  133. if (target.fields && target.tags && target.groupBy) {
  134. if (target.rawQuery) {
  135. delete target.fields;
  136. delete target.fill;
  137. } else {
  138. target.select = _.map(target.fields, function(field) {
  139. var parts = [];
  140. parts.push({ type: 'field', params: [field.name] });
  141. parts.push({ type: field.func, params: [] });
  142. if (field.mathExpr) {
  143. parts.push({ type: 'math', params: [field.mathExpr] });
  144. }
  145. if (field.asExpr) {
  146. parts.push({ type: 'alias', params: [field.asExpr] });
  147. }
  148. return parts;
  149. });
  150. delete target.fields;
  151. _.each(target.groupBy, function(part) {
  152. if (part.type === 'time' && part.interval) {
  153. part.params = [part.interval];
  154. delete part.interval;
  155. }
  156. if (part.type === 'tag' && part.key) {
  157. part.params = [part.key];
  158. delete part.key;
  159. }
  160. });
  161. if (target.fill) {
  162. target.groupBy.push({ type: 'fill', params: [target.fill] });
  163. delete target.fill;
  164. }
  165. }
  166. }
  167. });
  168. });
  169. }
  170. // schema version 9 changes
  171. if (oldVersion < 9) {
  172. // move aliasYAxis changes
  173. panelUpgrades.push(function(panel) {
  174. if (panel.type !== 'singlestat' && panel.thresholds !== '') {
  175. return;
  176. }
  177. if (panel.thresholds) {
  178. var k = panel.thresholds.split(',');
  179. if (k.length >= 3) {
  180. k.shift();
  181. panel.thresholds = k.join(',');
  182. }
  183. }
  184. });
  185. }
  186. // schema version 10 changes
  187. if (oldVersion < 10) {
  188. // move aliasYAxis changes
  189. panelUpgrades.push(function(panel) {
  190. if (panel.type !== 'table') {
  191. return;
  192. }
  193. _.each(panel.styles, function(style) {
  194. if (style.thresholds && style.thresholds.length >= 3) {
  195. var k = style.thresholds;
  196. k.shift();
  197. style.thresholds = k;
  198. }
  199. });
  200. });
  201. }
  202. if (oldVersion < 12) {
  203. // update template variables
  204. _.each(this.dashboard.templating.list, function(templateVariable) {
  205. if (templateVariable.refresh) {
  206. templateVariable.refresh = 1;
  207. }
  208. if (!templateVariable.refresh) {
  209. templateVariable.refresh = 0;
  210. }
  211. if (templateVariable.hideVariable) {
  212. templateVariable.hide = 2;
  213. } else if (templateVariable.hideLabel) {
  214. templateVariable.hide = 1;
  215. }
  216. });
  217. }
  218. if (oldVersion < 12) {
  219. // update graph yaxes changes
  220. panelUpgrades.push(function(panel) {
  221. if (panel.type !== 'graph') {
  222. return;
  223. }
  224. if (!panel.grid) {
  225. return;
  226. }
  227. if (!panel.yaxes) {
  228. panel.yaxes = [
  229. {
  230. show: panel['y-axis'],
  231. min: panel.grid.leftMin,
  232. max: panel.grid.leftMax,
  233. logBase: panel.grid.leftLogBase,
  234. format: panel.y_formats[0],
  235. label: panel.leftYAxisLabel,
  236. },
  237. {
  238. show: panel['y-axis'],
  239. min: panel.grid.rightMin,
  240. max: panel.grid.rightMax,
  241. logBase: panel.grid.rightLogBase,
  242. format: panel.y_formats[1],
  243. label: panel.rightYAxisLabel,
  244. },
  245. ];
  246. panel.xaxis = {
  247. show: panel['x-axis'],
  248. };
  249. delete panel.grid.leftMin;
  250. delete panel.grid.leftMax;
  251. delete panel.grid.leftLogBase;
  252. delete panel.grid.rightMin;
  253. delete panel.grid.rightMax;
  254. delete panel.grid.rightLogBase;
  255. delete panel.y_formats;
  256. delete panel.leftYAxisLabel;
  257. delete panel.rightYAxisLabel;
  258. delete panel['y-axis'];
  259. delete panel['x-axis'];
  260. }
  261. });
  262. }
  263. if (oldVersion < 13) {
  264. // update graph yaxes changes
  265. panelUpgrades.push(function(panel) {
  266. if (panel.type !== 'graph') {
  267. return;
  268. }
  269. if (!panel.grid) {
  270. return;
  271. }
  272. panel.thresholds = [];
  273. var t1: any = {},
  274. t2: any = {};
  275. if (panel.grid.threshold1 !== null) {
  276. t1.value = panel.grid.threshold1;
  277. if (panel.grid.thresholdLine) {
  278. t1.line = true;
  279. t1.lineColor = panel.grid.threshold1Color;
  280. t1.colorMode = 'custom';
  281. } else {
  282. t1.fill = true;
  283. t1.fillColor = panel.grid.threshold1Color;
  284. t1.colorMode = 'custom';
  285. }
  286. }
  287. if (panel.grid.threshold2 !== null) {
  288. t2.value = panel.grid.threshold2;
  289. if (panel.grid.thresholdLine) {
  290. t2.line = true;
  291. t2.lineColor = panel.grid.threshold2Color;
  292. t2.colorMode = 'custom';
  293. } else {
  294. t2.fill = true;
  295. t2.fillColor = panel.grid.threshold2Color;
  296. t2.colorMode = 'custom';
  297. }
  298. }
  299. if (_.isNumber(t1.value)) {
  300. if (_.isNumber(t2.value)) {
  301. if (t1.value > t2.value) {
  302. t1.op = t2.op = 'lt';
  303. panel.thresholds.push(t1);
  304. panel.thresholds.push(t2);
  305. } else {
  306. t1.op = t2.op = 'gt';
  307. panel.thresholds.push(t1);
  308. panel.thresholds.push(t2);
  309. }
  310. } else {
  311. t1.op = 'gt';
  312. panel.thresholds.push(t1);
  313. }
  314. }
  315. delete panel.grid.threshold1;
  316. delete panel.grid.threshold1Color;
  317. delete panel.grid.threshold2;
  318. delete panel.grid.threshold2Color;
  319. delete panel.grid.thresholdLine;
  320. });
  321. }
  322. if (oldVersion < 14) {
  323. this.dashboard.graphTooltip = old.sharedCrosshair ? 1 : 0;
  324. }
  325. if (oldVersion < 16) {
  326. this.upgradeToGridLayout(old);
  327. }
  328. if (panelUpgrades.length === 0) {
  329. return;
  330. }
  331. for (j = 0; j < this.dashboard.panels.length; j++) {
  332. for (k = 0; k < panelUpgrades.length; k++) {
  333. panelUpgrades[k].call(this, this.dashboard.panels[j]);
  334. }
  335. }
  336. }
  337. upgradeToGridLayout(old) {
  338. let yPos = 0;
  339. let widthFactor = GRID_COLUMN_COUNT / 12;
  340. const maxPanelId = _.max(
  341. _.flattenDeep(
  342. _.map(old.rows, row => {
  343. return _.map(row.panels, 'id');
  344. })
  345. )
  346. );
  347. let nextRowId = maxPanelId + 1;
  348. if (!old.rows) {
  349. return;
  350. }
  351. // Add special "row" panels if even one row is collapsed, repeated or has visible title
  352. const showRows = _.some(old.rows, row => row.collapse || row.showTitle || row.repeat);
  353. for (let row of old.rows) {
  354. if (row.repeatIteration) {
  355. continue;
  356. }
  357. let height: any = row.height || DEFAULT_ROW_HEIGHT;
  358. const rowGridHeight = getGridHeight(height);
  359. let rowPanel: any = {};
  360. let rowPanelModel: PanelModel;
  361. if (showRows) {
  362. // add special row panel
  363. rowPanel.id = nextRowId;
  364. rowPanel.type = 'row';
  365. rowPanel.title = row.title;
  366. rowPanel.collapsed = row.collapse;
  367. rowPanel.repeat = row.repeat;
  368. rowPanel.panels = [];
  369. rowPanel.gridPos = {
  370. x: 0,
  371. y: yPos,
  372. w: GRID_COLUMN_COUNT,
  373. h: rowGridHeight,
  374. };
  375. rowPanelModel = new PanelModel(rowPanel);
  376. nextRowId++;
  377. yPos++;
  378. }
  379. let rowArea = new RowArea(rowGridHeight, GRID_COLUMN_COUNT, yPos);
  380. for (let panel of row.panels) {
  381. panel.span = panel.span || DEFAULT_PANEL_SPAN;
  382. const panelWidth = Math.floor(panel.span) * widthFactor;
  383. const panelHeight = panel.height ? getGridHeight(panel.height) : rowGridHeight;
  384. let panelPos = rowArea.getPanelPosition(panelHeight, panelWidth);
  385. yPos = rowArea.yPos;
  386. panel.gridPos = {
  387. x: panelPos.x,
  388. y: yPos + panelPos.y,
  389. w: panelWidth,
  390. h: panelHeight,
  391. };
  392. rowArea.addPanel(panel.gridPos);
  393. delete panel.span;
  394. if (rowPanelModel && rowPanel.collapsed) {
  395. rowPanelModel.panels.push(panel);
  396. } else {
  397. this.dashboard.panels.push(new PanelModel(panel));
  398. }
  399. }
  400. if (rowPanelModel) {
  401. this.dashboard.panels.push(rowPanelModel);
  402. }
  403. if (!(rowPanelModel && rowPanel.collapsed)) {
  404. yPos += rowGridHeight;
  405. }
  406. }
  407. }
  408. }
  409. function getGridHeight(height) {
  410. if (_.isString(height)) {
  411. height = parseInt(height.replace('px', ''), 10);
  412. }
  413. if (height < MIN_PANEL_HEIGHT) {
  414. height = MIN_PANEL_HEIGHT;
  415. }
  416. const gridHeight = Math.ceil(height / (GRID_CELL_HEIGHT + GRID_CELL_VMARGIN));
  417. return gridHeight;
  418. }
  419. /**
  420. * RowArea represents dashboard row filled by panels
  421. * area is an array of numbers represented filled column's cells like
  422. * -----------------------
  423. * |******** ****
  424. * |******** ****
  425. * |********
  426. * -----------------------
  427. * 33333333 2222 00000 ...
  428. */
  429. class RowArea {
  430. area: number[];
  431. yPos: number;
  432. height: number;
  433. constructor(height, width = GRID_COLUMN_COUNT, rowYPos = 0) {
  434. this.area = new Array(width).fill(0);
  435. this.yPos = rowYPos;
  436. this.height = height;
  437. }
  438. reset() {
  439. this.area.fill(0);
  440. }
  441. /**
  442. * Update area after adding the panel.
  443. */
  444. addPanel(gridPos) {
  445. for (let i = gridPos.x; i < gridPos.x + gridPos.w; i++) {
  446. if (!this.area[i] || gridPos.y + gridPos.h - this.yPos > this.area[i]) {
  447. this.area[i] = gridPos.y + gridPos.h - this.yPos;
  448. }
  449. }
  450. return this.area;
  451. }
  452. /**
  453. * Calculate position for the new panel in the row.
  454. */
  455. getPanelPosition(panelHeight, panelWidth, callOnce = false) {
  456. let startPlace, endPlace;
  457. let place;
  458. for (let i = this.area.length - 1; i >= 0; i--) {
  459. if (this.height - this.area[i] > 0) {
  460. if (endPlace === undefined) {
  461. endPlace = i;
  462. } else {
  463. if (i < this.area.length - 1 && this.area[i] <= this.area[i + 1]) {
  464. startPlace = i;
  465. } else {
  466. break;
  467. }
  468. }
  469. } else {
  470. break;
  471. }
  472. }
  473. if (startPlace !== undefined && endPlace !== undefined && endPlace - startPlace >= panelWidth - 1) {
  474. const yPos = _.max(this.area.slice(startPlace));
  475. place = {
  476. x: startPlace,
  477. y: yPos,
  478. };
  479. } else if (!callOnce) {
  480. // wrap to next row
  481. this.yPos += this.height;
  482. this.reset();
  483. return this.getPanelPosition(panelHeight, panelWidth, true);
  484. } else {
  485. return null;
  486. }
  487. return place;
  488. }
  489. }