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

View File

@ -28,9 +28,18 @@ export function getFieldDisplayValuesProxy(
field = frame.fields[k]; field = frame.fields[k];
} }
if (!field) { if (!field) {
// 3. Match the title // 3. Match the config displayName
field = frame.fields.find((f) => key === f.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) { if (!field) {
return undefined; return undefined;
} }

View File

@ -134,7 +134,14 @@ export const getDataFrameVars = (dataFrames: DataFrame[]) => {
const suggestions: VariableSuggestion[] = []; const suggestions: VariableSuggestion[] = [];
const keys: KeyValue<true> = {}; const keys: KeyValue<true> = {};
for (const frame of 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 [];
}
const frame = dataFrames[0];
for (const field of frame.fields) { for (const field of frame.fields) {
const displayName = getFieldDisplayName(field, frame, dataFrames); const displayName = getFieldDisplayName(field, frame, dataFrames);
@ -159,7 +166,6 @@ export const getDataFrameVars = (dataFrames: DataFrame[]) => {
title = { ...field, name: displayName }; title = { ...field, name: displayName };
} }
} }
}
if (suggestions.length) { if (suggestions.length) {
suggestions.push({ suggestions.push({

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([]);
});
});
}); });