DataFrame: Add meta indicating comparison series (#71129)

* DataFrame: Add meta indicating comparison series

* Update metadata type

* Update test
This commit is contained in:
Dominik Prokop 2023-08-25 12:01:47 +02:00 committed by GitHub
parent 57960148e4
commit c060d5c341
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 3 deletions

View File

@ -15,6 +15,47 @@ function checkScenario(scenario: TitleScenario): string {
return getFieldDisplayName(field, frame, scenario.frames);
}
describe('getFieldDisplayName', () => {
it('Should add suffix for comparison frames', () => {
const frame = toDataFrame({
meta: {
timeCompare: {
diffMs: -86400000,
isTimeShiftQuery: true,
},
},
fields: [
{ name: TIME_SERIES_TIME_FIELD_NAME, values: [1, 2, 3], type: FieldType.time },
{
name: 'Value 1',
values: [1, 2, 3],
type: FieldType.number,
config: {
displayName: 'ServerA',
},
},
{
name: 'Value 2',
values: [1, 2, 3],
type: FieldType.number,
config: {
displayNameFromDS: 'ServerB',
},
},
{
name: 'Value 3',
values: [1, 2, 3],
type: FieldType.number,
},
],
});
expect(getFieldDisplayName(frame.fields[1], frame)).toBe('ServerA (comparison)');
expect(getFieldDisplayName(frame.fields[2], frame)).toBe('ServerB (comparison)');
expect(getFieldDisplayName(frame.fields[3], frame)).toBe('Value 3 (comparison)');
});
});
describe('getFrameDisplayName', () => {
it('Should return frame name if set', () => {
const frame = toDataFrame({

View File

@ -72,15 +72,15 @@ export function getFieldDisplayName(field: Field, frame?: DataFrame, allFrames?:
*/
export function calculateFieldDisplayName(field: Field, frame?: DataFrame, allFrames?: DataFrame[]): string {
const hasConfigTitle = field.config?.displayName && field.config?.displayName.length;
const isComparisonSeries = Boolean(frame?.meta?.timeCompare?.isTimeShiftQuery);
let displayName = hasConfigTitle ? field.config!.displayName! : field.name;
if (hasConfigTitle) {
return displayName;
return isComparisonSeries ? `${displayName} (comparison)` : displayName;
}
if (frame && field.config?.displayNameFromDS) {
return field.config.displayNameFromDS;
return isComparisonSeries ? `${field.config.displayNameFromDS} (comparison)` : field.config.displayNameFromDS;
}
// This is an ugly exception for time field
@ -151,6 +151,9 @@ export function calculateFieldDisplayName(field: Field, frame?: DataFrame, allFr
displayName = getUniqueFieldName(field, frame);
}
if (isComparisonSeries) {
displayName = `${displayName} (comparison)`;
}
return displayName;
}

View File

@ -94,6 +94,12 @@ export interface QueryResultMeta {
*/
pathSeparator?: string;
/** A time shift metadata indicating a result of comparison */
timeCompare?: {
diffMs: number;
isTimeShiftQuery: boolean;
};
/**
* Legacy data source specific, should be moved to custom
* */