mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: Fix handling multiple datalinks for a single field (#44029)
This commit is contained in:
parent
9f404daf9f
commit
cb27c9cd6f
@ -955,28 +955,53 @@ describe('enhanceDataFrame', () => {
|
|||||||
field: 'urlField',
|
field: 'urlField',
|
||||||
url: 'someUrl',
|
url: 'someUrl',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
field: 'urlField',
|
||||||
|
url: 'someOtherUrl',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
field: 'traceField',
|
field: 'traceField',
|
||||||
url: 'query',
|
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).toHaveLength(2);
|
||||||
expect(df.fields[0].config.links![0]).toEqual({
|
expect(df.fields[0].config.links).toContainEqual({
|
||||||
title: '',
|
title: '',
|
||||||
url: 'someUrl',
|
url: 'someUrl',
|
||||||
});
|
});
|
||||||
expect(df.fields[1].config.links!.length).toBe(1);
|
expect(df.fields[0].config.links).toContainEqual({
|
||||||
expect(df.fields[1].config.links![0]).toEqual({
|
|
||||||
title: '',
|
title: '',
|
||||||
url: '',
|
url: 'someOtherUrl',
|
||||||
internal: {
|
|
||||||
query: { query: 'query' },
|
|
||||||
datasourceName: 'elastic25',
|
|
||||||
datasourceUid: 'dsUid',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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', () => {
|
it('adds limit to dataframe', () => {
|
||||||
|
@ -936,8 +936,6 @@ export class ElasticDatasource
|
|||||||
* Exported for tests.
|
* Exported for tests.
|
||||||
*/
|
*/
|
||||||
export function enhanceDataFrame(dataFrame: DataFrame, dataLinks: DataLinkConfig[], limit?: number) {
|
export function enhanceDataFrame(dataFrame: DataFrame, dataLinks: DataLinkConfig[], limit?: number) {
|
||||||
const dataSourceSrv = getDataSourceSrv();
|
|
||||||
|
|
||||||
if (limit) {
|
if (limit) {
|
||||||
dataFrame.meta = {
|
dataFrame.meta = {
|
||||||
...dataFrame.meta,
|
...dataFrame.meta,
|
||||||
@ -950,35 +948,37 @@ export function enhanceDataFrame(dataFrame: DataFrame, dataLinks: DataLinkConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const field of dataFrame.fields) {
|
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;
|
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 = 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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user