Skip to content

Commit

Permalink
Issue #34: add enhanceDataFrameWithDataLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 8, 2023
1 parent c496ec5 commit 1dc167b
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AbstractQuery,
CoreApp,
DataFrame,
DataLink,
DataQueryError,
DataQueryRequest,
DataQueryResponse,
Expand All @@ -32,7 +33,11 @@ import {
TimeRange,
} from '@grafana/data';
import { BucketAggregation, DataLinkConfig, ElasticsearchQuery, Field, FieldMapping, IndexMetadata, Logs, TermsQuery } from './types';
import { DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
import {
DataSourceWithBackend,
getTemplateSrv,
TemplateSrv,
getDataSourceSrv } from '@grafana/runtime';
import { LogRowContextOptions, LogRowContextQueryDirection, QuickwitOptions } from 'quickwit';
import { ElasticQueryBuilder } from 'QueryBuilder';
import { colors } from '@grafana/ui';
Expand Down Expand Up @@ -83,15 +88,15 @@ export class QuickwitDataSource
this.languageProvider = new ElasticsearchLanguageProvider(this);
}

// /**
// * Ideally final -- any other implementation may not work as expected
// */
// query(request: DataQueryRequest<ElasticsearchQuery>): Observable<DataQueryResponse> {
// return super.query(request)
// .pipe(map((response) => {
// return response;
// }));
// }
query(request: DataQueryRequest<ElasticsearchQuery>): Observable<DataQueryResponse> {
return super.query(request)
.pipe(map((response) => {
response.data.forEach((dataFrame) => {
enhanceDataFrameWithDataLinks(dataFrame, this.dataLinks);
});
return response;
}));
}

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

export function enhanceDataFrameWithDataLinks(dataFrame: DataFrame, dataLinks: DataLinkConfig[]) {
if (!dataLinks.length) {
return;
}

for (const field of dataFrame.fields) {
const linksToApply = dataLinks.filter((dataLink) => new RegExp(dataLink.field).test(field.name));

if (linksToApply.length === 0) {
continue;
}

field.config = field.config || {};
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,
};
}
}

function createContextTimeRange(rowTimeEpochMs: number, direction: string) {
const offset = 7;
// For log context, we want to request data from 7 subsequent/previous indices
Expand All @@ -832,4 +877,3 @@ function createContextTimeRange(rowTimeEpochMs: number, direction: string) {
};
}
}

0 comments on commit 1dc167b

Please sign in to comment.