Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.5.0",
"@types/lodash": "^4.14.194",
"@types/lucene": "^2.1.7",
"@types/node": "^18.15.11",
"@types/react": "17.0.42",
"@types/react-dom": "17.0.14",
Expand Down Expand Up @@ -66,6 +67,7 @@
"@grafana/runtime": "9.5.14",
"@grafana/ui": "9.5.14",
"@reduxjs/toolkit": "^1.9.5",
"lucene": "^2.1.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"tslib": "2.5.3"
Expand Down
38 changes: 31 additions & 7 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { catchError, mergeMap, map } from 'rxjs/operators';

import {
AbstractQuery,
AdHocVariableFilter,
CoreApp,
DataFrame,
DataLink,
Expand Down Expand Up @@ -50,6 +51,7 @@ import { isBucketAggregationWithField } from 'components/QueryEditor/BucketAggre
import ElasticsearchLanguageProvider from 'LanguageProvider';
import { ReactNode } from 'react';
import { extractJsonPayload, fieldTypeMap } from 'utils';
import { addAddHocFilter } from 'modifyQuery';

export const REF_ID_STARTER_LOG_VOLUME = 'log-volume-';

Expand Down Expand Up @@ -371,8 +373,11 @@ export class QuickwitDataSource
return from(this.getResource('_elastic/' + this.index + '/_field_caps')).pipe(
map((field_capabilities_response: FieldCapabilitiesResponse) => {
const shouldAddField = (field: any) => {
if (aggregatable === undefined) {
return true;
}
if (aggregatable !== undefined && field.aggregatable !== aggregatable) {
return false
return false;
}
if (type?.length === 0) {
return true;
Expand All @@ -394,7 +399,12 @@ export class QuickwitDataSource
value: fieldTypeMap[field_capability.type],
}
});
return fieldCapabilities;
const uniquefieldCapabilities = fieldCapabilities.filter((field_capability, index, self) =>
index === self.findIndex((t) => (
t.text === field_capability.text && t.value === field_capability.value
))
).sort((a, b) => a.text.localeCompare(b.text));
return uniquefieldCapabilities;
})
);
}
Expand All @@ -403,7 +413,8 @@ export class QuickwitDataSource
* Get tag keys for adhoc filters
*/
getTagKeys() {
return lastValueFrom(this.getFields(true));
console.log("getTagKeys");
return lastValueFrom(this.getFields());
}

/**
Expand Down Expand Up @@ -547,6 +558,7 @@ export class QuickwitDataSource
}

metricFindQuery(query: string, options?: { range: TimeRange }): Promise<MetricFindValue[]> {
console.log("metricFindQuery");
const range = options?.range;
const parsedQuery = JSON.parse(query);
if (query) {
Expand All @@ -567,12 +579,12 @@ export class QuickwitDataSource
return this.templateSrv.replace(queryString, scopedVars, formatQuery);
}

interpolateVariablesInQueries(queries: ElasticsearchQuery[], scopedVars: ScopedVars | {}): ElasticsearchQuery[] {
return queries.map((q) => this.applyTemplateVariables(q, scopedVars));
interpolateVariablesInQueries(queries: ElasticsearchQuery[], scopedVars: ScopedVars | {}, filters?: AdHocVariableFilter[]): ElasticsearchQuery[] {
return queries.map((q) => this.applyTemplateVariables(q, scopedVars, filters));
}

// Used when running queries through backend
applyTemplateVariables(query: ElasticsearchQuery, scopedVars: ScopedVars): ElasticsearchQuery {
applyTemplateVariables(query: ElasticsearchQuery, scopedVars: ScopedVars, filters?: AdHocVariableFilter[]): ElasticsearchQuery {
// We need a separate interpolation format for lucene queries, therefore we first interpolate any
// lucene query string and then everything else
const interpolateBucketAgg = (bucketAgg: BucketAggregation): BucketAggregation => {
Expand All @@ -595,13 +607,25 @@ export class QuickwitDataSource
const expandedQuery = {
...query,
datasource: this.getRef(),
query: this.interpolateLuceneQuery(query.query || '', scopedVars),
query: this.addAdHocFilters(this.interpolateLuceneQuery(query.query || '', scopedVars), filters),
bucketAggs: query.bucketAggs?.map(interpolateBucketAgg),
};

const finalQuery = JSON.parse(this.templateSrv.replace(JSON.stringify(expandedQuery), scopedVars));
return finalQuery;
}

addAdHocFilters(query: string, adhocFilters?: AdHocVariableFilter[]) {
if (!adhocFilters) {
return query;
}
let finalQuery = query;
adhocFilters.forEach((filter) => {
finalQuery = addAddHocFilter(finalQuery, filter);
});

return finalQuery;
}
}

// Returns a flatten array of fields and nested fields found in the given `FieldMapping` array.
Expand Down
Loading