-
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
Changes from 6 commits
e07b2c2
5e2962c
19001da
e0fd06d
cad0534
fa8bc0a
815911e
f232c9a
6f00c13
d3e40be
6d4d1b0
bf66cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
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 vis = {}; | ||
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, vis, 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, vis, 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, vis, aggConfig) | ||
.pipe(first()) | ||
.toPromise(); | ||
expect(filter).toHaveBeenCalledWith(aggTypes[0], vis, aggConfig); | ||
expect(filter).toHaveBeenCalledWith(aggTypes[1], vis, aggConfig); | ||
}); | ||
|
||
it('should allow registered filters to filter out aggTypes', async () => { | ||
const aggTypes = [{ name: 'count' }, { name: 'sum' }, { name: 'avg' }]; | ||
const observable = registry.filter$(aggTypes, vis, 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]]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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 { AggConfig, Vis } from '../../vis'; | ||
|
||
type AggTypeFilter = ( | ||
aggType: AggType, | ||
vis: Vis, | ||
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[], vis: Vis, aggConfig: AggConfig) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I spent a lot of time reading this line by line to parse out what it was doing. I think if we changed some names and assigned some variables we can make the logic a little more accessible. Here's what I suggest: public allowedAggTypes$(aggTypes: AggType[], vis: Vis, aggConfig: AggConfig) {
return this.subject.map(filters => {
const aggTypeFilters = Array.from(filters);
const allowedAggTypes = aggTypes.filter(aggType => {
const isAggTypeAllowed = aggTypeFilters.every(aggTypeFilter => aggTypeFilter(aggType, vis, aggConfig));
return isAggTypeAllowed;
});
return allowedAggTypes;
});
} |
||
return this.subject.pipe( | ||
map(filters => { | ||
const allFilters = Array.from(filters); | ||
const allowedAggTypes = aggTypes.filter(aggType => { | ||
const isAggTypeAllowed = allFilters.every(filter => | ||
filter(aggType, vis, aggConfig) | ||
); | ||
return isAggTypeAllowed; | ||
}); | ||
return allowedAggTypes; | ||
}) | ||
); | ||
} | ||
} | ||
|
||
const aggTypeFilters = new AggTypeFilters(); | ||
|
||
export { aggTypeFilters, AggTypeFilters }; |
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'; |
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'; |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import { aggTypes } from '../../../agg_types'; | |
import { uiModules } from '../../../modules'; | ||
import { documentationLinks } from '../../../documentation_links/documentation_links'; | ||
import aggParamsTemplate from './agg_params.html'; | ||
import { aggTypeFilters } from '../../../agg_types/filter'; | ||
|
||
uiModules | ||
.get('app/visualize') | ||
|
@@ -39,8 +40,16 @@ uiModules | |
link: function ($scope, $el, attr) { | ||
$scope.$bind('agg', attr.agg); | ||
$scope.$bind('groupName', attr.groupName); | ||
$scope.$bind('vis', attr.vis); | ||
|
||
const aggTypeSubscription = aggTypeFilters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How important/valuable is it that we're exposing observables through the interface here? For example, I can also picture this functionality being supported by an interface that looked like this (while still using observables internally): const aggTypeSubscription = new AggTypeSubscription(aggTypes.byType[$scope.groupName], $scope.vis, $scope.agg);
aggTypeSubscription.subscribe(aggTypes => $scope.aggTypeOptions = aggTypes); This would encapsulate the complexity of observables and present me with an interface which appears and behaves like an event emitter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are anyway working with observables I think we are actually also want to expose this for a couple of reasons:
Since Observables and RxJS tend to become a common pattern (and tend to become a standard JavaScript pattern) I would rather only hide Observables in places we use them, if there is a really good reason for doing so. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a couple thoughts in response, but keep in mind I'm coming to this with zero experience in observables, so I'm mostly asking questions to learn and not with any strongly held opinion. I would not consider my comments a blocker on this PR in any way.
100% agree, from what I've learned about them so far their power is apparent. But this is why I'm trying to learn more about why this power is necessary, since you don't want to use a tank when all you need is a bicycle. In this particular case, it doesn't seem like we're taking advantage of the observable. Are there concrete use cases for the
Just to explain my thought process, I look for the abstractions we're creating in our code and try to figure out what kind of model can be inferred from them. So I poke and probe at abstractions that are complex and try to understand if that complexity is necessary. An example of where unnecessary complexity in abstractions created a confusing model would be our courier code. I was thinking that if it turns out that the observable is really just an implementation detail, then it might make sense to reduce complexity for the consumer by hiding it.
No argument here. 😄 I agree that observables can be really useful and I'm sure there are situations where an abstraction that deals in observables makes sense. I'm just trying to understand why they're a core part of this abstraction's interface. I have very little experience with observables, so I'm really just trying to learn more about the situations in which they're most useful and understand how this is one of those situations. |
||
.filter$(aggTypes.byType[$scope.groupName], $scope.vis, $scope.agg) | ||
.subscribe(aggTypes => $scope.aggTypeOptions = aggTypes); | ||
|
||
$scope.$on('$destroy', () => { | ||
aggTypeSubscription.unsubscribe(); | ||
}); | ||
|
||
$scope.aggTypeOptions = aggTypes.byType[$scope.groupName]; | ||
$scope.advancedToggled = false; | ||
|
||
// We set up this watch prior to adding the controls below, because when the controls are added, | ||
|
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