Skip to content

Commit deddaba

Browse files
committed
Improve datasource getFields
1 parent f040649 commit deddaba

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/datasource.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export const REF_ID_STARTER_LOG_VOLUME = 'log-volume-';
5757

5858
export type ElasticDatasource = QuickwitDataSource;
5959

60+
type FieldCapsSpec = {
61+
aggregatable?: boolean,
62+
searchable?: boolean,
63+
type?: string[],
64+
_range?: TimeRange
65+
}
66+
6067
export class QuickwitDataSource
6168
extends DataSourceWithBackend<ElasticsearchQuery, QuickwitOptions>
6269
implements
@@ -368,21 +375,21 @@ export class QuickwitDataSource
368375
);
369376
}
370377

371-
getFields(aggregatable?: boolean, type?: string[], _range?: TimeRange): Observable<MetricFindValue[]> {
378+
getFields(spec: FieldCapsSpec={}): Observable<MetricFindValue[]> {
372379
// TODO: use the time range.
373380
return from(this.getResource('_elastic/' + this.index + '/_field_caps')).pipe(
374381
map((field_capabilities_response: FieldCapabilitiesResponse) => {
375382
const shouldAddField = (field: any) => {
376-
if (aggregatable === undefined) {
377-
return true;
383+
if (spec.aggregatable !== undefined && field.aggregatable !== spec.aggregatable) {
384+
return false
378385
}
379-
if (aggregatable !== undefined && field.aggregatable !== aggregatable) {
380-
return false;
386+
if (spec.searchable !== undefined && field.searchable !== spec.searchable){
387+
return false
381388
}
382-
if (type?.length === 0) {
383-
return true;
389+
if (spec.type && spec.type.length !== 0 && !(spec.type.includes(field.type) || spec.type.includes(fieldTypeMap[field.type]))) {
390+
return false
384391
}
385-
return type?.includes(field.type) || type?.includes(fieldTypeMap[field.type]);
392+
return true
386393
};
387394
const fieldCapabilities = Object.entries(field_capabilities_response.fields)
388395
.flatMap(([field_name, field_capabilities]) => {
@@ -412,8 +419,8 @@ export class QuickwitDataSource
412419
/**
413420
* Get tag keys for adhoc filters
414421
*/
415-
getTagKeys() {
416-
return lastValueFrom(this.getFields());
422+
getTagKeys(spec?: FieldCapsSpec) {
423+
return lastValueFrom(this.getFields(spec));
417424
}
418425

419426
/**
@@ -562,7 +569,7 @@ export class QuickwitDataSource
562569
if (query) {
563570
if (parsedQuery.find === 'fields') {
564571
parsedQuery.type = this.interpolateLuceneQuery(parsedQuery.type);
565-
return lastValueFrom(this.getFields(true, parsedQuery.type, range));
572+
return lastValueFrom(this.getFields({aggregatable:true, type:parsedQuery.type, _range:range}));
566573
}
567574
if (parsedQuery.find === 'terms') {
568575
parsedQuery.field = this.interpolateLuceneQuery(parsedQuery.field);

src/hooks/useFields.test.tsx

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { ElasticsearchQuery, MetricAggregationType, BucketAggregationType } from
1111
import { useFields } from './useFields';
1212
import { renderHook } from '@testing-library/react-hooks';
1313

14+
15+
1416
describe('useFields hook', () => {
1517
// TODO: If we move the field type to the configuration objects as described in the hook's source
1618
// we can stop testing for getField to be called with the correct parameters.
@@ -48,39 +50,39 @@ describe('useFields hook', () => {
4850
{ wrapper, initialProps: 'cardinality' }
4951
);
5052
result.current();
51-
expect(getFields).toHaveBeenLastCalledWith(true, [], timeRange);
53+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:[], _range:timeRange});
5254

5355
// All other metric aggregations only work on numbers
5456
rerender('avg');
5557
result.current();
56-
expect(getFields).toHaveBeenLastCalledWith(true, ['number'], timeRange);
58+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], _range:timeRange});
5759

5860
//
5961
// BUCKET AGGREGATIONS
6062
//
6163
// Date Histrogram only works on dates
6264
rerender('date_histogram');
6365
result.current();
64-
expect(getFields).toHaveBeenLastCalledWith(true, ['date'], timeRange);
66+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['date'], _range:timeRange});
6567

6668
// Histrogram only works on numbers
6769
rerender('histogram');
6870
result.current();
69-
expect(getFields).toHaveBeenLastCalledWith(true, ['number'], timeRange);
71+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], _range:timeRange});
7072

7173
// Geohash Grid only works on geo_point data
7274
rerender('geohash_grid');
7375
result.current();
74-
expect(getFields).toHaveBeenLastCalledWith(true, ['geo_point'], timeRange);
76+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['geo_point'], _range:timeRange});
7577

7678
// All other bucket aggregation work on any kind of data
7779
rerender('terms');
7880
result.current();
79-
expect(getFields).toHaveBeenLastCalledWith(true, [], timeRange);
81+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:[], _range:timeRange});
8082

8183
// top_metrics work on only on numeric data in 7.7
8284
rerender('top_metrics');
8385
result.current();
84-
expect(getFields).toHaveBeenLastCalledWith(true, ['number'], timeRange);
86+
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], _range:timeRange});
8587
});
8688
});

src/hooks/useFields.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const useFields = (type: AggregationType | string[]) => {
6262
return async (q?: string) => {
6363
// _mapping doesn't support filtering, we avoid sending a request everytime q changes
6464
if (!rawFields) {
65-
rawFields = await lastValueFrom(datasource.getFields(true, filter, range));
65+
rawFields = await lastValueFrom(datasource.getFields({aggregatable:true, type:filter, _range:range}));
6666
}
6767

6868
return rawFields.filter(({ text }) => q === undefined || text.includes(q)).map(toSelectableValue);

0 commit comments

Comments
 (0)