From ed73d06846360e04cf4f746935e59ad746c1574e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Tue, 5 May 2020 13:27:05 +0200 Subject: [PATCH] Variables: migrates old tags format for consistency (#24276) * Variables: migrates old tags format for consistency * Update DashboardGrid.test.tsx.snap * Update DashboardPage.test.tsx.snap * Update DashboardGrid.test.tsx.snap --- .../__snapshots__/DashboardPage.test.tsx.snap | 14 +-- .../__snapshots__/DashboardGrid.test.tsx.snap | 8 +- .../dashboard/state/DashboardMigrator.test.ts | 91 ++++++++++++++++++- .../dashboard/state/DashboardMigrator.ts | 48 +++++++++- 4 files changed, 146 insertions(+), 15 deletions(-) diff --git a/public/app/features/dashboard/containers/__snapshots__/DashboardPage.test.tsx.snap b/public/app/features/dashboard/containers/__snapshots__/DashboardPage.test.tsx.snap index da633484cd3..12c84b96049 100644 --- a/public/app/features/dashboard/containers/__snapshots__/DashboardPage.test.tsx.snap +++ b/public/app/features/dashboard/containers/__snapshots__/DashboardPage.test.tsx.snap @@ -78,7 +78,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -191,7 +191,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -285,7 +285,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -410,7 +410,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -523,7 +523,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -617,7 +617,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -717,7 +717,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], diff --git a/public/app/features/dashboard/dashgrid/__snapshots__/DashboardGrid.test.tsx.snap b/public/app/features/dashboard/dashgrid/__snapshots__/DashboardGrid.test.tsx.snap index d9494e674a8..c36e1fd60af 100644 --- a/public/app/features/dashboard/dashgrid/__snapshots__/DashboardGrid.test.tsx.snap +++ b/public/app/features/dashboard/dashgrid/__snapshots__/DashboardGrid.test.tsx.snap @@ -235,7 +235,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -477,7 +477,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -719,7 +719,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], @@ -961,7 +961,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ], "refresh": undefined, "revision": undefined, - "schemaVersion": 24, + "schemaVersion": 25, "snapshot": undefined, "style": "dark", "tags": Array [], diff --git a/public/app/features/dashboard/state/DashboardMigrator.test.ts b/public/app/features/dashboard/state/DashboardMigrator.test.ts index e1457cba853..c7d1dab818b 100644 --- a/public/app/features/dashboard/state/DashboardMigrator.test.ts +++ b/public/app/features/dashboard/state/DashboardMigrator.test.ts @@ -132,7 +132,7 @@ describe('DashboardModel', () => { }); it('dashboard schema version should be set to latest', () => { - expect(model.schemaVersion).toBe(24); + expect(model.schemaVersion).toBe(25); }); it('graph thresholds should be migrated', () => { @@ -626,6 +626,95 @@ describe('DashboardModel', () => { }); }); }); + + describe('when migrating variables with old tags format', () => { + let model: DashboardModel; + + beforeEach(() => { + model = new DashboardModel({ + templating: { + list: [ + { + type: 'query', + tags: ['Africa', 'America', 'Asia', 'Europe'], + }, + { + type: 'query', + current: { + tags: [ + { + selected: true, + text: 'America', + values: ['server-us-east', 'server-us-central', 'server-us-west'], + valuesText: 'server-us-east + server-us-central + server-us-west', + }, + { + selected: true, + text: 'Europe', + values: ['server-eu-east', 'server-eu-west'], + valuesText: 'server-eu-east + server-eu-west', + }, + ], + text: 'server-us-east + server-us-central + server-us-west + server-eu-east + server-eu-west', + value: ['server-us-east', 'server-us-central', 'server-us-west', 'server-eu-east', 'server-eu-west'], + }, + tags: ['Africa', 'America', 'Asia', 'Europe'], + }, + { + type: 'query', + tags: [ + { text: 'Africa', selected: false }, + { text: 'America', selected: true }, + { text: 'Asia', selected: false }, + { text: 'Europe', selected: false }, + ], + }, + ], + }, + }); + }); + + it('should have three variables after migration', () => { + expect(model.templating.list.length).toBe(3); + }); + + it('should be migrated with defaults if being out of sync', () => { + expect(model.templating.list[0].tags).toEqual([ + { text: 'Africa', selected: false }, + { text: 'America', selected: false }, + { text: 'Asia', selected: false }, + { text: 'Europe', selected: false }, + ]); + }); + + it('should be migrated with current values if being out of sync', () => { + expect(model.templating.list[1].tags).toEqual([ + { text: 'Africa', selected: false }, + { + text: 'America', + selected: true, + values: ['server-us-east', 'server-us-central', 'server-us-west'], + valuesText: 'server-us-east + server-us-central + server-us-west', + }, + { text: 'Asia', selected: false }, + { + text: 'Europe', + selected: true, + values: ['server-eu-east', 'server-eu-west'], + valuesText: 'server-eu-east + server-eu-west', + }, + ]); + }); + + it('should not be migrated if being in sync', () => { + expect(model.templating.list[2].tags).toEqual([ + { text: 'Africa', selected: false }, + { text: 'America', selected: true }, + { text: 'Asia', selected: false }, + { text: 'Europe', selected: false }, + ]); + }); + }); }); function createRow(options: any, panelDescriptions: any[]) { diff --git a/public/app/features/dashboard/state/DashboardMigrator.ts b/public/app/features/dashboard/state/DashboardMigrator.ts index 61fd242f0c8..8031eae0597 100644 --- a/public/app/features/dashboard/state/DashboardMigrator.ts +++ b/public/app/features/dashboard/state/DashboardMigrator.ts @@ -6,7 +6,7 @@ import kbn from 'app/core/utils/kbn'; // Types import { PanelModel } from './PanelModel'; import { DashboardModel } from './DashboardModel'; -import { DataLink, urlUtil, DataLinkBuiltInVars } from '@grafana/data'; +import { DataLink, DataLinkBuiltInVars, urlUtil } from '@grafana/data'; // Constants import { DEFAULT_PANEL_SPAN, @@ -16,8 +16,9 @@ import { GRID_COLUMN_COUNT, MIN_PANEL_HEIGHT, } from 'app/core/constants'; -import { isMulti } from 'app/features/variables/guard'; +import { isMulti, isQuery } from 'app/features/variables/guard'; import { alignCurrentWithMulti } from 'app/features/variables/shared/multiOptions'; +import { VariableTag } from '../../templating/types'; export class DashboardMigrator { dashboard: DashboardModel; @@ -30,7 +31,7 @@ export class DashboardMigrator { let i, j, k, n; const oldVersion = this.dashboard.schemaVersion; const panelUpgrades = []; - this.dashboard.schemaVersion = 24; + this.dashboard.schemaVersion = 25; if (oldVersion === this.dashboard.schemaVersion) { return; @@ -522,6 +523,47 @@ export class DashboardMigrator { }); } + if (oldVersion < 25) { + for (const variable of this.dashboard.templating.list) { + if (!isQuery(variable)) { + continue; + } + + const { tags, current } = variable; + if (!Array.isArray(tags)) { + variable.tags = []; + continue; + } + + const currentTags = current?.tags ?? []; + const currents = currentTags.reduce((all, tag) => { + if (tag && tag.hasOwnProperty('text') && typeof tag['text'] === 'string') { + all[tag.text] = tag; + } + return all; + }, {} as Record); + + const newTags: VariableTag[] = []; + + for (const tag of tags) { + if (typeof tag === 'object') { + // new format let's assume it's correct + newTags.push(tag); + continue; + } + + if (typeof tag !== 'string') { + // something that we do not support + continue; + } + + const currentValue = currents[tag]; + newTags.push({ text: tag, selected: false, ...currentValue }); + } + variable.tags = newTags; + } + } + if (panelUpgrades.length === 0) { return; }