-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AggTypeFilters to filter out aggs in editor #19913
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e07b2c2
Add AggTypeFilters to filter out aggs in editor
5e2962c
Add documentation
19001da
Implement CJ's feedback
e0fd06d
Add link to missing vis variable
cad0534
Fix for RxJS 6
fa8bc0a
Add babel-core types and fix tests
815911e
Merge branch 'master' into aggtype-filter
f232c9a
Merge branch 'master' into aggtype-filter
6f00c13
Pass index pattern instead of vis
d3e40be
Merge branch 'master' into aggtype-filter
6d4d1b0
Merge branch 'master' into aggtype-filter
bf66cbf
Fix documentation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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 AggType = 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
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
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
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,76 @@ | ||
/* | ||
* 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 { first } from 'rxjs/operators'; | ||
import { AggTypeFilters } from './agg_type_filters'; | ||
|
||
describe('AggTypeFilters', () => { | ||
let registry: AggTypeFilters; | ||
const indexPattern = {}; | ||
const aggConfig = {}; | ||
|
||
beforeEach(() => { | ||
registry = new AggTypeFilters(); | ||
}); | ||
|
||
it('should filter nothing without registered filters', async () => { | ||
const aggTypes = [{ name: 'count' }, { name: 'sum' }]; | ||
const observable = registry.filter$(aggTypes, indexPattern, aggConfig); | ||
const filtered = await observable.pipe(first()).toPromise(); | ||
expect(filtered).toEqual(aggTypes); | ||
}); | ||
|
||
it('should emit a new filtered list when registering a new filter', async () => { | ||
const aggTypes = [{ name: 'count' }, { name: 'sum' }]; | ||
const observable = registry.filter$(aggTypes, indexPattern, aggConfig); | ||
const spy = jest.fn(); | ||
observable.subscribe(spy); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
registry.addFilter(() => true); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should pass all aggTypes to the registered filter', async () => { | ||
const aggTypes = [{ name: 'count' }, { name: 'sum' }]; | ||
const filter = jest.fn(); | ||
registry.addFilter(filter); | ||
await registry | ||
.filter$(aggTypes, indexPattern, aggConfig) | ||
.pipe(first()) | ||
.toPromise(); | ||
expect(filter).toHaveBeenCalledWith(aggTypes[0], indexPattern, aggConfig); | ||
expect(filter).toHaveBeenCalledWith(aggTypes[1], indexPattern, aggConfig); | ||
}); | ||
|
||
it('should allow registered filters to filter out aggTypes', async () => { | ||
const aggTypes = [{ name: 'count' }, { name: 'sum' }, { name: 'avg' }]; | ||
const observable = registry.filter$(aggTypes, indexPattern, aggConfig); | ||
let filtered = await observable.pipe(first()).toPromise(); | ||
expect(filtered).toEqual(aggTypes); | ||
|
||
registry.addFilter(() => true); | ||
registry.addFilter(aggType => aggType.name !== 'count'); | ||
filtered = await observable.pipe(first()).toPromise(); | ||
expect(filtered).toEqual([aggTypes[1], aggTypes[2]]); | ||
|
||
registry.addFilter(aggType => aggType.name !== 'avg'); | ||
filtered = await observable.pipe(first()).toPromise(); | ||
expect(filtered).toEqual([aggTypes[1]]); | ||
}); | ||
}); |
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,83 @@ | ||
/* | ||
* 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 { BehaviorSubject } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { AggType } from '..'; | ||
import { IndexPattern } from '../../index_patterns'; | ||
import { AggConfig } from '../../vis'; | ||
|
||
type AggTypeFilter = ( | ||
aggType: AggType, | ||
indexPattern: IndexPattern, | ||
aggConfig: AggConfig | ||
) => boolean; | ||
|
||
/** | ||
* A registry to store {@link AggTypeFilter} which are used to filter down | ||
* available aggregations for a specific visualization and {@link AggConfig}. | ||
*/ | ||
class AggTypeFilters { | ||
private filters = new Set<AggTypeFilter>(); | ||
private subject = new BehaviorSubject<Set<AggTypeFilter>>(this.filters); | ||
|
||
/** | ||
* Register a new {@link AggTypeFilter} with this registry. | ||
* This will emit a new set of filtered aggTypes on every Observer returned | ||
* by the {@link #filter$|filter method}. | ||
* | ||
* @param filter The filter to register. | ||
*/ | ||
public addFilter(filter: AggTypeFilter): void { | ||
this.filters.add(filter); | ||
this.subject.next(this.filters); | ||
} | ||
|
||
/** | ||
* Returns an Observable that will emit a filtered list of the passed {@link AggType|aggTypes}. | ||
* A new filtered list will always be emitted when the {@link AggTypeFilter} | ||
* registered with this registry will change. | ||
* | ||
* @param aggTypes A list of aggTypes that will be filtered down by this registry. | ||
* @param vis The vis for which this list should be filtered down. | ||
* @param aggConfig The aggConfig for which the returning list will be used. | ||
* @return A filtered list of the passed aggTypes. | ||
*/ | ||
public filter$( | ||
aggTypes: AggType[], | ||
indexPattern: IndexPattern, | ||
aggConfig: AggConfig | ||
) { | ||
return this.subject.pipe( | ||
map(filters => { | ||
const allFilters = Array.from(filters); | ||
const allowedAggTypes = aggTypes.filter(aggType => { | ||
const isAggTypeAllowed = allFilters.every(filter => | ||
filter(aggType, indexPattern, aggConfig) | ||
); | ||
return isAggTypeAllowed; | ||
}); | ||
return allowedAggTypes; | ||
}) | ||
); | ||
} | ||
} | ||
|
||
const aggTypeFilters = new AggTypeFilters(); | ||
|
||
export { aggTypeFilters, AggTypeFilters }; |
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 { aggTypeFilters } from './agg_type_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 { AggType } from './agg_type'; |
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
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 IndexPattern = 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
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 { IndexPattern } from './_index_pattern'; |
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 AggConfig = 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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be changed to indexPattern