Elasticsearch: Fix handling multiple datalinks for a single field (#44029)

This commit is contained in:
Giordano Ricci 2022-01-14 14:05:23 +00:00 committed by GitHub
parent 9f404daf9f
commit cb27c9cd6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 37 deletions

View File

@ -955,28 +955,53 @@ describe('enhanceDataFrame', () => {
field: 'urlField',
url: 'someUrl',
},
{
field: 'urlField',
url: 'someOtherUrl',
},
{
field: 'traceField',
url: 'query',
datasourceUid: 'dsUid',
datasourceUid: 'ds1',
},
{
field: 'traceField',
url: 'otherQuery',
datasourceUid: 'ds2',
},
]);
expect(df.fields[0].config.links!.length).toBe(1);
expect(df.fields[0].config.links![0]).toEqual({
expect(df.fields[0].config.links).toHaveLength(2);
expect(df.fields[0].config.links).toContainEqual({
title: '',
url: 'someUrl',
});
expect(df.fields[1].config.links!.length).toBe(1);
expect(df.fields[1].config.links![0]).toEqual({
expect(df.fields[0].config.links).toContainEqual({
title: '',
url: '',
internal: {
query: { query: 'query' },
datasourceName: 'elastic25',
datasourceUid: 'dsUid',
},
url: 'someOtherUrl',
});
expect(df.fields[1].config.links).toHaveLength(2);
expect(df.fields[1].config.links).toContainEqual(
expect.objectContaining({
title: '',
url: '',
internal: expect.objectContaining({
query: { query: 'query' },
datasourceUid: 'ds1',
}),
})
);
expect(df.fields[1].config.links).toContainEqual(
expect.objectContaining({
title: '',
url: '',
internal: expect.objectContaining({
query: { query: 'otherQuery' },
datasourceUid: 'ds2',
}),
})
);
});
it('adds limit to dataframe', () => {

View File

@ -936,8 +936,6 @@ export class ElasticDatasource
* Exported for tests.
*/
export function enhanceDataFrame(dataFrame: DataFrame, dataLinks: DataLinkConfig[], limit?: number) {
const dataSourceSrv = getDataSourceSrv();
if (limit) {
dataFrame.meta = {
...dataFrame.meta,
@ -950,35 +948,37 @@ export function enhanceDataFrame(dataFrame: DataFrame, dataLinks: DataLinkConfig
}
for (const field of dataFrame.fields) {
const dataLinkConfig = dataLinks.find((dataLink) => field.name && field.name.match(dataLink.field));
const linksToApply = dataLinks.filter((dataLink) => new RegExp(dataLink.field).test(field.name));
if (!dataLinkConfig) {
if (linksToApply.length === 0) {
continue;
}
let link: DataLink;
if (dataLinkConfig.datasourceUid) {
const dsSettings = dataSourceSrv.getInstanceSettings(dataLinkConfig.datasourceUid);
link = {
title: dataLinkConfig.urlDisplayLabel || '',
url: '',
internal: {
query: { query: dataLinkConfig.url },
datasourceUid: dataLinkConfig.datasourceUid,
datasourceName: dsSettings?.name ?? 'Data source not found',
},
};
} else {
link = {
title: dataLinkConfig.urlDisplayLabel || '',
url: dataLinkConfig.url,
};
}
field.config = field.config || {};
field.config.links = [...(field.config.links || []), link];
field.config.links = [...(field.config.links || [], linksToApply.map(generateDataLink))];
}
}
function generateDataLink(linkConfig: DataLinkConfig): DataLink {
const dataSourceSrv = getDataSourceSrv();
if (linkConfig.datasourceUid) {
const dsSettings = dataSourceSrv.getInstanceSettings(linkConfig.datasourceUid);
return {
title: linkConfig.urlDisplayLabel || '',
url: '',
internal: {
query: { query: linkConfig.url },
datasourceUid: linkConfig.datasourceUid,
datasourceName: dsSettings?.name ?? 'Data source not found',
},
};
} else {
return {
title: linkConfig.urlDisplayLabel || '',
url: linkConfig.url,
};
}
}