dashboard_migration.ts 15 KB

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