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

Add AggTypeFilters to filter out aggs in editor #19913

Merged
merged 12 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
"@kbn/eslint-plugin-license-header": "link:packages/kbn-eslint-plugin-license-header",
"@kbn/plugin-generator": "link:packages/kbn-plugin-generator",
"@kbn/test": "link:packages/kbn-test",
"@types/babel-core": "^6.25.5",
"@types/angular": "^1.6.45",
"@types/classnames": "^2.2.3",
"@types/eslint": "^4.16.2",
Expand Down
1 change: 1 addition & 0 deletions src/core_plugins/kibana/public/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'uiExports/visTypes';
import 'uiExports/visResponseHandlers';
import 'uiExports/visRequestHandlers';
import 'uiExports/visEditorTypes';
import 'uiExports/visualize';
import 'uiExports/savedObjectTypes';
import 'uiExports/fieldFormats';
import 'uiExports/fieldFormatEditors';
Expand Down
1 change: 0 additions & 1 deletion src/core_plugins/kibana/public/visualize/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import _ from 'lodash';
import '../saved_visualizations/saved_visualizations';
import 'ui/vis/editors/default/sidebar';
import './agg_filter';
import 'ui/visualize';
import 'ui/collapsible_sidebar';
import 'ui/share';
Expand Down
1 change: 0 additions & 1 deletion src/core_plugins/kibana/public/visualize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import './styles/main.less';
import './editor/editor';
import './wizard/wizard';
import './editor/agg_filter';
import 'ui/draggable/draggable_container';
import 'ui/draggable/draggable_item';
import 'ui/draggable/draggable_handle';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ describe('editor', function () {
]
});

const $el = $('<vis-editor-agg-params agg="agg" group-name="groupName"></vis-editor-agg-params>');
const $el = $('<vis-editor-agg-params agg="agg" index-pattern="vis.indexPattern" group-name="groupName"></vis-editor-agg-params>');
const $parentScope = $injector.get('$rootScope').$new();

agg = $parentScope.agg = vis.aggs.bySchemaName.segment[0];
$parentScope.groupName = 'buckets';
$parentScope.vis = vis;

$compile($el)($parentScope);
$scope = $el.scope();
Expand Down
20 changes: 20 additions & 0 deletions src/ui/public/agg_types/agg_type.d.ts
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;
1 change: 1 addition & 0 deletions src/ui/public/agg_types/controls/order_agg.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</div>
<div ng-show="agg.params.orderAgg" class="vis-editor-agg-order-agg">
<vis-editor-agg-params
index-pattern="vis.indexPattern"
agg="agg.params.orderAgg"
ng-if="agg.params.orderAgg"
group-name="'metrics'">
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/agg_types/controls/sub_agg.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<div ng-if="agg.params.metricAgg === 'custom'" class="vis-editor-agg-order-agg">
<ng-form name="customMetricForm">
<vis-editor-agg-params
index-pattern="vis.indexPattern"
agg="agg.params.customMetric"
group-name="'metrics'">
</vis-editor-agg-params>
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/agg_types/controls/sub_metric.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ng-form name="{{aggType}}Form">
<vis-editor-agg-params
agg="agg.params[aggType]"
index-pattern="vis.indexPattern"
group-name="'{{aggGroup}}'">
</vis-editor-agg-params>
</ng-form>
Expand Down
76 changes: 76 additions & 0 deletions src/ui/public/agg_types/filter/agg_type_filters.test.ts
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]]);
});
});
83 changes: 83 additions & 0 deletions src/ui/public/agg_types/filter/agg_type_filters.ts
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.
Copy link
Contributor

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

* @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 };
20 changes: 20 additions & 0 deletions src/ui/public/agg_types/filter/index.ts
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';
20 changes: 20 additions & 0 deletions src/ui/public/agg_types/index.d.ts
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
Expand Up @@ -17,11 +17,8 @@
* under the License.
*/

import { propFilter } from 'ui/filters/_prop_filter';
import { uiModules } from 'ui/modules';
type FilterFunc = <I>(item: I) => boolean;

uiModules
.get('kibana')
.filter('aggFilter', function () {
return propFilter('name');
});
export const propFilter: (
prop: string
) => <T>(list: T[], filters: string[] | string | FilterFunc) => T[];
20 changes: 20 additions & 0 deletions src/ui/public/index_patterns/_index_pattern.d.ts
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;
20 changes: 20 additions & 0 deletions src/ui/public/index_patterns/index.d.ts
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';
20 changes: 20 additions & 0 deletions src/ui/public/vis/agg_config.d.ts
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;
3 changes: 2 additions & 1 deletion src/ui/public/vis/editors/default/__tests__/agg_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ describe('Vis-Editor-Agg-Params plugin directive', function () {
});

$parentScope.agg = new AggConfig(vis, state);
$parentScope.vis = vis;

// make the element
$elem = angular.element(
`<vis-editor-agg-params agg="agg" group-name="groupName"></vis-editor-agg-params>`
`<vis-editor-agg-params index-pattern="vis.indexPattern" agg="agg" group-name="groupName"></vis-editor-agg-params>`
);

// compile the html
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/vis/editors/default/agg.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
agg="agg"
group-name="groupName"
ng-show="editorOpen"
index-pattern="vis.indexPattern"
class="vis-editor-agg-editor">
</vis-editor-agg-params>

Expand Down
Loading