dashboard_migration.ts 14 KB

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