DashboardMigrator.ts 18 KB

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