ValueMappings: Improve singlestat value mappings migration (#35578)

* Migrate singlestat value mappings to new value mappings

* Update public/app/features/dashboard/state/DashboardMigrator.ts

* Update migration to produce single value map
This commit is contained in:
Dominik Prokop
2021-06-14 13:47:34 +02:00
committed by GitHub
parent 7599ab8217
commit 08bc2b402b
4 changed files with 158 additions and 5 deletions

View File

@@ -1242,6 +1242,146 @@ describe('DashboardModel', () => {
`);
});
});
describe('when migrating singlestat value mappings', () => {
it('should migrate value mapping', () => {
const model = new DashboardModel({
panels: [
{
type: 'singlestat',
legend: true,
thresholds: '10,20,30',
colors: ['#FF0000', 'green', 'orange'],
aliasYAxis: { test: 2 },
grid: { min: 1, max: 10 },
targets: [{ refId: 'A' }, {}],
mappingType: 1,
mappingTypes: [
{
name: 'value to text',
value: 1,
},
],
valueMaps: [
{
op: '=',
text: 'test',
value: '20',
},
{
op: '=',
text: 'test1',
value: '30',
},
{
op: '=',
text: '50',
value: '40',
},
],
},
],
});
expect(model.panels[0].fieldConfig.defaults.mappings).toMatchInlineSnapshot(`
Array [
Object {
"options": Object {
"20": Object {
"color": undefined,
"text": "test",
},
"30": Object {
"color": undefined,
"text": "test1",
},
"40": Object {
"color": "orange",
"text": "50",
},
},
"type": "value",
},
]
`);
});
it('should migrate range mapping', () => {
const model = new DashboardModel({
panels: [
{
type: 'singlestat',
legend: true,
thresholds: '10,20,30',
colors: ['#FF0000', 'green', 'orange'],
aliasYAxis: { test: 2 },
grid: { min: 1, max: 10 },
targets: [{ refId: 'A' }, {}],
mappingType: 2,
mappingTypes: [
{
name: 'range to text',
value: 2,
},
],
rangeMaps: [
{
from: '20',
to: '25',
text: 'text1',
},
{
from: '1',
to: '5',
text: 'text2',
},
{
from: '5',
to: '10',
text: '50',
},
],
},
],
});
expect(model.panels[0].fieldConfig.defaults.mappings).toMatchInlineSnapshot(`
Array [
Object {
"options": Object {
"from": 20,
"result": Object {
"color": undefined,
"text": "text1",
},
"to": 25,
},
"type": "range",
},
Object {
"options": Object {
"from": 1,
"result": Object {
"color": undefined,
"text": "text2",
},
"to": 5,
},
"type": "range",
},
Object {
"options": Object {
"from": 5,
"result": Object {
"color": "orange",
"text": "50",
},
"to": 10,
},
"type": "range",
},
]
`);
});
});
});
function createRow(options: any, panelDescriptions: any[]) {

View File

@@ -977,6 +977,20 @@ function upgradeValueMappings(oldMappings: any, thresholds?: ThresholdsConfig):
const newMappings: ValueMapping[] = [];
for (const old of oldMappings) {
// when migrating singlestat to stat/gauge, mappings are handled by panel type change handler used in that migration
if (old.type && old.options) {
// collect al value->text mappings in a single value map object. These are migrated by panel change handler as a separate value maps
if (old.type === MappingType.ValueToText) {
valueMaps.options = {
...valueMaps.options,
...old.options,
};
} else {
newMappings.push(old);
}
continue;
}
// Use the color we would have picked from thesholds
let color: string | undefined = undefined;
const numeric = parseFloat(old.text);