mirror of https://github.com/grafana/grafana
Gauge/BarGauge: Rewrite of how migrations are applied (#18375)
parent
2514209083
commit
541981c341
@ -0,0 +1,39 @@ |
|||||||
|
import { sharedSingleStatMigrationCheck } from './SingleStatBaseOptions'; |
||||||
|
|
||||||
|
describe('sharedSingleStatMigrationCheck', () => { |
||||||
|
it('from old valueOptions model without pluginVersion', () => { |
||||||
|
const panel = { |
||||||
|
options: { |
||||||
|
valueOptions: { |
||||||
|
unit: 'watt', |
||||||
|
stat: 'last', |
||||||
|
decimals: 5, |
||||||
|
}, |
||||||
|
minValue: 10, |
||||||
|
maxValue: 100, |
||||||
|
valueMappings: [{ type: 1, value: '1', text: 'OK' }], |
||||||
|
thresholds: [ |
||||||
|
{ |
||||||
|
color: 'green', |
||||||
|
index: 0, |
||||||
|
value: null, |
||||||
|
}, |
||||||
|
{ |
||||||
|
color: 'orange', |
||||||
|
index: 1, |
||||||
|
value: 40, |
||||||
|
}, |
||||||
|
{ |
||||||
|
color: 'red', |
||||||
|
index: 2, |
||||||
|
value: 80, |
||||||
|
}, |
||||||
|
], |
||||||
|
}, |
||||||
|
title: 'Usage', |
||||||
|
type: 'bargauge', |
||||||
|
}; |
||||||
|
|
||||||
|
expect(sharedSingleStatMigrationCheck(panel as any)).toMatchSnapshot(); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,38 @@ |
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP |
||||||
|
|
||||||
|
exports[`sharedSingleStatMigrationCheck from old valueOptions model without pluginVersion 1`] = ` |
||||||
|
Object { |
||||||
|
"fieldOptions": Object { |
||||||
|
"calcs": Array [ |
||||||
|
"last", |
||||||
|
], |
||||||
|
"defaults": Object { |
||||||
|
"decimals": 5, |
||||||
|
"mappings": Array [ |
||||||
|
Object { |
||||||
|
"text": "OK", |
||||||
|
"type": 1, |
||||||
|
"value": "1", |
||||||
|
}, |
||||||
|
], |
||||||
|
"max": 100, |
||||||
|
"min": 10, |
||||||
|
"thresholds": Array [ |
||||||
|
Object { |
||||||
|
"color": "green", |
||||||
|
"value": -Infinity, |
||||||
|
}, |
||||||
|
Object { |
||||||
|
"color": "orange", |
||||||
|
"value": 40, |
||||||
|
}, |
||||||
|
Object { |
||||||
|
"color": "red", |
||||||
|
"value": 80, |
||||||
|
}, |
||||||
|
], |
||||||
|
"unit": "watt", |
||||||
|
}, |
||||||
|
}, |
||||||
|
} |
||||||
|
`; |
@ -1,36 +1,7 @@ |
|||||||
import { PanelModel } from '@grafana/ui'; |
import { PanelModel } from '@grafana/ui'; |
||||||
import { |
import { sharedSingleStatMigrationCheck } from '@grafana/ui/src/components/SingleStatShared/SingleStatBaseOptions'; |
||||||
sharedSingleStatMigrationCheck, |
|
||||||
migrateOldThresholds, |
|
||||||
} from '@grafana/ui/src/components/SingleStatShared/SingleStatBaseOptions'; |
|
||||||
import { BarGaugeOptions } from './types'; |
import { BarGaugeOptions } from './types'; |
||||||
|
|
||||||
export const barGaugePanelMigrationCheck = (panel: PanelModel<BarGaugeOptions>): Partial<BarGaugeOptions> => { |
export const barGaugePanelMigrationCheck = (panel: PanelModel<BarGaugeOptions>): Partial<BarGaugeOptions> => { |
||||||
if (!panel.options) { |
|
||||||
// This happens on the first load or when migrating from angular
|
|
||||||
return {}; |
|
||||||
} |
|
||||||
|
|
||||||
// Move thresholds to field
|
|
||||||
const previousVersion = panel.pluginVersion || ''; |
|
||||||
if (previousVersion.startsWith('6.2') || previousVersion.startsWith('6.3')) { |
|
||||||
console.log('TRANSFORM', panel.options); |
|
||||||
const old = panel.options as any; |
|
||||||
const { fieldOptions } = old; |
|
||||||
if (fieldOptions) { |
|
||||||
const { mappings, thresholds, ...rest } = fieldOptions; |
|
||||||
rest.defaults = { |
|
||||||
mappings, |
|
||||||
thresholds: migrateOldThresholds(thresholds), |
|
||||||
...rest.defaults, |
|
||||||
}; |
|
||||||
return { |
|
||||||
...old, |
|
||||||
fieldOptions: rest, |
|
||||||
}; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// Default to the standard migration path
|
|
||||||
return sharedSingleStatMigrationCheck(panel); |
return sharedSingleStatMigrationCheck(panel); |
||||||
}; |
}; |
||||||
|
@ -1,60 +1,7 @@ |
|||||||
import { Field, fieldReducers } from '@grafana/data'; |
import { PanelModel } from '@grafana/ui'; |
||||||
import { PanelModel, FieldDisplayOptions } from '@grafana/ui'; |
|
||||||
import { GaugeOptions } from './types'; |
import { GaugeOptions } from './types'; |
||||||
import { |
import { sharedSingleStatMigrationCheck } from '@grafana/ui/src/components/SingleStatShared/SingleStatBaseOptions'; |
||||||
sharedSingleStatMigrationCheck, |
|
||||||
migrateOldThresholds, |
|
||||||
} from '@grafana/ui/src/components/SingleStatShared/SingleStatBaseOptions'; |
|
||||||
|
|
||||||
export const gaugePanelMigrationCheck = (panel: PanelModel<GaugeOptions>): Partial<GaugeOptions> => { |
export const gaugePanelMigrationCheck = (panel: PanelModel<GaugeOptions>): Partial<GaugeOptions> => { |
||||||
if (!panel.options) { |
|
||||||
// This happens on the first load or when migrating from angular
|
|
||||||
return {}; |
|
||||||
} |
|
||||||
|
|
||||||
const previousVersion = panel.pluginVersion || ''; |
|
||||||
if (!previousVersion || previousVersion.startsWith('6.1')) { |
|
||||||
const old = panel.options as any; |
|
||||||
const { valueOptions } = old; |
|
||||||
|
|
||||||
const options = {} as GaugeOptions; |
|
||||||
options.showThresholdLabels = old.showThresholdLabels; |
|
||||||
options.showThresholdMarkers = old.showThresholdMarkers; |
|
||||||
options.orientation = old.orientation; |
|
||||||
|
|
||||||
const fieldOptions = (options.fieldOptions = {} as FieldDisplayOptions); |
|
||||||
|
|
||||||
const field = (fieldOptions.defaults = {} as Field); |
|
||||||
field.mappings = old.valueMappings; |
|
||||||
field.thresholds = migrateOldThresholds(old.thresholds); |
|
||||||
field.unit = valueOptions.unit; |
|
||||||
field.decimals = valueOptions.decimals; |
|
||||||
|
|
||||||
// Make sure the stats have a valid name
|
|
||||||
if (valueOptions.stat) { |
|
||||||
fieldOptions.calcs = [fieldReducers.get(valueOptions.stat).id]; |
|
||||||
} |
|
||||||
field.min = old.minValue; |
|
||||||
field.max = old.maxValue; |
|
||||||
|
|
||||||
return options; |
|
||||||
} else if (previousVersion.startsWith('6.2') || previousVersion.startsWith('6.3')) { |
|
||||||
const old = panel.options as any; |
|
||||||
const { fieldOptions } = old; |
|
||||||
if (fieldOptions) { |
|
||||||
const { mappings, thresholds, ...rest } = fieldOptions; |
|
||||||
rest.default = { |
|
||||||
mappings, |
|
||||||
thresholds: migrateOldThresholds(thresholds), |
|
||||||
...rest.defaults, |
|
||||||
}; |
|
||||||
return { |
|
||||||
...old.options, |
|
||||||
fieldOptions: rest, |
|
||||||
}; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// Default to the standard migration path
|
|
||||||
return sharedSingleStatMigrationCheck(panel); |
return sharedSingleStatMigrationCheck(panel); |
||||||
}; |
}; |
||||||
|
Loading…
Reference in new issue