Skip to content

Commit

Permalink
add filters_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed May 4, 2020
1 parent 4c8aca9 commit 910c7b9
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/plugins/data/public/search/aggs/agg_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const getAggTypes = ({

/** Buckets: **/
import { aggFilter } from './buckets/filter_fn';
import { aggFilters } from './buckets/filters_fn';
import { aggSignificantTerms } from './buckets/significant_terms_fn';
import { aggIpRange } from './buckets/ip_range_fn';
import { aggDateRange } from './buckets/date_range_fn';
Expand All @@ -117,6 +118,7 @@ import { aggTerms } from './buckets/terms_fn';

export const getAggTypesFunctions = () => [
aggFilter,
aggFilters,
aggSignificantTerms,
aggIpRange,
aggDateRange,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/search/aggs/buckets/date_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface DateRangeBucketAggDependencies {
}

export interface AggParamsDateRange extends BaseAggParams {
field: string;
field?: string;
ranges?: DateRangeKey[];
time_zone?: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ describe('agg_expression_functions', () => {
const fn = functionWrapper(aggDateRange());

test('fills in defaults when only required args are provided', () => {
const actual = fn({
field: 'date_field',
});
const actual = fn({});
expect(actual).toMatchInlineSnapshot(`
Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"field": "date_field",
"field": undefined,
"json": undefined,
"ranges": undefined,
"time_zone": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const aggDateRange = (): FunctionDefinition => ({
},
field: {
types: ['string'],
required: true,
required: false,
help: i18n.translate('data.search.aggs.buckets.dateRange.field.help', {
defaultMessage: 'Field to use for this aggregation',
}),
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/public/search/aggs/buckets/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export interface FilterBucketAggDependencies {
}

export interface AggParamsFilter extends BaseAggParams {
field: string;
geo_bounding_box?: GeoBoundingBox;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ describe('agg_expression_functions', () => {
const fn = functionWrapper(aggFilter());

test('fills in defaults when only required args are provided', () => {
const actual = fn({
field: 'geo_field',
});
const actual = fn({});
expect(actual).toMatchInlineSnapshot(`
Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"field": "geo_field",
"geo_bounding_box": undefined,
"json": undefined,
},
Expand All @@ -48,7 +45,6 @@ describe('agg_expression_functions', () => {

test('includes optional params when they are provided', () => {
const actual = fn({
field: 'geo_field',
geo_bounding_box: JSON.stringify({
wkt: 'BBOX (-74.1, -71.12, 40.73, 40.01)',
}),
Expand All @@ -59,7 +55,6 @@ describe('agg_expression_functions', () => {
"enabled": true,
"id": undefined,
"params": Object {
"field": "geo_field",
"geo_bounding_box": Object {
"wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)",
},
Expand All @@ -73,15 +68,13 @@ describe('agg_expression_functions', () => {

test('correctly parses json string argument', () => {
const actual = fn({
field: 'ip_field',
json: '{ "foo": true }',
});

expect(actual.value.params.json).toEqual({ foo: true });

expect(() => {
fn({
field: 'ip_field',
json: '/// intentionally malformed json ///',
});
}).toThrowErrorMatchingInlineSnapshot(`"Unable to parse json argument string"`);
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/data/public/search/aggs/buckets/filter_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ export const aggFilter = (): FunctionDefinition => ({
defaultMessage: 'Schema to use for this aggregation',
}),
},
field: {
types: ['string'],
required: true,
help: i18n.translate('data.search.aggs.buckets.filter.field.help', {
defaultMessage: 'Field to use for this aggregation',
}),
},
geo_bounding_box: {
types: ['string'],
required: false,
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Storage } from '../../../../../../plugins/kibana_utils/public';
import { getEsQueryConfig, buildEsQuery, Query } from '../../../../common';
import { getQueryLog } from '../../../query';
import { GetInternalStartServicesFn } from '../../../types';
import { BaseAggParams } from '../types';

const filtersTitle = i18n.translate('data.search.aggs.buckets.filtersTitle', {
defaultMessage: 'Filters',
Expand All @@ -47,6 +48,13 @@ export interface FiltersBucketAggDependencies {
getInternalStartServices: GetInternalStartServicesFn;
}

export interface AggParamsFilters extends BaseAggParams {
filters?: Array<{
input: Query;
label: string;
}>;
}

export const getFiltersBucketAgg = ({
uiSettings,
getInternalStartServices,
Expand Down
91 changes: 91 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/filters_fn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { functionWrapper } from '../test_helpers';
import { aggFilters } from './filters_fn';

describe('agg_expression_functions', () => {
describe('aggFilters', () => {
const fn = functionWrapper(aggFilters());

test('fills in defaults when only required args are provided', () => {
const actual = fn({});
expect(actual).toMatchInlineSnapshot(`
Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"filters": undefined,
"json": undefined,
},
"schema": undefined,
"type": "filters",
},
}
`);
});

test('includes optional params when they are provided', () => {
const actual = fn({
filters: JSON.stringify([
{
query: 'query',
language: 'lucene',
label: 'test',
},
]),
});

expect(actual.value).toMatchInlineSnapshot(`
Object {
"enabled": true,
"id": undefined,
"params": Object {
"filters": Array [
Object {
"label": "test",
"language": "lucene",
"query": "query",
},
],
"json": undefined,
},
"schema": undefined,
"type": "filters",
}
`);
});

test('correctly parses json string argument', () => {
const actual = fn({
json: '{ "foo": true }',
});

expect(actual.value.params.json).toEqual({ foo: true });

expect(() => {
fn({
json: '/// intentionally malformed json ///',
});
}).toThrowErrorMatchingInlineSnapshot(`"Unable to parse json argument string"`);
});
});
});
94 changes: 94 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/filters_fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { Assign } from '@kbn/utility-types';
import { ExpressionFunctionDefinition } from '../../../../../expressions/public';
import { AggExpressionType, AggExpressionFunctionArgs, BUCKET_TYPES } from '../';
import { getParsedValue } from '../utils/get_parsed_value';

const fnName = 'aggFilters';

type Input = any;
type AggArgs = AggExpressionFunctionArgs<typeof BUCKET_TYPES.FILTERS>;

type Arguments = Assign<AggArgs, { filters?: string }>;

type Output = AggExpressionType;
type FunctionDefinition = ExpressionFunctionDefinition<typeof fnName, Input, Arguments, Output>;

export const aggFilters = (): FunctionDefinition => ({
name: fnName,
help: i18n.translate('data.search.aggs.function.buckets.filters.help', {
defaultMessage: 'Generates a serialized agg config for a Filter agg',
}),
type: 'agg_type',
args: {
id: {
types: ['string'],
help: i18n.translate('data.search.aggs.buckets.filters.id.help', {
defaultMessage: 'ID for this aggregation',
}),
},
enabled: {
types: ['boolean'],
default: true,
help: i18n.translate('data.search.aggs.buckets.filters.enabled.help', {
defaultMessage: 'Specifies whether this aggregation should be enabled',
}),
},
schema: {
types: ['string'],
help: i18n.translate('data.search.aggs.buckets.filters.schema.help', {
defaultMessage: 'Schema to use for this aggregation',
}),
},
filters: {
types: ['string'],
required: false,
help: i18n.translate('data.search.aggs.buckets.filters.filters.help', {
defaultMessage: 'Filters to use for this aggregation',
}),
},
json: {
types: ['string'],
help: i18n.translate('data.search.aggs.buckets.filters.json.help', {
defaultMessage: 'Advanced json to include when the agg is sent to Elasticsearch',
}),
},
},
fn: (input, args) => {
const { id, enabled, schema, ...rest } = args;

return {
type: 'agg_type',
value: {
id,
enabled,
schema,
type: BUCKET_TYPES.FILTERS,
params: {
...rest,
filters: getParsedValue(args, 'filters'),
json: getParsedValue(args, 'json'),
},
},
};
},
});
1 change: 1 addition & 0 deletions src/plugins/data/public/search/aggs/buckets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './date_histogram';
export * from './date_range';
export * from './range';
export * from './filter';
export * from './filters';
export * from './geo_tile';
export * from './geo_hash';
export * from './ip_range';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/search/aggs/buckets/ip_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface IpRangeBucketAggDependencies {

export interface AggParamsIpRange extends BaseAggParams {
field: string;
ipRangeType: IP_RANGE_TYPES;
ipRangeType?: IP_RANGE_TYPES;
ranges?: Partial<{
[IP_RANGE_TYPES.FROM_TO]: RangeIpRangeAggKey[];
[IP_RANGE_TYPES.MASK]: CidrMaskIpRangeAggKey[];
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/search/aggs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
AggParamsIpRange,
AggParamsDateRange,
AggParamsFilter,
AggParamsFilters,
AggParamsSignificantTerms,
AggParamsGeoTile,
AggParamsGeoHash,
Expand Down Expand Up @@ -91,6 +92,7 @@ export interface AggParamsMapping {
[BUCKET_TYPES.IP_RANGE]: AggParamsIpRange;
[BUCKET_TYPES.DATE_RANGE]: AggParamsDateRange;
[BUCKET_TYPES.FILTER]: AggParamsFilter;
[BUCKET_TYPES.FILTERS]: AggParamsFilters;
[BUCKET_TYPES.SIGNIFICANT_TERMS]: AggParamsSignificantTerms;
[BUCKET_TYPES.GEOTILE_GRID]: AggParamsGeoTile;
[BUCKET_TYPES.GEOHASH_GRID]: AggParamsGeoHash;
Expand Down

0 comments on commit 910c7b9

Please sign in to comment.