Alerting: Use displayNameFromDS if available in preview (#65342)

This commit is contained in:
Gilles De Mey 2023-03-28 13:58:21 +02:00 committed by GitHub
parent b2ab57d14b
commit f68fd83951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 12 deletions

View File

@ -135,21 +135,13 @@ interface ExpressionResultProps {
} }
export const PAGE_SIZE = 20; export const PAGE_SIZE = 20;
export const ExpressionResult: FC<ExpressionResultProps> = ({ series, isAlertCondition }) => { export const ExpressionResult: FC<ExpressionResultProps> = ({ series, isAlertCondition }) => {
const { page, pageItems, onPageChange, numberOfPages, pageStart, pageEnd } = usePagination(series, 1, PAGE_SIZE); const { pageItems, previousPage, nextPage, numberOfPages, pageStart, pageEnd } = usePagination(series, 1, PAGE_SIZE);
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
// sometimes we receive results where every value is just "null" when noData occurs // sometimes we receive results where every value is just "null" when noData occurs
const emptyResults = isEmptySeries(series); const emptyResults = isEmptySeries(series);
const isTimeSeriesResults = !emptyResults && isTimeSeriesFrames(series); const isTimeSeriesResults = !emptyResults && isTimeSeriesFrames(series);
const previousPage = useCallback(() => {
onPageChange(page - 1);
}, [page, onPageChange]);
const nextPage = useCallback(() => {
onPageChange(page + 1);
}, [page, onPageChange]);
const shouldShowPagination = numberOfPages > 1; const shouldShowPagination = numberOfPages > 1;
return ( return (
@ -331,8 +323,11 @@ const FrameRow: FC<FrameProps> = ({ frame, index, isAlertCondition }) => {
const TimeseriesRow: FC<FrameProps & { index: number }> = ({ frame, index }) => { const TimeseriesRow: FC<FrameProps & { index: number }> = ({ frame, index }) => {
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
const hasLabels = frame.fields[1].labels; const valueField = frame.fields[1]; // field 0 is "time", field 1 is "value"
const name = hasLabels ? formatLabels(frame.fields[1].labels ?? {}) : 'Series ' + index;
const hasLabels = valueField.labels;
const displayNameFromDS = valueField.config?.displayNameFromDS;
const name = displayNameFromDS ?? (hasLabels ? formatLabels(valueField.labels ?? {}) : 'Series ' + index);
const timestamps = frame.fields[0].values.toArray(); const timestamps = frame.fields[0].values.toArray();

View File

@ -62,6 +62,25 @@ describe('getSeriesName', () => {
it('should work with NoData frames', () => { it('should work with NoData frames', () => {
expect(getSeriesName(EMPTY_FRAME)).toBe(''); expect(getSeriesName(EMPTY_FRAME)).toBe('');
}); });
it('should give preference to displayNameFromDS', () => {
const frame: DataFrame = {
name: 'MyFrame',
...toDataFrame({
fields: [
{
name: 'value',
type: FieldType.number,
values: [1, 2, 3],
labels: { foo: 'bar' },
config: { displayNameFromDS: 'series-name-override' },
},
],
}),
};
expect(getSeriesName(frame)).toBe('series-name-override');
});
}); });
describe('getSeriesValue', () => { describe('getSeriesValue', () => {

View File

@ -10,7 +10,10 @@ import { DataFrame, Labels, roundDecimals } from '@grafana/data';
*/ */
const getSeriesName = (frame: DataFrame): string => { const getSeriesName = (frame: DataFrame): string => {
return frame.name ?? formatLabels(frame.fields[0]?.labels ?? {}); const firstField = frame.fields[0];
const displayNameFromDS = firstField?.config?.displayNameFromDS;
return displayNameFromDS ?? frame.name ?? formatLabels(firstField?.labels ?? {});
}; };
const getSeriesValue = (frame: DataFrame) => { const getSeriesValue = (frame: DataFrame) => {