Transformers: fixes column names generated by seriesToColumn transformer (#23642)

This commit is contained in:
Hugo Häggmark 2020-04-17 09:33:56 +02:00 committed by GitHub
parent 6c021f7bb9
commit 0205c42bfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import {
ArrayVector,
DataTransformerConfig,
DataTransformerID,
Field,
FieldType,
toDataFrame,
transformDataFrame,
@ -178,4 +179,58 @@ describe('SeriesToColumns Transformer', () => {
},
]);
});
describe('Field names', () => {
const seriesWithSameFieldAndDataFrameName = toDataFrame({
name: 'temperature',
fields: [
{ name: 'time', type: FieldType.time, values: [1000, 2000, 3000, 4000] },
{ name: 'temperature', type: FieldType.number, values: [1, 3, 5, 7] },
],
});
const seriesB = toDataFrame({
name: 'B',
fields: [
{ name: 'time', type: FieldType.time, values: [1000, 2000, 3000, 4000] },
{ name: 'temperature', type: FieldType.number, values: [2, 4, 6, 8] },
],
});
it('when dataframe and field share the same name then use the field name', () => {
const cfg: DataTransformerConfig<SeriesToColumnsOptions> = {
id: DataTransformerID.seriesToColumns,
options: {
byField: 'time',
},
};
const filtered = transformDataFrame([cfg], [seriesWithSameFieldAndDataFrameName, seriesB])[0];
const expected: Field[] = [
{
name: 'time',
type: FieldType.time,
values: new ArrayVector([1000, 2000, 3000, 4000]),
config: {},
labels: { origin: 'temperature,B' },
},
{
name: 'temperature',
type: FieldType.number,
values: new ArrayVector([1, 3, 5, 7]),
config: {},
labels: { origin: 'temperature' },
},
{
name: 'temperature {B}',
type: FieldType.number,
values: new ArrayVector([2, 4, 6, 8]),
config: {},
labels: { origin: 'B' },
},
];
expect(filtered.fields).toEqual(expected);
});
});
});

View File

@ -111,9 +111,10 @@ export const seriesToColumnsTransformer: DataTransformerInfo<SeriesToColumnsOpti
const getColumnName = (frames: DataFrame[], frameIndex: number, fieldIndex: number, isKeyField = false) => {
const frame = frames[frameIndex];
const field = frame.fields[fieldIndex];
const frameName = frame.name || `${frameIndex}`;
const fieldName = frame.fields[fieldIndex].name;
const seriesName = isKeyField ? fieldName : `${fieldName} {${frameName}}`;
const fieldName = field.name;
const seriesName = isKeyField ? fieldName : fieldName === frameName ? fieldName : `${fieldName} {${frameName}}`;
return seriesName;
};