elasticsearch: improved types in the typescript files (#56293)

pull/56317/head
Gábor Farkas 3 years ago committed by GitHub
parent 152c7f149a
commit 291276e767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 262
      public/app/plugins/datasource/elasticsearch/QueryBuilder.test.ts
  2. 8
      public/app/plugins/datasource/elasticsearch/QueryBuilder.ts

@ -48,14 +48,11 @@ describe('ElasticQueryBuilder', () => {
}); });
it('with select field', () => { it('with select field', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [{ type: 'avg', field: '@value', id: '1' }],
metrics: [{ type: 'avg', field: '@value', id: '1' }], bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }], });
},
100
);
const aggs = query.aggs['2'].aggs; const aggs = query.aggs['2'].aggs;
expect(aggs['1'].avg.field).toBe('@value'); expect(aggs['1'].avg.field).toBe('@value');
@ -79,32 +76,29 @@ describe('ElasticQueryBuilder', () => {
], ],
}; };
const query = builder.build(target, 100); const query = builder.build(target);
const firstLevel = query.aggs['2']; const firstLevel = query.aggs['2'];
expect(firstLevel.terms.order._key).toBe('asc'); expect(firstLevel.terms.order._key).toBe('asc');
}); });
it('with term agg and order by metric agg', () => { it('with term agg and order by metric agg', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [
metrics: [ { type: 'count', id: '1' },
{ type: 'count', id: '1' }, { type: 'avg', field: '@value', id: '5' },
{ type: 'avg', field: '@value', id: '5' }, ],
], bucketAggs: [
bucketAggs: [ {
{ type: 'terms',
type: 'terms', field: '@host',
field: '@host', settings: { size: '5', order: 'asc', orderBy: '5' },
settings: { size: '5', order: 'asc', orderBy: '5' }, id: '2',
id: '2', },
}, { type: 'date_histogram', field: '@timestamp', id: '3' },
{ type: 'date_histogram', field: '@timestamp', id: '3' }, ],
], });
},
100
);
const firstLevel = query.aggs['2']; const firstLevel = query.aggs['2'];
const secondLevel = firstLevel.aggs['3']; const secondLevel = firstLevel.aggs['3'];
@ -114,47 +108,41 @@ describe('ElasticQueryBuilder', () => {
}); });
it('with term agg and order by count agg', () => { it('with term agg and order by count agg', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [
metrics: [ { type: 'count', id: '1' },
{ type: 'count', id: '1' }, { type: 'avg', field: '@value', id: '5' },
{ type: 'avg', field: '@value', id: '5' }, ],
], bucketAggs: [
bucketAggs: [ {
{ type: 'terms',
type: 'terms', field: '@host',
field: '@host', settings: { size: '5', order: 'asc', orderBy: '1' },
settings: { size: '5', order: 'asc', orderBy: '1' }, id: '2',
id: '2', },
}, { type: 'date_histogram', field: '@timestamp', id: '3' },
{ type: 'date_histogram', field: '@timestamp', id: '3' }, ],
], });
},
100
);
expect(query.aggs['2'].terms.order._count).toEqual('asc'); expect(query.aggs['2'].terms.order._count).toEqual('asc');
expect(query.aggs['2'].aggs).not.toHaveProperty('1'); expect(query.aggs['2'].aggs).not.toHaveProperty('1');
}); });
it('with term agg and order by extended_stats agg', () => { it('with term agg and order by extended_stats agg', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [{ type: 'extended_stats', id: '1', field: '@value', meta: { std_deviation: true } }],
metrics: [{ type: 'extended_stats', id: '1', field: '@value', meta: { std_deviation: true } }], bucketAggs: [
bucketAggs: [ {
{ type: 'terms',
type: 'terms', field: '@host',
field: '@host', settings: { size: '5', order: 'asc', orderBy: '1[std_deviation]' },
settings: { size: '5', order: 'asc', orderBy: '1[std_deviation]' }, id: '2',
id: '2', },
}, { type: 'date_histogram', field: '@timestamp', id: '3' },
{ type: 'date_histogram', field: '@timestamp', id: '3' }, ],
], });
},
100
);
const firstLevel = query.aggs['2']; const firstLevel = query.aggs['2'];
const secondLevel = firstLevel.aggs['3']; const secondLevel = firstLevel.aggs['3'];
@ -164,22 +152,19 @@ describe('ElasticQueryBuilder', () => {
}); });
it('with term agg and order by percentiles agg', () => { it('with term agg and order by percentiles agg', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [{ type: 'percentiles', id: '1', field: '@value', settings: { percents: ['95', '99'] } }],
metrics: [{ type: 'percentiles', id: '1', field: '@value', settings: { percents: ['95', '99'] } }], bucketAggs: [
bucketAggs: [ {
{ type: 'terms',
type: 'terms', field: '@host',
field: '@host', settings: { size: '5', order: 'asc', orderBy: '1[95.0]' },
settings: { size: '5', order: 'asc', orderBy: '1[95.0]' }, id: '2',
id: '2', },
}, { type: 'date_histogram', field: '@timestamp', id: '3' },
{ type: 'date_histogram', field: '@timestamp', id: '3' }, ],
], });
},
100
);
const firstLevel = query.aggs['2']; const firstLevel = query.aggs['2'];
const secondLevel = firstLevel.aggs['3']; const secondLevel = firstLevel.aggs['3'];
@ -189,44 +174,38 @@ describe('ElasticQueryBuilder', () => {
}); });
it('with term agg and valid min_doc_count', () => { it('with term agg and valid min_doc_count', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [{ type: 'count', id: '1' }],
metrics: [{ type: 'count', id: '1' }], bucketAggs: [
bucketAggs: [ {
{ type: 'terms',
type: 'terms', field: '@host',
field: '@host', settings: { min_doc_count: '1' },
settings: { min_doc_count: '1' }, id: '2',
id: '2', },
}, { type: 'date_histogram', field: '@timestamp', id: '3' },
{ type: 'date_histogram', field: '@timestamp', id: '3' }, ],
], });
},
100
);
const firstLevel = query.aggs['2']; const firstLevel = query.aggs['2'];
expect(firstLevel.terms.min_doc_count).toBe(1); expect(firstLevel.terms.min_doc_count).toBe(1);
}); });
it('with term agg and variable as min_doc_count', () => { it('with term agg and variable as min_doc_count', () => {
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [{ type: 'count', id: '1' }],
metrics: [{ type: 'count', id: '1' }], bucketAggs: [
bucketAggs: [ {
{ type: 'terms',
type: 'terms', field: '@host',
field: '@host', settings: { min_doc_count: '$min_doc_count' },
settings: { min_doc_count: '$min_doc_count' }, id: '2',
id: '2', },
}, { type: 'date_histogram', field: '@timestamp', id: '3' },
{ type: 'date_histogram', field: '@timestamp', id: '3' }, ],
], });
},
100
);
const firstLevel = query.aggs['2']; const firstLevel = query.aggs['2'];
expect(firstLevel.terms.min_doc_count).toBe('$min_doc_count'); expect(firstLevel.terms.min_doc_count).toBe('$min_doc_count');
@ -236,23 +215,20 @@ describe('ElasticQueryBuilder', () => {
const percents = ['1', '2', '3', '4']; const percents = ['1', '2', '3', '4'];
const field = '@load_time'; const field = '@load_time';
const query = builder.build( const query = builder.build({
{ refId: 'A',
refId: 'A', metrics: [
metrics: [ {
{ id: '1',
id: '1', type: 'percentiles',
type: 'percentiles', field,
field, settings: {
settings: { percents,
percents,
},
}, },
], },
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }], ],
}, bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }],
100 });
);
const firstLevel = query.aggs['3']; const firstLevel = query.aggs['3'];
@ -620,13 +596,13 @@ describe('ElasticQueryBuilder', () => {
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }], bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }],
}, },
[ [
{ key: 'key1', operator: '=', value: 'value1' }, { key: 'key1', operator: '=', value: 'value1', condition: '' },
{ key: 'key2', operator: '=', value: 'value2' }, { key: 'key2', operator: '=', value: 'value2', condition: '' },
{ key: 'key2', operator: '!=', value: 'value2' }, { key: 'key2', operator: '!=', value: 'value2', condition: '' },
{ key: 'key3', operator: '<', value: 'value3' }, { key: 'key3', operator: '<', value: 'value3', condition: '' },
{ key: 'key4', operator: '>', value: 'value4' }, { key: 'key4', operator: '>', value: 'value4', condition: '' },
{ key: 'key5', operator: '=~', value: 'value5' }, { key: 'key5', operator: '=~', value: 'value5', condition: '' },
{ key: 'key6', operator: '!~', value: 'value6' }, { key: 'key6', operator: '!~', value: 'value6', condition: '' },
] ]
); );
@ -713,7 +689,7 @@ describe('ElasticQueryBuilder', () => {
describe('getLogsQuery', () => { describe('getLogsQuery', () => {
it('should return query with defaults', () => { it('should return query with defaults', () => {
const query = builder.getLogsQuery({ refId: 'A' }, 500, null); const query = builder.getLogsQuery({ refId: 'A' }, 500);
expect(query.size).toEqual(500); expect(query.size).toEqual(500);
@ -750,7 +726,7 @@ describe('ElasticQueryBuilder', () => {
describe('lucene query', () => { describe('lucene query', () => {
it('should add query_string filter when query is not empty', () => { it('should add query_string filter when query is not empty', () => {
const luceneQuery = 'foo'; const luceneQuery = 'foo';
const query = builder.getLogsQuery({ refId: 'A', query: luceneQuery }, 500, null); const query = builder.getLogsQuery({ refId: 'A', query: luceneQuery }, 500);
expect(query.query.bool.filter).toContainEqual({ expect(query.query.bool.filter).toContainEqual({
query_string: { analyze_wildcard: true, query: luceneQuery }, query_string: { analyze_wildcard: true, query: luceneQuery },
@ -758,7 +734,7 @@ describe('ElasticQueryBuilder', () => {
}); });
it('should not add query_string filter when query is empty', () => { it('should not add query_string filter when query is empty', () => {
const query = builder.getLogsQuery({ refId: 'A' }, 500, null); const query = builder.getLogsQuery({ refId: 'A' }, 500);
expect(query.query.bool.filter.find((filter: any) => Object.keys(filter).includes('query_string'))).toBeFalsy(); expect(query.query.bool.filter.find((filter: any) => Object.keys(filter).includes('query_string'))).toBeFalsy();
}); });
@ -767,12 +743,12 @@ describe('ElasticQueryBuilder', () => {
it('with adhoc filters', () => { it('with adhoc filters', () => {
// TODO: Types for AdHocFilters // TODO: Types for AdHocFilters
const adhocFilters = [ const adhocFilters = [
{ key: 'key1', operator: '=', value: 'value1' }, { key: 'key1', operator: '=', value: 'value1', condition: '' },
{ key: 'key2', operator: '!=', value: 'value2' }, { key: 'key2', operator: '!=', value: 'value2', condition: '' },
{ key: 'key3', operator: '<', value: 'value3' }, { key: 'key3', operator: '<', value: 'value3', condition: '' },
{ key: 'key4', operator: '>', value: 'value4' }, { key: 'key4', operator: '>', value: 'value4', condition: '' },
{ key: 'key5', operator: '=~', value: 'value5' }, { key: 'key5', operator: '=~', value: 'value5', condition: '' },
{ key: 'key6', operator: '!~', value: 'value6' }, { key: 'key6', operator: '!~', value: 'value6', condition: '' },
]; ];
const query = builder.getLogsQuery({ refId: 'A' }, 500, adhocFilters); const query = builder.getLogsQuery({ refId: 'A' }, 500, adhocFilters);

@ -1,4 +1,4 @@
import { InternalTimeZones } from '@grafana/data'; import { AdHocVariableFilter, InternalTimeZones } from '@grafana/data';
import { import {
Filters, Filters,
@ -200,7 +200,7 @@ export class ElasticQueryBuilder {
} }
} }
build(target: ElasticsearchQuery, adhocFilters?: any) { build(target: ElasticsearchQuery, adhocFilters?: AdHocVariableFilter[]) {
// make sure query has defaults; // make sure query has defaults;
target.metrics = target.metrics || [defaultMetricAgg()]; target.metrics = target.metrics || [defaultMetricAgg()];
target.bucketAggs = target.bucketAggs || [defaultBucketAgg()]; target.bucketAggs = target.bucketAggs || [defaultBucketAgg()];
@ -478,7 +478,7 @@ export class ElasticQueryBuilder {
return query; return query;
} }
getLogsQuery(target: ElasticsearchQuery, limit: number, adhocFilters?: any) { getLogsQuery(target: ElasticsearchQuery, limit: number, adhocFilters?: AdHocVariableFilter[]) {
let query: any = { let query: any = {
size: 0, size: 0,
query: { query: {
@ -503,7 +503,7 @@ export class ElasticQueryBuilder {
return { return {
...query, ...query,
aggs: this.build(target, null).aggs, aggs: this.build(target).aggs,
highlight: { highlight: {
fields: { fields: {
'*': {}, '*': {},

Loading…
Cancel
Save