Commit d097c7da by Torkel Ödegaard Committed by GitHub

FieldDisplayName: Make field display names unique if field name exists more than once (#27963)

* FieldDisplayName: Make field display names unique if fields with same name exists

* Updated tests

* Minor optimization

* Updated seriesToColumns test

* Fixed arrow data frame test
parent c70291de
......@@ -43,7 +43,7 @@ describe('Read/Write arrow Table to DataFrame', () => {
],
});
const table = grafanaDataFrameToArrowTable(frame);
const table = grafanaDataFrameToArrowTable(frame, true);
expect(table.length).toEqual(frame.length);
// Now back to DataFrame
......
......@@ -54,6 +54,40 @@ describe('Check field state calculations (displayName and id)', () => {
expect(title).toEqual('Series A Field 1');
});
it('should add field name count to name if it exists more than once and is equal to TIME_SERIES_VALUE_FIELD_NAME', () => {
const title = checkScenario({
frames: [
toDataFrame({
fields: [{ name: TIME_SERIES_VALUE_FIELD_NAME }, { name: TIME_SERIES_VALUE_FIELD_NAME }],
}),
],
});
const title2 = checkScenario({
frames: [
toDataFrame({
fields: [{ name: TIME_SERIES_VALUE_FIELD_NAME }, { name: TIME_SERIES_VALUE_FIELD_NAME }],
}),
],
fieldIndex: 1,
});
expect(title).toEqual('Value 1');
expect(title2).toEqual('Value 2');
});
it('should add field name count to name if field name exists more than once', () => {
const title2 = checkScenario({
frames: [
toDataFrame({
fields: [{ name: 'A' }, { name: 'A' }],
}),
],
fieldIndex: 1,
});
expect(title2).toEqual('A 2');
});
it('should only use label value if only one label', () => {
const title = checkScenario({
frames: [
......
......@@ -125,9 +125,46 @@ function calculateFieldDisplayName(field: Field, frame?: DataFrame, allFrames?:
displayName = TIME_SERIES_VALUE_FIELD_NAME;
}
// Ensure unique field name
if (displayName === field.name) {
displayName = getUniqueFieldName(field, frame);
}
return displayName;
}
function getUniqueFieldName(field: Field, frame?: DataFrame) {
let dupeCount = 0;
let foundSelf = false;
if (frame) {
for (let i = 0; i < frame.fields.length; i++) {
const otherField = frame.fields[i];
if (field === otherField) {
foundSelf = true;
if (dupeCount > 0) {
dupeCount++;
break;
}
} else if (field.name === otherField.name) {
dupeCount++;
if (foundSelf) {
break;
}
}
}
}
if (dupeCount) {
return `${field.name} ${dupeCount}`;
}
return field.name;
}
/**
* Checks all data frames and return name of label if there is only one label name in all frames
*/
......
......@@ -346,4 +346,55 @@ describe('SeriesToColumns Transformer', () => {
},
]);
});
it('handles duplicate field name', () => {
const cfg: DataTransformerConfig<SeriesToColumnsOptions> = {
id: DataTransformerID.seriesToColumns,
options: {
byField: 'time',
},
};
const frame1 = toDataFrame({
fields: [
{ name: 'time', type: FieldType.time, values: [1] },
{ name: 'temperature', type: FieldType.number, values: [10] },
],
});
const frame2 = toDataFrame({
fields: [
{ name: 'time', type: FieldType.time, values: [1] },
{ name: 'temperature', type: FieldType.number, values: [20] },
],
});
const filtered = transformDataFrame([cfg], [frame1, frame2])[0];
expect(filtered.fields).toEqual([
{
name: 'time',
state: { displayName: 'time' },
type: FieldType.time,
values: new ArrayVector([1]),
config: {},
},
{
name: 'temperature',
state: { displayName: 'temperature 1' },
type: FieldType.number,
values: new ArrayVector([10]),
config: {},
labels: {},
},
{
name: 'temperature',
state: { displayName: 'temperature 2' },
type: FieldType.number,
values: new ArrayVector([20]),
config: {},
labels: {},
},
]);
});
});
......@@ -68,7 +68,7 @@ export const seriesToColumnsTransformer: DataTransformerInfo<SeriesToColumnsOpti
});
for (const item of allFields) {
resultFrame.addField(item.newField);
item.newField = resultFrame.addField(item.newField);
}
const keyFieldTitle = getFieldDisplayName(resultFrame.fields[0], resultFrame);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment