Skip to content

Commit

Permalink
[ML] filter bool agg support
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Jun 2, 2020
1 parent d758648 commit 9a2f734
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FC } from 'react';
import { EuiCodeEditor } from '@elastic/eui';
import React from 'react';
import { EuiCodeEditor, EuiSpacer } from '@elastic/eui';
import { FilterAggConfigEditor } from '../types';

export const FilterEditorForm: FC = () => {
return <EuiCodeEditor value={''} mode="json" style={{ width: '100%' }} theme="textmate" />;
export const FilterEditorForm: FilterAggConfigEditor['aggTypeConfig']['FilterAggFormComponent'] = ({
config,
onChange,
}) => {
return (
<>
<EuiSpacer size="m" />
<EuiCodeEditor
value={config}
onChange={(d) => {
onChange({ config: d });
}}
mode="json"
style={{ width: '100%' }}
theme="textmate"
height="300px"
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
import { FILTERS } from './constants';
import { FilterAggForm, FilterEditorForm, FilterRangeForm, FilterTermForm } from './components';
import {
FilterAggConfigBase,
FilterAggConfigBool,
FilterAggConfigExists,
FilterAggConfigRange,
FilterAggConfigTerm,
FilterAggConfigUnion,
Expand Down Expand Up @@ -67,7 +70,7 @@ export function getFilterAggConfig(
export function getFilterAggTypeConfig(
filterAggType: FilterAggConfigUnion['filterAgg'] | FilterAggType,
config?: { [key: string]: any }
): FilterAggConfigUnion['aggTypeConfig'] {
): FilterAggConfigUnion['aggTypeConfig'] | FilterAggConfigBase['aggTypeConfig'] {
switch (filterAggType) {
case FILTERS.TERM:
const value = typeof config === 'object' ? Object.values(config)[0] : undefined;
Expand Down Expand Up @@ -118,13 +121,29 @@ export function getFilterAggTypeConfig(
field: fieldName,
};
},
};
} as FilterAggConfigExists['aggTypeConfig'];
case FILTERS.BOOL:
return {
FilterAggFormComponent: FilterEditorForm,
filterAggConfig: JSON.stringify(
{
must: [],
must_not: [],
should: [],
},
null,
2
),
getEsAggConfig(fieldName) {
return JSON.parse(this.filterAggConfig!);
},
} as FilterAggConfigBool['aggTypeConfig'];
default:
return {
FilterAggFormComponent: FilterEditorForm,
filterAggConfig: {},
filterAggConfig: '',
getEsAggConfig() {
return { ...this.filterAggConfig };
return this.filterAggConfig !== undefined ? JSON.parse(this.filterAggConfig!) : {};
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ type FilterAggForm<T> = FC<{

interface FilterAggTypeConfig<U> {
/** Form component */
FilterAggFormComponent?: FilterAggForm<U>;
FilterAggFormComponent?: U extends undefined ? undefined : FilterAggForm<U>;
/** Filter agg type configuration*/
filterAggConfig?: U;
filterAggConfig?: U extends undefined ? undefined : U;
/** Converts UI agg config form to ES agg request object */
getEsAggConfig: (field?: string) => { [key: string]: any };
isValid?: () => boolean;
}

/** Filter agg type definition */
interface FilterAggProps<K extends FilterAggType, U extends {}> {
interface FilterAggProps<K extends FilterAggType, U> {
/** Filter aggregation type */
filterAgg: K;
/** Definition of the filter agg config */
Expand All @@ -44,8 +44,19 @@ export type FilterAggConfigRange = FilterAggProps<
'range',
{ gt?: number; lt?: number; lte?: number; gte?: number }
>;
/** Filter exists agg */
export type FilterAggConfigExists = FilterAggProps<'exists', undefined>;
/** Filter bool agg */
export type FilterAggConfigBool = FilterAggProps<'bool', string>;

export type FilterAggConfigUnion = FilterAggConfigTerm | FilterAggConfigRange;
/** General type for filter agg */
export type FilterAggConfigEditor = FilterAggProps<FilterAggType, string>;

export type FilterAggConfigUnion =
| FilterAggConfigTerm
| FilterAggConfigRange
| FilterAggConfigBool
| FilterAggConfigExists;

/**
* Union type for filter aggregations
Expand Down

0 comments on commit 9a2f734

Please sign in to comment.