dashboard_migration.ts 15 KB

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