Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Mar 3, 2020
1 parent 34aa486 commit a255bb3
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ export class AggConfigs {
}

byName(name: string) {
return this.aggs.filter(agg => agg.type.name === name);
return this.aggs.filter(agg => agg.type && agg.type.name === name);
}

byType(type: string) {
return this.aggs.filter(agg => agg.type.type === type);
return this.aggs.filter(agg => agg.type && agg.type.type === type);
}

byTypeName(type: string) {
return this.aggs.filter(agg => agg.type.name === type);
return this.byName(type);
}

bySchemaName(schema: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ export const topHitMetricAgg = new MetricAggType({
name: 'field',
type: 'field',
onlyAggregatable: false,
filterFieldTypes: (aggConfig: IMetricAggConfig) =>
_.get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false)
? '*'
: KBN_FIELD_TYPES.NUMBER,
filterFieldTypes: '*',
write(agg, output) {
const field = agg.getParam('field');
output.params = {};
Expand Down Expand Up @@ -133,7 +130,7 @@ export const topHitMetricAgg = new MetricAggType({
defaultMessage: 'Concatenate',
}),
isCompatible(aggConfig: IMetricAggConfig) {
return _.get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false);
return _.get(aggConfig.params, 'field.filterFieldTypes', '*') === '*';
},
disabled: true,
value: 'concat',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class AggParamType<TAggConfig extends IAggConfig = IAggConfig> extends Ba
constructor(config: Record<string, any>) {
super(config);

if (config.allowedAggs) {
this.allowedAggs = config.allowedAggs;
}

if (!config.write) {
this.write = (aggConfig: TAggConfig, output: Record<string, any>) => {
if (aggConfig.params[this.name] && aggConfig.params[this.name].length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import { get } from 'lodash';
import { BaseParamType } from './base';
import { FieldParamType } from './field';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../../../../../../../plugins/data/public';
Expand Down Expand Up @@ -109,10 +108,7 @@ describe('Field', () => {
const aggParam = new FieldParamType({
name: 'field',
type: 'field',
filterFieldTypes: (aggConfig: IMetricAggConfig) =>
get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false)
? '*'
: KBN_FIELD_TYPES.NUMBER,
filterFieldTypes: '*',
});
const fields = aggParam.getAvailableFields(agg);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface DefaultEditorAggParamsProps extends DefaultEditorCommonProps {
setValidity: (isValid: boolean) => void;
setTouched: (isTouched: boolean) => void;
schemas: Schema[];
allowedAggs?: string[];
}

function DefaultEditorAggParams({
Expand All @@ -78,12 +79,12 @@ function DefaultEditorAggParams({
setTouched,
setValidity,
schemas,
allowedAggs,
}: DefaultEditorAggParamsProps) {
const groupedAggTypeOptions = useMemo(() => getAggTypeOptions(agg, indexPattern, groupName), [
agg,
indexPattern,
groupName,
]);
const groupedAggTypeOptions = useMemo(
() => getAggTypeOptions(agg, indexPattern, groupName, allowedAggs || []),
[agg, indexPattern, groupName, allowedAggs]
);
const { title } = getSchemaByName(schemas, agg.schema);
const error = aggIsTooLow
? i18n.translate('visDefaultEditor.aggParams.errors.aggWrongRunOrderErrorMessage', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('DefaultEditorAggParams helpers', () => {
describe('getAggTypeOptions', () => {
it('should return agg type options grouped by subtype', () => {
const indexPattern = {} as IndexPattern;
const aggs = getAggTypeOptions({} as IAggConfig, indexPattern, 'metrics');
const aggs = getAggTypeOptions({} as IAggConfig, indexPattern, 'metrics', []);

expect(aggs).toEqual(['indexedFields']);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
AggParam,
IFieldParamType,
IAggType,
propFilter,
} from '../legacy_imports';
import { EditorConfig } from './utils';
import { Schema, getSchemaByName } from '../schemas';
Expand Down Expand Up @@ -82,9 +83,15 @@ function getAggParamsToRender({
}
// if field param exists, compute allowed fields
if (param.type === 'field') {
const availableFields: IndexPatternField[] = (param as IFieldParamType).getAvailableFields(
agg
);
let availableFields: IndexPatternField[] = (param as IFieldParamType).getAvailableFields(agg);
// should be refactored in the future to provide a more general way
// for visualization to override some agg config settings
if (agg.type.name === 'top_hits') {
const allowStrings = _.get(schema, `aggSettings[${agg.type.name}].allowStrings`, false);
if (!allowStrings) {
availableFields = availableFields.filter(field => field.type === 'number');
}
}
fields = aggTypeFieldFilters.filter(availableFields, agg);
indexedFields = groupAndSortBy(fields, 'type', 'name');

Expand Down Expand Up @@ -126,12 +133,19 @@ function getAggParamsToRender({
return params;
}

const filterByName = propFilter('name');

function getAggTypeOptions(
agg: IAggConfig,
indexPattern: IndexPattern,
groupName: string
groupName: string,
allowedAggs: string[]
): ComboBoxGroupedOptions<IAggType> {
const aggTypeOptions = aggTypeFilters.filter((aggTypes as any)[groupName], indexPattern, agg);
const aggTypeOptions = aggTypeFilters
.filter((aggTypes as any)[groupName], indexPattern, agg)
.filter(aggType => {
return filterByName([aggType], allowedAggs).length !== 0;
});
return groupAndSortBy(aggTypeOptions as any[], 'subtype', 'title');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function OrderAggParamEditor({
<EuiSpacer size="m" />
<DefaultEditorAggParams
agg={value as IAggConfig}
allowedAggs={aggParam.allowedAggs}
groupName={AggGroupNames.Metrics}
className="visEditorAgg__subAgg"
formIsTouched={formIsTouched}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function SubAggParamEditor({
<EuiSpacer size="m" />
<DefaultEditorAggParams
agg={agg.params.customMetric}
allowedAggs={aggParam.allowedAggs}
groupName={AggGroupNames.Metrics}
className="visEditorAgg__subAgg"
formIsTouched={formIsTouched}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function SubMetricParamEditor({
<EuiSpacer size="s" />
<DefaultEditorAggParams
agg={agg.params[type]}
allowedAggs={aggParam.allowedAggs}
groupName={aggGroup}
className="visEditorAgg__subAgg"
formIsTouched={formIsTouched}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function DefaultEditorDataTab({
const addSchema: AddSchema = useCallback(schema => dispatch(addNewAgg(schema)), [dispatch]);

const onAggRemove: DefaultEditorAggCommonProps['removeAgg'] = useCallback(
aggId => dispatch(removeAgg(aggId, schemas.all)),
aggId => dispatch(removeAgg(aggId, schemas.all || [])),
[dispatch, schemas]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function DefaultEditorSideBar({
const { formState, setTouched, setValidity, resetValidity } = useEditorFormState();

const responseAggs = useMemo(() => state.aggs.getResponseAggs(), [state.aggs]);
const metricSchemas = getSchemasByGroup(vis.type.schemas.all, AggGroupNames.Metrics).map(
const metricSchemas = getSchemasByGroup(vis.type.schemas.all || [], AggGroupNames.Metrics).map(
s => s.name
);
const metricAggs = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState {
switch (action.type) {
case EditorStateActionTypes.ADD_NEW_AGG: {
const { schema } = action.payload;
const defaultConfig = schema.defaults
? (schema as any).defaults.slice(0, schema.max)
: { schema: schema.name };
const defaultConfig =
!state.aggs.aggs.find(agg => agg.schema === schema.name) && schema.defaults
? (schema as any).defaults.slice(0, schema.max)
: { schema: schema.name };
const aggConfig = state.aggs.createAggConfig(defaultConfig, {
addToAggConfigs: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from '../../visualizations/public/';
import { PanelsContainer, Panel } from '../../../../plugins/kibana_react/public';

import './vis_type_agg_filter';
import { DefaultEditorSideBar } from './components/sidebar';
import { DefaultEditorControllerState } from './default_editor_controller';
import { getInitialWidth } from './editor_size';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@ class VisImpl extends EventEmitter {
// but whatever). Also if a schema.name is already set then don't
// set anything.
const newConfigs = [...configStates];
_(schemas)
.filter(schema => {
return Array.isArray(schema.defaults) && schema.defaults.length > 0;
})
.each(schema => {
if (!configStates.find(agg => agg.schema && agg.schema === schema.name)) {
const defaults = schema.defaults.slice(0, schema.max);
defaults.forEach(d => newConfigs.push(d));
}
schemas
.filter(schema => Array.isArray(schema.defaults) && schema.defaults.length > 0)
.filter(schema => !configStates.find(agg => agg.schema && agg.schema === schema.name))
.forEach(schema => {
const defaults = schema.defaults.slice(0, schema.max);
defaults.forEach(d => newConfigs.push(d));
});
return newConfigs;
}
Expand All @@ -104,7 +101,7 @@ class VisImpl extends EventEmitter {

if (state.aggs || !this.aggs) {
let configStates = state.aggs ? state.aggs.aggs || state.aggs : [];
configStates = this.initializeDefaultsFromSchemas(configStates, this.type.schemas.all);
configStates = this.initializeDefaultsFromSchemas(configStates, this.type.schemas.all || []);
this.aggs = createAggConfigs(this.indexPattern, configStates);
}
}
Expand Down

0 comments on commit a255bb3

Please sign in to comment.