Remove datalink template suggestions for accessing specific fields when there are multiple dataframes. (#32057)

* Don't suggest template strings using fields when there are mutliple dataframes

* Change to use label instead of state
This commit is contained in:
Oscar Kilhed 2021-03-19 09:28:15 +01:00 committed by GitHub
parent 8854001b67
commit f48a52e590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 20 deletions

View File

@ -13,6 +13,9 @@ describe('getFieldDisplayValuesProxy', () => {
{
name: 'power',
values: [100, 200, 300],
labels: {
name: 'POWAH!',
},
config: {
displayName: 'The Power',
},
@ -60,6 +63,7 @@ describe('getFieldDisplayValuesProxy', () => {
});
expect(p.power.numeric).toEqual(300);
expect(p['power'].numeric).toEqual(300);
expect(p['POWAH!'].numeric).toEqual(300);
expect(p['The Power'].numeric).toEqual(300);
expect(p[1].numeric).toEqual(300);
});

View File

@ -28,9 +28,18 @@ export function getFieldDisplayValuesProxy(
field = frame.fields[k];
}
if (!field) {
// 3. Match the title
// 3. Match the config displayName
field = frame.fields.find((f) => key === f.config.displayName);
}
if (!field) {
// 4. Match the name label
field = frame.fields.find((f) => {
if (f.labels) {
return key === f.labels.name;
}
return false;
});
}
if (!field) {
return undefined;
}

View File

@ -134,30 +134,36 @@ export const getDataFrameVars = (dataFrames: DataFrame[]) => {
const suggestions: VariableSuggestion[] = [];
const keys: KeyValue<true> = {};
for (const frame of dataFrames) {
for (const field of frame.fields) {
const displayName = getFieldDisplayName(field, frame, dataFrames);
if (dataFrames.length !== 1) {
// It's not possible to access fields of other dataframes. So if there are multiple dataframes we need to skip these suggestions.
// Also return early if there are no dataFrames.
return [];
}
if (keys[displayName]) {
continue;
}
const frame = dataFrames[0];
suggestions.push({
value: `__data.fields${buildLabelPath(displayName)}`,
label: `${displayName}`,
documentation: `Formatted value for ${displayName} on the same row`,
origin: VariableOrigin.Fields,
});
for (const field of frame.fields) {
const displayName = getFieldDisplayName(field, frame, dataFrames);
keys[displayName] = true;
if (keys[displayName]) {
continue;
}
if (!numeric && field.type === FieldType.number) {
numeric = { ...field, name: displayName };
}
suggestions.push({
value: `__data.fields${buildLabelPath(displayName)}`,
label: `${displayName}`,
documentation: `Formatted value for ${displayName} on the same row`,
origin: VariableOrigin.Fields,
});
if (!title && field.config.displayName && field.config.displayName !== field.name) {
title = { ...field, name: displayName };
}
keys[displayName] = true;
if (!numeric && field.type === FieldType.number) {
numeric = { ...field, name: displayName };
}
if (!title && field.config.displayName && field.config.displayName !== field.name) {
title = { ...field, name: displayName };
}
}

View File

@ -424,4 +424,28 @@ describe('getDataFrameVars', () => {
]);
});
});
describe('when called with multiple DataFrames', () => {
it('it should not return any suggestions', () => {
const frame1 = toDataFrame({
name: 'server1',
fields: [
{ name: 'time', type: FieldType.time, values: [1, 2, 3] },
{ name: 'value', type: FieldType.number, values: [10, 11, 12] },
],
});
const frame2 = toDataFrame({
name: 'server2',
fields: [
{ name: 'time', type: FieldType.time, values: [1, 2, 3] },
{ name: 'value', type: FieldType.number, values: [10, 11, 12] },
],
});
const suggestions = getDataFrameVars([frame1, frame2]);
expect(suggestions).toEqual([]);
});
});
});