Commit ed73d068 by Hugo Häggmark Committed by GitHub

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
parent 1ff0d9f6
...@@ -78,7 +78,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1` ...@@ -78,7 +78,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1`
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -191,7 +191,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1` ...@@ -191,7 +191,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1`
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -285,7 +285,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1` ...@@ -285,7 +285,7 @@ exports[`DashboardPage Dashboard init completed Should render dashboard grid 1`
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -410,7 +410,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ...@@ -410,7 +410,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -523,7 +523,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ...@@ -523,7 +523,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -617,7 +617,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ...@@ -617,7 +617,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -717,7 +717,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti ...@@ -717,7 +717,7 @@ exports[`DashboardPage When dashboard has editview url state should render setti
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
......
...@@ -235,7 +235,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ...@@ -235,7 +235,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = `
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -477,7 +477,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ...@@ -477,7 +477,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = `
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -719,7 +719,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ...@@ -719,7 +719,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = `
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
...@@ -961,7 +961,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = ` ...@@ -961,7 +961,7 @@ exports[`DashboardGrid Can render dashboard grid Should render 1`] = `
], ],
"refresh": undefined, "refresh": undefined,
"revision": undefined, "revision": undefined,
"schemaVersion": 24, "schemaVersion": 25,
"snapshot": undefined, "snapshot": undefined,
"style": "dark", "style": "dark",
"tags": Array [], "tags": Array [],
......
...@@ -132,7 +132,7 @@ describe('DashboardModel', () => { ...@@ -132,7 +132,7 @@ describe('DashboardModel', () => {
}); });
it('dashboard schema version should be set to latest', () => { 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', () => { it('graph thresholds should be migrated', () => {
...@@ -626,6 +626,95 @@ describe('DashboardModel', () => { ...@@ -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[]) { function createRow(options: any, panelDescriptions: any[]) {
......
...@@ -6,7 +6,7 @@ import kbn from 'app/core/utils/kbn'; ...@@ -6,7 +6,7 @@ import kbn from 'app/core/utils/kbn';
// Types // Types
import { PanelModel } from './PanelModel'; import { PanelModel } from './PanelModel';
import { DashboardModel } from './DashboardModel'; import { DashboardModel } from './DashboardModel';
import { DataLink, urlUtil, DataLinkBuiltInVars } from '@grafana/data'; import { DataLink, DataLinkBuiltInVars, urlUtil } from '@grafana/data';
// Constants // Constants
import { import {
DEFAULT_PANEL_SPAN, DEFAULT_PANEL_SPAN,
...@@ -16,8 +16,9 @@ import { ...@@ -16,8 +16,9 @@ import {
GRID_COLUMN_COUNT, GRID_COLUMN_COUNT,
MIN_PANEL_HEIGHT, MIN_PANEL_HEIGHT,
} from 'app/core/constants'; } 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 { alignCurrentWithMulti } from 'app/features/variables/shared/multiOptions';
import { VariableTag } from '../../templating/types';
export class DashboardMigrator { export class DashboardMigrator {
dashboard: DashboardModel; dashboard: DashboardModel;
...@@ -30,7 +31,7 @@ export class DashboardMigrator { ...@@ -30,7 +31,7 @@ export class DashboardMigrator {
let i, j, k, n; let i, j, k, n;
const oldVersion = this.dashboard.schemaVersion; const oldVersion = this.dashboard.schemaVersion;
const panelUpgrades = []; const panelUpgrades = [];
this.dashboard.schemaVersion = 24; this.dashboard.schemaVersion = 25;
if (oldVersion === this.dashboard.schemaVersion) { if (oldVersion === this.dashboard.schemaVersion) {
return; return;
...@@ -522,6 +523,47 @@ export class DashboardMigrator { ...@@ -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<string, VariableTag>);
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) { if (panelUpgrades.length === 0) {
return; return;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment