-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
258 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export type FieldParamType = any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/ui/public/agg_types/param_types/filter/field_filters.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 { AggTypeFieldFilters } from './field_filters'; | ||
|
||
describe('AggTypeFieldFilters', () => { | ||
let registry: AggTypeFieldFilters; | ||
const fieldParamType = {}; | ||
const indexPattern = {}; | ||
const aggConfig = {}; | ||
|
||
beforeEach(() => { | ||
registry = new AggTypeFieldFilters(); | ||
}); | ||
|
||
it('should filter nothing without registered filters', async () => { | ||
const fields = [{ name: 'foo' }, { name: 'bar' }]; | ||
const filtered = registry.filter(fields, fieldParamType, indexPattern, aggConfig); | ||
expect(filtered).toEqual(fields); | ||
}); | ||
|
||
it('should pass all fields to the registered filter', async () => { | ||
const fields = [{ name: 'foo' }, { name: 'bar' }]; | ||
const filter = jest.fn(); | ||
registry.addFilter(filter); | ||
registry.filter(fields, fieldParamType, indexPattern, aggConfig); | ||
expect(filter).toHaveBeenCalledWith(fields[0], fieldParamType, indexPattern, aggConfig); | ||
expect(filter).toHaveBeenCalledWith(fields[1], fieldParamType, indexPattern, aggConfig); | ||
}); | ||
|
||
it('should allow registered filters to filter out fields', async () => { | ||
const fields = [{ name: 'foo' }, { name: 'bar' }]; | ||
let filtered = registry.filter(fields, fieldParamType, indexPattern, aggConfig); | ||
expect(filtered).toEqual(fields); | ||
|
||
registry.addFilter(() => true); | ||
registry.addFilter(field => field.name !== 'foo'); | ||
filtered = registry.filter(fields, fieldParamType, indexPattern, aggConfig); | ||
expect(filtered).toEqual([fields[1]]); | ||
|
||
registry.addFilter(field => field.name !== 'bar'); | ||
filtered = registry.filter(fields, fieldParamType, indexPattern, aggConfig); | ||
expect(filtered).toEqual([]); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
src/ui/public/agg_types/param_types/filter/field_filters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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 { FieldParamType } from '../'; | ||
import { IndexPattern } from '../../../index_patterns'; | ||
import { AggConfig } from '../../../vis'; | ||
|
||
type AggTypeFieldFilter = ( | ||
field: any, | ||
fieldParamType: FieldParamType, | ||
indexPattern: IndexPattern, | ||
aggConfig: AggConfig | ||
) => boolean; | ||
|
||
/** | ||
* A registry to store {@link AggTypeFieldFilter} which are used to filter down | ||
* available fields for a specific visualization and {@link AggType}. | ||
*/ | ||
class AggTypeFieldFilters { | ||
private filters = new Set<AggTypeFieldFilter>(); | ||
|
||
/** | ||
* Register a new {@link AggTypeFieldFilter} with this registry. | ||
* This will be used by the {@link #filter|filter method}. | ||
* | ||
* @param filter The filter to register. | ||
*/ | ||
public addFilter(filter: AggTypeFieldFilter): void { | ||
this.filters.add(filter); | ||
} | ||
|
||
/** | ||
* Returns the {@link any|fields} filtered by all registered filters. | ||
* | ||
* @param fields A list of fields that will be filtered down by this registry. | ||
* @param fieldParamType The fieldParamType for which the returning list will be used. | ||
* @param indexPattern The indexPattern for which the returning list will be used. | ||
* @param aggConfig The aggConfig for which the returning list will be used. | ||
* @return A filtered list of the passed fields. | ||
*/ | ||
public filter( | ||
fields: any[], | ||
fieldParamType: FieldParamType, | ||
indexPattern: IndexPattern, | ||
aggConfig: AggConfig | ||
) { | ||
const allFilters = Array.from(this.filters); | ||
const allowedAggTypeFields = fields.filter(field => { | ||
const isAggTypeFieldAllowed = allFilters.every(filter => | ||
filter(field, fieldParamType, indexPattern, aggConfig) | ||
); | ||
return isAggTypeFieldAllowed; | ||
}); | ||
return allowedAggTypeFields; | ||
} | ||
} | ||
|
||
const aggTypeFieldFilters = new AggTypeFieldFilters(); | ||
|
||
export { aggTypeFieldFilters, AggTypeFieldFilters }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { aggTypeFieldFilters } from './field_filters'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { FieldParamType } from './field'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/ui/public/vis/editors/default/vis_type_field_filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 { isFunction } from 'lodash'; | ||
import { FieldParamType } from '../../../agg_types/param_types'; | ||
import { aggTypeFieldFilters } from '../../../agg_types/param_types/filter'; | ||
import { IndexPattern } from '../../../index_patterns'; | ||
import { AggConfig } from '../../../vis'; | ||
|
||
import { propFilter } from '../../../filters/_prop_filter'; | ||
|
||
const filterByType = propFilter('type'); | ||
|
||
/** | ||
* This filter uses the {@link FieldParamType|fieldParamType} information | ||
* and limits available fields based on that. | ||
*/ | ||
aggTypeFieldFilters.addFilter( | ||
( | ||
field: any, | ||
fieldParamType: FieldParamType, | ||
indexPattern: IndexPattern, | ||
aggConfig: AggConfig | ||
) => { | ||
const { onlyAggregatable, scriptable, filterFieldTypes } = fieldParamType; | ||
|
||
const filters = isFunction(filterFieldTypes) | ||
? filterFieldTypes.bind(fieldParamType, aggConfig.vis) | ||
: filterFieldTypes; | ||
|
||
if ((onlyAggregatable && !field.aggregatable) || (!scriptable && field.scripted)) { | ||
return false; | ||
} | ||
|
||
if (!filters) { | ||
return true; | ||
} | ||
|
||
return filterByType([field], filters).length !== 0; | ||
} | ||
); |