Skip to content
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

Make aggTypeFilter registry return value directly #20523

Merged
merged 1 commit into from Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 5 additions & 21 deletions src/ui/public/agg_types/filter/agg_type_filters.test.ts
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import { first } from 'rxjs/operators';
import { AggTypeFilters } from './agg_type_filters';

describe('AggTypeFilters', () => {
Expand All @@ -31,46 +30,31 @@ describe('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();
const filtered = registry.filter(aggTypes, indexPattern, aggConfig);
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();
registry.filter(aggTypes, indexPattern, aggConfig);
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();
let filtered = registry.filter(aggTypes, indexPattern, aggConfig);
expect(filtered).toEqual(aggTypes);

registry.addFilter(() => true);
registry.addFilter(aggType => aggType.name !== 'count');
filtered = await observable.pipe(first()).toPromise();
filtered = registry.filter(aggTypes, indexPattern, aggConfig);
expect(filtered).toEqual([aggTypes[1], aggTypes[2]]);

registry.addFilter(aggType => aggType.name !== 'avg');
filtered = await observable.pipe(first()).toPromise();
filtered = registry.filter(aggTypes, indexPattern, aggConfig);
expect(filtered).toEqual([aggTypes[1]]);
});
});
32 changes: 10 additions & 22 deletions src/ui/public/agg_types/filter/agg_type_filters.ts
Expand Up @@ -16,8 +16,6 @@
* 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';
Expand All @@ -34,47 +32,37 @@ type AggTypeFilter = (
*/
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.
* Returns the {@link AggType|aggTypes} filtered by all registered filters.
*
* @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$(
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 allFilters = Array.from(this.filters);
const allowedAggTypes = aggTypes.filter(aggType => {
const isAggTypeAllowed = allFilters.every(filter =>
filter(aggType, indexPattern, aggConfig)
);
return isAggTypeAllowed;
});
return allowedAggTypes;
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/ui/public/vis/editors/default/agg_params.js
Expand Up @@ -42,13 +42,8 @@ uiModules
$scope.$bind('groupName', attr.groupName);
$scope.$bind('indexPattern', attr.indexPattern);

const aggTypeSubscription = aggTypeFilters
.filter$(aggTypes.byType[$scope.groupName], $scope.indexPattern, $scope.agg)
.subscribe(aggTypes => $scope.aggTypeOptions = aggTypes);

$scope.$on('$destroy', () => {
aggTypeSubscription.unsubscribe();
});
$scope.aggTypeOptions = aggTypeFilters
.filter(aggTypes.byType[$scope.groupName], $scope.indexPattern, $scope.agg);

$scope.advancedToggled = false;

Expand Down