Skip to content

Commit

Permalink
[Maps] Fix geo alerts handling of multi-fields (elastic#100348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Caldwell committed Jun 15, 2021
1 parent 75c35e8 commit 98d155c
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { mount } from 'enzyme';
import { EntityByExpression, getValidIndexPatternFields } from './entity_by_expression';

const defaultProps = {
errors: {
index: [],
indexId: [],
geoField: [],
entity: [],
dateField: [],
boundaryType: [],
boundaryIndexTitle: [],
boundaryIndexId: [],
boundaryGeoField: [],
name: ['Name is required.'],
interval: [],
alertTypeId: [],
actionConnectors: [],
},
entity: 'FlightNum',
setAlertParamsEntity: (arg: string) => {},
indexFields: [
{
count: 0,
name: 'DestLocation',
type: 'geo_point',
esTypes: ['geo_point'],
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
{
count: 0,
name: 'FlightNum',
type: 'string',
esTypes: ['keyword'],
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
{
count: 0,
name: 'OriginLocation',
type: 'geo_point',
esTypes: ['geo_point'],
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
{
count: 0,
name: 'timestamp',
type: 'date',
esTypes: ['date'],
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
],
isInvalid: false,
};

test('should render entity by expression with aggregatable field options for entity', async () => {
const component = mount(<EntityByExpression {...defaultProps} />);
expect(component).toMatchSnapshot();
});
//

test('should only use valid index fields', async () => {
// Only the string index field should match
const indexFields = getValidIndexPatternFields(defaultProps.indexFields);
expect(indexFields.length).toEqual(1);

// Set all agg fields to false, invalidating them for use
const invalidIndexFields = defaultProps.indexFields.map((field) => ({
...field,
aggregatable: false,
}));

const noIndexFields = getValidIndexPatternFields(invalidIndexFields);
expect(noIndexFields.length).toEqual(0);
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ interface Props {
isInvalid: boolean;
}

const ENTITY_TYPES = ['string', 'number', 'ip'];
export function getValidIndexPatternFields(fields: IFieldType[]): IFieldType[] {
return fields.filter((field) => {
const isSpecifiedSupportedField = ENTITY_TYPES.includes(field.type);
const hasLeadingUnderscore = field.name.startsWith('_');
const isAggregatable = !!field.aggregatable;
return isSpecifiedSupportedField && isAggregatable && !hasLeadingUnderscore;
});
}

export const EntityByExpression: FunctionComponent<Props> = ({
errors,
entity,
setAlertParamsEntity,
indexFields,
isInvalid,
}) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
const ENTITY_TYPES = ['string', 'number', 'ip'];

const usePrevious = <T extends unknown>(value: T): T | undefined => {
const ref = useRef<T>();
useEffect(() => {
Expand All @@ -48,14 +55,12 @@ export const EntityByExpression: FunctionComponent<Props> = ({
});
useEffect(() => {
if (!_.isEqual(oldIndexFields, indexFields)) {
fields.current.indexFields = indexFields.filter(
(field: IFieldType) => ENTITY_TYPES.includes(field.type) && !field.name.startsWith('_')
);
fields.current.indexFields = getValidIndexPatternFields(indexFields);
if (!entity && fields.current.indexFields.length) {
setAlertParamsEntity(fields.current.indexFields[0].name);
}
}
}, [ENTITY_TYPES, indexFields, oldIndexFields, setAlertParamsEntity, entity]);
}, [indexFields, oldIndexFields, setAlertParamsEntity, entity]);

const indexPopover = (
<EuiFormRow id="entitySelect" fullWidth error={errors.index}>
Expand Down

0 comments on commit 98d155c

Please sign in to comment.