dashboard_migration.ts 15 KB

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