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 AggParamFilters to filter out params in vis editor #20686

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions src/ui/public/agg_types/agg_params.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 class AggParams {
constructor(params: any[]);
}
68 changes: 68 additions & 0 deletions src/ui/public/agg_types/filter/agg_param_filters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.
*/

jest.mock('../agg_params', () => ({
AggParams: class AggParams {
constructor(params) {
return [...params];
}
},
}));

import { AggParamFilters } from './agg_param_filters';

describe('AggParamFilters', () => {
let registry: AggParamFilters;
const indexPattern = {};
const aggConfig = {};

beforeEach(() => {
registry = new AggParamFilters();
});

it('should filter nothing without registered filters', async () => {
const aggParams = [{ name: 'field' }, { name: 'interval' }];
const filtered = registry.filter(aggParams, indexPattern, aggConfig);
expect(filtered).toEqual(aggParams);
});

it('should pass all aggParams to the registered filter', async () => {
const aggParams = [{ name: 'field' }, { name: 'interval' }];
const filter = jest.fn();
registry.addFilter(filter);
registry.filter(aggParams, indexPattern, aggConfig);
expect(filter).toHaveBeenCalledWith(aggParams[0], indexPattern, aggConfig);
expect(filter).toHaveBeenCalledWith(aggParams[1], indexPattern, aggConfig);
});

it('should allow registered filters to filter out aggParams', async () => {
const aggParams = [{ name: 'field' }, { name: 'interval' }, { name: 'json' }];
let filtered = registry.filter(aggParams, indexPattern, aggConfig);
expect(filtered).toEqual(aggParams);

registry.addFilter(() => true);
registry.addFilter(aggType => aggType.name !== 'field');
filtered = registry.filter(aggParams, indexPattern, aggConfig);
expect(filtered).toEqual([aggParams[1], aggParams[2]]);

registry.addFilter(aggType => aggType.name !== 'json');
filtered = registry.filter(aggParams, indexPattern, aggConfig);
expect(filtered).toEqual([aggParams[1]]);
});
});
64 changes: 64 additions & 0 deletions src/ui/public/agg_types/filter/agg_param_filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 { IndexPattern } from '../../index_patterns';
import { AggConfig } from '../../vis';
import { AggParams } from '../agg_params';

type AggParamFilter = (aggParam: any, indexPattern: IndexPattern, aggConfig: AggConfig) => boolean;

/**
* A registry to store {@link AggParamFilter} which are used to filter down
* available aggregations params.
*/
class AggParamFilters {
private filters = new Set<AggParamFilter>();

/**
* Register a new {@link AggParamFilter} with this registry.
*
* @param filter The filter to register.
*/
public addFilter(filter: AggParamFilter): void {
this.filters.add(filter);
}

/**
* Returns the {@link AggParams|aggParams} filtered by all registered filters.
*
* @param aggParams A list of aggParams 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 aggParams.
*/
public filter(aggParams: any[], indexPattern: IndexPattern, aggConfig: AggConfig) {
const allFilters = Array.from(this.filters);
const allowedAggParams = aggParams.filter(aggParam => {
const isAggParamAllowed = allFilters.every(filter =>
filter(aggParam, indexPattern, aggConfig)
);
return isAggParamAllowed;
});

return new AggParams(allowedAggParams);
}
}

const aggParamFilters = new AggParamFilters();

export { aggParamFilters, AggParamFilters };
1 change: 1 addition & 0 deletions src/ui/public/agg_types/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { aggTypeFilters } from './agg_type_filters';
export { aggParamFilters } from './agg_param_filters';
2 changes: 1 addition & 1 deletion src/ui/public/vis/editors/default/agg.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</span>

<!-- error -->
<span ng-if="!editorOpen && aggForm.$invalid" class="vis-editor-agg-header-description danger" title="{{aggForm.describeErrors()}}">
<span ng-if="!editorOpen && aggForm.$invalid && aggForm.softErrorCount()" class="vis-editor-agg-header-description danger" title="{{aggForm.describeErrors()}}">
{{ aggForm.describeErrors() }}
</span>

Expand Down
17 changes: 12 additions & 5 deletions src/ui/public/vis/editors/default/agg_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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';
import { aggTypeFilters, aggParamFilters } from '../../../agg_types/filter';

uiModules
.get('app/visualize')
Expand All @@ -45,6 +45,11 @@ uiModules
$scope.aggTypeOptions = aggTypeFilters
.filter(aggTypes.byType[$scope.groupName], $scope.indexPattern, $scope.agg);

// If current agg type is not part of allowed agg types, set to first element of allowed agg types
if(!$scope.aggTypeOptions.includes($scope.agg.type)) {
$scope.agg.type = $scope.aggTypeOptions[0];
}

$scope.advancedToggled = false;

// We set up this watch prior to adding the controls below, because when the controls are added,
Expand Down Expand Up @@ -105,8 +110,12 @@ uiModules
advanced: []
};

// filter agg params
$scope.aggTypeParams = aggParamFilters
.filter($scope.agg.type.params, $scope.indexPattern, $scope.agg);

// build collection of agg params html
$scope.agg.type.params.forEach(function (param, i) {
$scope.aggTypeParams.forEach(function (param, i) {
let aggParam;
let fields;
if ($scope.agg.schema.hideCustomLabel && param.name === 'customLabel') {
Expand All @@ -127,14 +136,12 @@ uiModules
}
}


let type = 'basic';
if (param.advanced) type = 'advanced';

if (aggParam = getAggParamHTML(param, i)) {
aggParamHTML[type].push(aggParam);
}

});

// compile the paramEditors html elements
Expand All @@ -157,7 +164,7 @@ uiModules
}

const attrs = {
'agg-param': 'agg.type.params[' + idx + ']'
'agg-param': 'aggTypeParams[' + idx + ']'
};

if (param.advanced) {
Expand Down