Skip to content

Commit d387f4c

Browse files
committed
Issue #34: add enhanceDataFrameWithDataLinks
1 parent 31f68a9 commit d387f4c

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
@@ -816,6 +821,46 @@ function luceneEscape(value: string) {
816821
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1');
817822
}
818823

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

0 commit comments

Comments
 (0)