Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Jan 9, 2024
1 parent 3e09fed commit ab83b8d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,28 @@ describe('filter manager utilities', () => {
const params = { gt: 50 };
const filter = { meta: { params } } as RangeFilter;
const result = getRangeDisplayValue(filter);
expect(result).toMatchInlineSnapshot(`"50 to Infinity"`);
expect(result).toMatchInlineSnapshot(`"≥ 50"`);
});

test('gte', () => {
const params = { gte: 60 };
const filter = { meta: { params } } as RangeFilter;
const result = getRangeDisplayValue(filter);
expect(result).toMatchInlineSnapshot(`"60 to Infinity"`);
expect(result).toMatchInlineSnapshot(`"≥ 60"`);
});

test('lt', () => {
const params = { lt: 70 };
const filter = { meta: { params } } as RangeFilter;
const result = getRangeDisplayValue(filter);
expect(result).toMatchInlineSnapshot(`"-Infinity to 70"`);
expect(result).toMatchInlineSnapshot(`"< 70"`);
});

test('lte', () => {
const params = { lte: 80 };
const filter = { meta: { params } } as RangeFilter;
const result = getRangeDisplayValue(filter);
expect(result).toMatchInlineSnapshot(`"-Infinity to 80"`);
expect(result).toMatchInlineSnapshot(`"< 80"`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ export function FilterContent({ filter, valueLabel, fieldLabel, hideAlias }: Fil
</>
);
case FILTERS.PHRASE:
return (
<>
<FilterField filter={filter} fieldLabel={fieldLabel} />
<FilterValue value={valueLabel} />
</>
);
case FILTERS.RANGE:
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@ import { ES_FIELD_TYPES } from '@kbn/field-types';
import isSemverValid from 'semver/functions/valid';
import { isFilterable, IpAddress } from '@kbn/data-plugin/common';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/common';
import { FILTER_OPERATORS, Operator } from './filter_operators';
import { FILTER_OPERATORS, OPERATORS, Operator } from './filter_operators';

export function getFieldFromFilter(filter: Filter, indexPattern?: DataView) {
return indexPattern?.fields.find((field) => field.name === filter.meta.key);
}

function getRangeOperatorFromFilter({ meta: { params } }: RangeFilter | ScriptedRangeFilter) {
const { gte, gt, lte, lt } = params || {};
if (gte === undefined && lt === undefined && lte === undefined && gt === undefined) {
return 'between';
function getRangeOperatorFromFilter({
meta: { params = {}, negate },
}: RangeFilter | ScriptedRangeFilter) {
if (negate) {
// if filter is negated, always use 'is not between' operator
return OPERATORS.NOT_BETWEEN;
}
const { gte, gt, lte, lt } = params;
const left = gte ?? gt;
const right = lte ?? lt;
if (left !== undefined && right !== undefined) {
return 'between';
return OPERATORS.BETWEEN;
}
if (left !== undefined) {
return 'greater_or_equal';
return OPERATORS.GREATER_OR_EQUAL;
}

if (right !== undefined) {
return 'less';
return OPERATORS.LESS;
}
return 'between';
return OPERATORS.BETWEEN;
}

export function getOperatorFromFilter(filter: Filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,24 @@ export const strings = {
}),
};

export enum OPERATORS {
LESS = 'less',
GREATER_OR_EQUAL = 'greater_or_equal',
BETWEEN = 'between',
IS = 'is',
NOT_BETWEEN = 'not_between',
IS_NOT = 'is_not',
IS_ONE_OF = 'is_one_of',
IS_NOT_ONE_OF = 'is_not_one_of',
EXISTS = 'exists',
DOES_NOT_EXIST = 'does_not_exist',
}

export interface Operator {
message: string;
type: FILTERS;
negate: boolean;
id?: 'less' | 'greater_or_equal' | 'between';
id: OPERATORS;

/**
* KbnFieldTypes applicable for operator
Expand All @@ -76,26 +89,30 @@ export const isOperator = {
message: strings.getIsOperatorOptionLabel(),
type: FILTERS.PHRASE,
negate: false,
id: OPERATORS.IS,
};

export const isNotOperator = {
message: strings.getIsNotOperatorOptionLabel(),
type: FILTERS.PHRASE,
negate: true,
id: OPERATORS.IS_NOT,
};

export const isOneOfOperator = {
message: strings.getIsOneOfOperatorOptionLabel(),
type: FILTERS.PHRASES,
negate: false,
fieldTypes: ['string', 'number', 'date', 'ip', 'geo_point', 'geo_shape'],
id: OPERATORS.IS_ONE_OF,
};

export const isNotOneOfOperator = {
message: strings.getIsNotOneOfOperatorOptionLabel(),
type: FILTERS.PHRASES,
negate: true,
fieldTypes: ['string', 'number', 'date', 'ip', 'geo_point', 'geo_shape'],
id: OPERATORS.IS_NOT_ONE_OF,
};

const rangeOperatorsSharedProps = {
Expand All @@ -113,41 +130,43 @@ const rangeOperatorsSharedProps = {
export const isBetweenOperator = {
...rangeOperatorsSharedProps,
message: strings.getIsBetweenOperatorOptionLabel(),
id: 'between',
id: OPERATORS.BETWEEN,
negate: false,
};

export const isLessThanOperator = {
...rangeOperatorsSharedProps,
message: strings.getLessThanOperatorOptionLabel(),
id: 'less',
id: OPERATORS.LESS,
negate: false,
};

export const isGreaterOrEqualOperator = {
...rangeOperatorsSharedProps,
message: strings.getIsGreaterOrEqualOperatorOptionLabel(),
id: 'greater_or_equal',
id: OPERATORS.GREATER_OR_EQUAL,
negate: false,
};

export const isNotBetweenOperator = {
...rangeOperatorsSharedProps,
message: strings.getIsNotBetweenOperatorOptionLabel(),
negate: true,
id: 'between',
id: OPERATORS.NOT_BETWEEN,
};

export const existsOperator = {
message: strings.getExistsOperatorOptionLabel(),
type: FILTERS.EXISTS,
negate: false,
id: OPERATORS.EXISTS,
};

export const doesNotExistOperator = {
message: strings.getDoesNotExistOperatorOptionLabel(),
type: FILTERS.EXISTS,
negate: true,
id: OPERATORS.DOES_NOT_EXIST,
};

export const FILTER_OPERATORS: Operator[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../../filter_bar/filter_editor';
import type { Operator } from '../../filter_bar/filter_editor';
import { SuggestionsAbstraction } from '../../typeahead/suggestions_component';
import { OPERATORS } from '../../filter_bar/filter_editor/lib/filter_operators';

export const strings = {
getSelectFieldPlaceholderLabel: () =>
Expand Down Expand Up @@ -106,19 +107,8 @@ export function ParamsEditorInput({
/>
);
case 'range':
switch (operator?.id) {
case 'between':
return (
<RangeValueInput
compressed
field={field!}
value={isRangeParams(params) ? params : undefined}
onChange={onParamsChange}
fullWidth
disabled={disabled}
/>
);
case 'greater_or_equal':
switch (operator.id) {
case OPERATORS.GREATER_OR_EQUAL:
return (
<PhraseValueInput
compressed
Expand All @@ -131,7 +121,7 @@ export function ParamsEditorInput({
disabled={disabled}
/>
);
case 'less':
case OPERATORS.LESS:
return (
<PhraseValueInput
compressed
Expand All @@ -144,6 +134,17 @@ export function ParamsEditorInput({
disabled={disabled}
/>
);
default:
return (
<RangeValueInput
compressed
field={field!}
value={isRangeParams(params) ? params : undefined}
onChange={onParamsChange}
fullWidth
disabled={disabled}
/>
);
}
default:
const placeholderText = getPlaceholderText(Boolean(field), Boolean(operator?.type));
Expand Down

0 comments on commit ab83b8d

Please sign in to comment.