forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AggTypeFilters to filter out aggs in editor (elastic#19913)
* Add AggTypeFilters to filter out aggs in editor * Add documentation * Implement CJ's feedback * Add link to missing vis variable * Fix for RxJS 6 * Add babel-core types and fix tests * Pass index pattern instead of vis * Fix documentation
- Loading branch information
Showing
27 changed files
with
388 additions
and
13 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
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 indexPattern The indexPattern 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.