dashboard_migration.ts 15 KB

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