Skip to content

Commit 1dc167b

Browse files
committed
Issue #34: add enhanceDataFrameWithDataLinks
1 parent c496ec5 commit 1dc167b

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

src/datasource.ts

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
AbstractQuery,
77
CoreApp,
88
DataFrame,
9+
DataLink,
910
DataQueryError,
1011
DataQueryRequest,
1112
DataQueryResponse,
@@ -32,7 +33,11 @@ import {
3233
TimeRange,
3334
} from '@grafana/data';
3435
import { BucketAggregation, DataLinkConfig, ElasticsearchQuery, Field, FieldMapping, IndexMetadata, Logs, TermsQuery } from './types';
35-
import { DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
36+
import {
37+
DataSourceWithBackend,
38+
getTemplateSrv,
39+
TemplateSrv,
40+
getDataSourceSrv } from '@grafana/runtime';
3641
import { LogRowContextOptions, LogRowContextQueryDirection, QuickwitOptions } from 'quickwit';
3742
import { ElasticQueryBuilder } from 'QueryBuilder';
3843
import { colors } from '@grafana/ui';
@@ -83,15 +88,15 @@ export class QuickwitDataSource
8388
this.languageProvider = new ElasticsearchLanguageProvider(this);
8489
}
8590

86-
// /**
87-
// * Ideally final -- any other implementation may not work as expected
88-
// */
89-
// query(request: DataQueryRequest<ElasticsearchQuery>): Observable<DataQueryResponse> {
90-
// return super.query(request)
91-
// .pipe(map((response) => {
92-
// return response;
93-
// }));
94-
// }
91+
query(request: DataQueryRequest<ElasticsearchQuery>): Observable<DataQueryResponse> {
92+
return super.query(request)
93+
.pipe(map((response) => {
94+
response.data.forEach((dataFrame) => {
95+
enhanceDataFrameWithDataLinks(dataFrame, this.dataLinks);
96+
});
97+
return response;
98+
}));
99+
}
95100

96101
/**
97102
* Checks the plugin health
@@ -817,6 +822,46 @@ function luceneEscape(value: string) {
817822
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1');
818823
}
819824

825+
export function enhanceDataFrameWithDataLinks(dataFrame: DataFrame, dataLinks: DataLinkConfig[]) {
826+
if (!dataLinks.length) {
827+
return;
828+
}
829+
830+
for (const field of dataFrame.fields) {
831+
const linksToApply = dataLinks.filter((dataLink) => new RegExp(dataLink.field).test(field.name));
832+
833+
if (linksToApply.length === 0) {
834+
continue;
835+
}
836+
837+
field.config = field.config || {};
838+
field.config.links = [...(field.config.links || [], linksToApply.map(generateDataLink))];
839+
}
840+
}
841+
842+
function generateDataLink(linkConfig: DataLinkConfig): DataLink {
843+
const dataSourceSrv = getDataSourceSrv();
844+
845+
if (linkConfig.datasourceUid) {
846+
const dsSettings = dataSourceSrv.getInstanceSettings(linkConfig.datasourceUid);
847+
848+
return {
849+
title: linkConfig.urlDisplayLabel || '',
850+
url: '',
851+
internal: {
852+
query: { query: linkConfig.url },
853+
datasourceUid: linkConfig.datasourceUid,
854+
datasourceName: dsSettings?.name ?? 'Data source not found',
855+
},
856+
};
857+
} else {
858+
return {
859+
title: linkConfig.urlDisplayLabel || '',
860+
url: linkConfig.url,
861+
};
862+
}
863+
}
864+
820865
function createContextTimeRange(rowTimeEpochMs: number, direction: string) {
821866
const offset = 7;
822867
// For log context, we want to request data from 7 subsequent/previous indices
@@ -832,4 +877,3 @@ function createContextTimeRange(rowTimeEpochMs: number, direction: string) {
832877
};
833878
}
834879
}
835-

0 commit comments

Comments
 (0)