dashboard_migration.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 = 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. let 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(panel => {
  125. _.each(panel.targets, target => {
  126. if (!target.refId) {
  127. target.refId = this.dashboard.getNextQueryLetter(panel);
  128. }
  129. });
  130. });
  131. }
  132. if (oldVersion < 8) {
  133. panelUpgrades.push(function(panel) {
  134. _.each(panel.targets, function(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, function(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, function(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(function(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(function(panel) {
  193. if (panel.type !== 'table') {
  194. return;
  195. }
  196. _.each(panel.styles, function(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, function(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(function(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(function(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 (panelUpgrades.length === 0) {
  332. return;
  333. }
  334. for (j = 0; j < this.dashboard.panels.length; j++) {
  335. for (k = 0; k < panelUpgrades.length; k++) {
  336. panelUpgrades[k].call(this, this.dashboard.panels[j]);
  337. if (this.dashboard.panels[j].panels) {
  338. for (n = 0; n < this.dashboard.panels[j].panels.length; n++) {
  339. panelUpgrades[k].call(this, this.dashboard.panels[j].panels[n]);
  340. }
  341. }
  342. }
  343. }
  344. }
  345. upgradeToGridLayout(old) {
  346. let yPos = 0;
  347. const widthFactor = GRID_COLUMN_COUNT / 12;
  348. const maxPanelId = _.max(
  349. _.flattenDeep(
  350. _.map(old.rows, row => {
  351. return _.map(row.panels, 'id');
  352. })
  353. )
  354. );
  355. let nextRowId = maxPanelId + 1;
  356. if (!old.rows) {
  357. return;
  358. }
  359. // Add special "row" panels if even one row is collapsed, repeated or has visible title
  360. const showRows = _.some(old.rows, row => row.collapse || row.showTitle || row.repeat);
  361. for (const row of old.rows) {
  362. if (row.repeatIteration) {
  363. continue;
  364. }
  365. const height: any = row.height || DEFAULT_ROW_HEIGHT;
  366. const rowGridHeight = getGridHeight(height);
  367. const rowPanel: any = {};
  368. let rowPanelModel: PanelModel;
  369. if (showRows) {
  370. // add special row panel
  371. rowPanel.id = nextRowId;
  372. rowPanel.type = 'row';
  373. rowPanel.title = row.title;
  374. rowPanel.collapsed = row.collapse;
  375. rowPanel.repeat = row.repeat;
  376. rowPanel.panels = [];
  377. rowPanel.gridPos = {
  378. x: 0,
  379. y: yPos,
  380. w: GRID_COLUMN_COUNT,
  381. h: rowGridHeight,
  382. };
  383. rowPanelModel = new PanelModel(rowPanel);
  384. nextRowId++;
  385. yPos++;
  386. }
  387. const rowArea = new RowArea(rowGridHeight, GRID_COLUMN_COUNT, yPos);
  388. for (const panel of row.panels) {
  389. panel.span = panel.span || DEFAULT_PANEL_SPAN;
  390. if (panel.minSpan) {
  391. panel.minSpan = Math.min(GRID_COLUMN_COUNT, GRID_COLUMN_COUNT / 12 * panel.minSpan);
  392. }
  393. const panelWidth = Math.floor(panel.span) * widthFactor;
  394. const panelHeight = panel.height ? getGridHeight(panel.height) : rowGridHeight;
  395. const panelPos = rowArea.getPanelPosition(panelHeight, panelWidth);
  396. yPos = rowArea.yPos;
  397. panel.gridPos = {
  398. x: panelPos.x,
  399. y: yPos + panelPos.y,
  400. w: panelWidth,
  401. h: panelHeight,
  402. };
  403. rowArea.addPanel(panel.gridPos);
  404. delete panel.span;
  405. if (rowPanelModel && rowPanel.collapsed) {
  406. rowPanelModel.panels.push(panel);
  407. } else {
  408. this.dashboard.panels.push(new PanelModel(panel));
  409. }
  410. }
  411. if (rowPanelModel) {
  412. this.dashboard.panels.push(rowPanelModel);
  413. }
  414. if (!(rowPanelModel && rowPanel.collapsed)) {
  415. yPos += rowGridHeight;
  416. }
  417. }
  418. }
  419. }
  420. function getGridHeight(height) {
  421. if (_.isString(height)) {
  422. height = parseInt(height.replace('px', ''), 10);
  423. }
  424. if (height < MIN_PANEL_HEIGHT) {
  425. height = MIN_PANEL_HEIGHT;
  426. }
  427. const gridHeight = Math.ceil(height / (GRID_CELL_HEIGHT + GRID_CELL_VMARGIN));
  428. return gridHeight;
  429. }
  430. /**
  431. * RowArea represents dashboard row filled by panels
  432. * area is an array of numbers represented filled column's cells like
  433. * -----------------------
  434. * |******** ****
  435. * |******** ****
  436. * |********
  437. * -----------------------
  438. * 33333333 2222 00000 ...
  439. */
  440. class RowArea {
  441. area: number[];
  442. yPos: number;
  443. height: number;
  444. constructor(height, width = GRID_COLUMN_COUNT, rowYPos = 0) {
  445. this.area = new Array(width).fill(0);
  446. this.yPos = rowYPos;
  447. this.height = height;
  448. }
  449. reset() {
  450. this.area.fill(0);
  451. }
  452. /**
  453. * Update area after adding the panel.
  454. */
  455. addPanel(gridPos) {
  456. for (let i = gridPos.x; i < gridPos.x + gridPos.w; i++) {
  457. if (!this.area[i] || gridPos.y + gridPos.h - this.yPos > this.area[i]) {
  458. this.area[i] = gridPos.y + gridPos.h - this.yPos;
  459. }
  460. }
  461. return this.area;
  462. }
  463. /**
  464. * Calculate position for the new panel in the row.
  465. */
  466. getPanelPosition(panelHeight, panelWidth, callOnce = false) {
  467. let startPlace, endPlace;
  468. let place;
  469. for (let i = this.area.length - 1; i >= 0; i--) {
  470. if (this.height - this.area[i] > 0) {
  471. if (endPlace === undefined) {
  472. endPlace = i;
  473. } else {
  474. if (i < this.area.length - 1 && this.area[i] <= this.area[i + 1]) {
  475. startPlace = i;
  476. } else {
  477. break;
  478. }
  479. }
  480. } else {
  481. break;
  482. }
  483. }
  484. if (startPlace !== undefined && endPlace !== undefined && endPlace - startPlace >= panelWidth - 1) {
  485. const yPos = _.max(this.area.slice(startPlace));
  486. place = {
  487. x: startPlace,
  488. y: yPos,
  489. };
  490. } else if (!callOnce) {
  491. // wrap to next row
  492. this.yPos += this.height;
  493. this.reset();
  494. return this.getPanelPosition(panelHeight, panelWidth, true);
  495. } else {
  496. return null;
  497. }
  498. return place;
  499. }
  500. }