mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SingleStat: apply mappings to no data response (#19951)
This commit is contained in:
parent
73944c18fd
commit
8232659012
@ -72,7 +72,7 @@ export function sharedSingleStatPanelChangedHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert value mappings
|
// Convert value mappings
|
||||||
const mappings = convertOldAngulrValueMapping(panel);
|
const mappings = convertOldAngularValueMapping(panel);
|
||||||
if (mappings && mappings.length) {
|
if (mappings && mappings.length) {
|
||||||
defaults.mappings = mappings;
|
defaults.mappings = mappings;
|
||||||
}
|
}
|
||||||
@ -192,7 +192,7 @@ export function migrateOldThresholds(thresholds?: any[]): Threshold[] | undefine
|
|||||||
/**
|
/**
|
||||||
* Convert the angular single stat mapping to new react style
|
* Convert the angular single stat mapping to new react style
|
||||||
*/
|
*/
|
||||||
export function convertOldAngulrValueMapping(panel: any): ValueMapping[] {
|
export function convertOldAngularValueMapping(panel: any): ValueMapping[] {
|
||||||
const mappings: ValueMapping[] = [];
|
const mappings: ValueMapping[] = [];
|
||||||
|
|
||||||
// Guess the right type based on options
|
// Guess the right type based on options
|
||||||
|
@ -5,5 +5,5 @@ export {
|
|||||||
SingleStatBaseOptions,
|
SingleStatBaseOptions,
|
||||||
sharedSingleStatPanelChangedHandler,
|
sharedSingleStatPanelChangedHandler,
|
||||||
sharedSingleStatMigrationHandler,
|
sharedSingleStatMigrationHandler,
|
||||||
convertOldAngulrValueMapping,
|
convertOldAngularValueMapping,
|
||||||
} from './SingleStatBaseOptions';
|
} from './SingleStatBaseOptions';
|
||||||
|
@ -22,7 +22,7 @@ import {
|
|||||||
LegacyResponseData,
|
LegacyResponseData,
|
||||||
getFlotPairs,
|
getFlotPairs,
|
||||||
getDisplayProcessor,
|
getDisplayProcessor,
|
||||||
convertOldAngulrValueMapping,
|
convertOldAngularValueMapping,
|
||||||
getColorFromHexRgbOrName,
|
getColorFromHexRgbOrName,
|
||||||
PanelEvents,
|
PanelEvents,
|
||||||
} from '@grafana/ui';
|
} from '@grafana/ui';
|
||||||
@ -186,13 +186,17 @@ class SingleStatCtrl extends MetricsPanelCtrl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!fieldInfo) {
|
if (!fieldInfo) {
|
||||||
|
const processor = getDisplayProcessor({
|
||||||
|
config: {
|
||||||
|
mappings: convertOldAngularValueMapping(this.panel),
|
||||||
|
noValue: 'No Data',
|
||||||
|
},
|
||||||
|
theme: config.theme,
|
||||||
|
});
|
||||||
// When we don't have any field
|
// When we don't have any field
|
||||||
this.data = {
|
this.data = {
|
||||||
value: 'No Data',
|
value: null,
|
||||||
display: {
|
display: processor(null),
|
||||||
text: 'No Data',
|
|
||||||
numeric: NaN,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
this.data = this.processField(fieldInfo);
|
this.data = this.processField(fieldInfo);
|
||||||
@ -246,7 +250,7 @@ class SingleStatCtrl extends MetricsPanelCtrl {
|
|||||||
...fieldInfo.field.config,
|
...fieldInfo.field.config,
|
||||||
unit: panel.format,
|
unit: panel.format,
|
||||||
decimals: panel.decimals,
|
decimals: panel.decimals,
|
||||||
mappings: convertOldAngulrValueMapping(panel),
|
mappings: convertOldAngularValueMapping(panel),
|
||||||
},
|
},
|
||||||
theme: config.theme,
|
theme: config.theme,
|
||||||
isUtc: dashboard.isTimezoneUtc && dashboard.isTimezoneUtc(),
|
isUtc: dashboard.isTimezoneUtc && dashboard.isTimezoneUtc(),
|
||||||
|
@ -205,6 +205,21 @@ describe('SingleStatCtrl', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
singleStatScenario('When mapping null values and no data', (ctx: TestContext) => {
|
||||||
|
ctx.setup(() => {
|
||||||
|
ctx.input = []; // No data
|
||||||
|
ctx.ctrl.panel.valueMaps = [{ value: 'null', text: 'XYZ' }];
|
||||||
|
});
|
||||||
|
|
||||||
|
it('value should be null', () => {
|
||||||
|
expect(ctx.data.value).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should replace value with text', () => {
|
||||||
|
expect(ctx.data.display.text).toBe('XYZ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
singleStatScenario('When range to text mapping is specified for first range', (ctx: TestContext) => {
|
singleStatScenario('When range to text mapping is specified for first range', (ctx: TestContext) => {
|
||||||
ctx.setup(() => {
|
ctx.setup(() => {
|
||||||
ctx.input = [{ target: 'test.cpu1', datapoints: [[41, 50]] }];
|
ctx.input = [{ target: 'test.cpu1', datapoints: [[41, 50]] }];
|
||||||
|
Loading…
Reference in New Issue
Block a user