Skip to content

Commit

Permalink
Convert filter_manager/lib to TypeScript / Jest (#45785)
Browse files Browse the repository at this point in the history
* Convert filter_manager/lib to TypeScript / Jest

Fix: #44952

* Update map_query_string.ts

* remove extra ts-ignore

* formatting

* fix PR comments

* Fix PR comments

* fix PR comments

* fix PR comments

* fix merge conflicts

* revert logic

* Fix PR commnets

* add tests for compare_filters

* fix PR comments
  • Loading branch information
alexwizp authored Sep 27, 2019
1 parent a99a5d6 commit 905d021
Show file tree
Hide file tree
Showing 65 changed files with 1,658 additions and 1,330 deletions.
7 changes: 7 additions & 0 deletions packages/kbn-es-query/src/filters/lib/exists_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import { Filter, FilterMeta } from './meta_filter';

export type ExistsFilterMeta = FilterMeta;

export interface FilterExistsProperty {
field: any;
}

export type ExistsFilter = Filter & {
meta: ExistsFilterMeta;
exists?: FilterExistsProperty;
};

export const isExistsFilter = (filter: any): filter is ExistsFilter => filter && filter.exists;
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ export type GeoBoundingBoxFilterMeta = FilterMeta & {

export type GeoBoundingBoxFilter = Filter & {
meta: GeoBoundingBoxFilterMeta;
geo_bounding_box: any;
};

export const isGeoBoundingBoxFilter = (filter: any): filter is GeoBoundingBoxFilter =>
filter && filter.geo_bounding_box;
4 changes: 4 additions & 0 deletions packages/kbn-es-query/src/filters/lib/geo_polygon_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export type GeoPolygonFilterMeta = FilterMeta & {

export type GeoPolygonFilter = Filter & {
meta: GeoPolygonFilterMeta;
geo_polygon: any;
};

export const isGeoPolygonFilter = (filter: any): filter is GeoPolygonFilter =>
filter && filter.geo_polygon;
47 changes: 39 additions & 8 deletions packages/kbn-es-query/src/filters/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,38 @@ export * from './meta_filter';

// The actual filter types
import { CustomFilter } from './custom_filter';
import { ExistsFilter } from './exists_filter';
import { GeoBoundingBoxFilter } from './geo_bounding_box_filter';
import { GeoPolygonFilter } from './geo_polygon_filter';
import { PhraseFilter } from './phrase_filter';
import { PhrasesFilter } from './phrases_filter';
import { QueryStringFilter } from './query_string_filter';
import { RangeFilter } from './range_filter';
import { ExistsFilter, isExistsFilter } from './exists_filter';
import { GeoBoundingBoxFilter, isGeoBoundingBoxFilter } from './geo_bounding_box_filter';
import { GeoPolygonFilter, isGeoPolygonFilter } from './geo_polygon_filter';
import { PhraseFilter, isPhraseFilter, isScriptedPhraseFilter } from './phrase_filter';
import { PhrasesFilter, isPhrasesFilter } from './phrases_filter';
import { QueryStringFilter, isQueryStringFilter } from './query_string_filter';
import { RangeFilter, isRangeFilter, isScriptedRangeFilter } from './range_filter';
import { MatchAllFilter, isMatchAllFilter } from './match_all_filter';
import { MissingFilter, isMissingFilter } from './missing_filter';

export {
CustomFilter,
ExistsFilter,
isExistsFilter,
GeoBoundingBoxFilter,
isGeoBoundingBoxFilter,
GeoPolygonFilter,
isGeoPolygonFilter,
PhraseFilter,
isPhraseFilter,
isScriptedPhraseFilter,
PhrasesFilter,
isPhrasesFilter,
QueryStringFilter,
isQueryStringFilter,
RangeFilter,
isRangeFilter,
isScriptedRangeFilter,
MatchAllFilter,
isMatchAllFilter,
MissingFilter,
isMissingFilter,
};

// Any filter associated with a field (used in the filter bar/editor)
Expand All @@ -47,4 +63,19 @@ export type FieldFilter =
| GeoPolygonFilter
| PhraseFilter
| PhrasesFilter
| RangeFilter;
| RangeFilter
| MatchAllFilter
| MissingFilter;

export enum FILTERS {
CUSTOM = 'custom',
PHRASES = 'phrases',
PHRASE = 'phrase',
EXISTS = 'exists',
MATCH_ALL = 'match_all',
MISSING = 'missing',
QUERY_STRING = 'query_string',
RANGE = 'range',
GEO_BOUNDING_BOX = 'geo_bounding_box',
GEO_POLYGON = 'geo_polygon',
}
33 changes: 33 additions & 0 deletions packages/kbn-es-query/src/filters/lib/match_all_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { Filter, FilterMeta } from './meta_filter';

export interface MatchAllFilterMeta extends FilterMeta {
field: any;
formattedValue: string;
}

export type MatchAllFilter = Filter & {
meta: MatchAllFilterMeta;
match_all: any;
};

export const isMatchAllFilter = (filter: any): filter is MatchAllFilter =>
filter && filter.match_all;
3 changes: 2 additions & 1 deletion packages/kbn-es-query/src/filters/lib/meta_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ export interface FilterMeta {
alias: string | null;
key?: string;
value?: string;
params?: any;
}

export interface Filter {
$state?: FilterState;
meta: FilterMeta;
query?: object;
query?: any;
}

export interface LatLon {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* under the License.
*/

import _ from 'lodash';
import { mapFilter } from './map_filter';
import { Filter, FilterMeta } from './meta_filter';

export function mapAndFlattenFilters(indexPatterns, filters) {
const flattened = _(filters)
.flatten()
.compact()
.map(item => mapFilter(indexPatterns, item)).value();
return Promise.all(flattened);
}
export type MissingFilterMeta = FilterMeta;

export type MissingFilter = Filter & {
meta: MissingFilterMeta;
missing: any;
};

export const isMissingFilter = (filter: any): filter is MissingFilter => filter && filter.missing;
13 changes: 13 additions & 0 deletions packages/kbn-es-query/src/filters/lib/phrase_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@
* under the License.
*/

import { get } from 'lodash';
import { Filter, FilterMeta } from './meta_filter';

export type PhraseFilterMeta = FilterMeta & {
params: {
query: string; // The unformatted value
};
script?: {
script: {
params: any;
};
};
field?: any;
};

export type PhraseFilter = Filter & {
meta: PhraseFilterMeta;
};

export const isPhraseFilter = (filter: any): filter is PhraseFilter =>
filter && (filter.query && filter.query.match);

export const isScriptedPhraseFilter = (filter: any): filter is PhraseFilter =>
Boolean(get(filter, 'script.script.params.value'));
4 changes: 4 additions & 0 deletions packages/kbn-es-query/src/filters/lib/phrases_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import { Filter, FilterMeta } from './meta_filter';

export type PhrasesFilterMeta = FilterMeta & {
params: string[]; // The unformatted values
field?: string;
};

export type PhrasesFilter = Filter & {
meta: PhrasesFilterMeta;
};

export const isPhrasesFilter = (filter: any): filter is PhrasesFilter =>
filter && filter.meta.type === 'phrases';
8 changes: 8 additions & 0 deletions packages/kbn-es-query/src/filters/lib/query_string_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ export type QueryStringFilterMeta = FilterMeta;

export type QueryStringFilter = Filter & {
meta: QueryStringFilterMeta;
query?: {
query_string: {
query: string;
};
};
};

export const isQueryStringFilter = (filter: any): filter is QueryStringFilter =>
filter && filter.query && filter.query.query_string;
36 changes: 33 additions & 3 deletions packages/kbn-es-query/src/filters/lib/range_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,50 @@
* specific language governing permissions and limitations
* under the License.
*/

import { get, keys } from 'lodash';
import { Filter, FilterMeta } from './meta_filter';

export interface RangeFilterParams {
interface FilterRange {
from?: number | string;
to?: number | string;
}

interface FilterRangeGt {
gt?: number | string;
lt?: number | string;
}

interface FilterRangeGte {
gte?: number | string;
lte?: number | string;
lt?: number | string;
}

export type RangeFilterParams = FilterRange & FilterRangeGt & FilterRangeGte;

export type RangeFilterMeta = FilterMeta & {
params: RangeFilterParams;
field?: any;
};

export type RangeFilter = Filter & {
meta: RangeFilterMeta;
script?: {
script: {
params: any;
};
};
range: { [key: string]: RangeFilterParams };
};

const hasRangeKeys = (params: RangeFilterParams) =>
Boolean(
keys(params).find((key: string) => ['gte', 'gt', 'lte', 'lt', 'from', 'to'].includes(key))
);

export const isRangeFilter = (filter: any): filter is RangeFilter => filter && filter.range;

export const isScriptedRangeFilter = (filter: any): filter is RangeFilter => {
const params: RangeFilterParams = get(filter, 'script.script.params', {});

return hasRangeKeys(params);
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export const rangeFilter: RangeFilter = {
$state: {
store: FilterStateStore.APP_STATE,
},
range: {},
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
* under the License.
*/

import { Filter, isFilterPinned, FilterStateStore } from '@kbn/es-query';
import { Filter, isFilterPinned, isRangeFilter, FilterStateStore } from '@kbn/es-query';

import _ from 'lodash';
import { Subject } from 'rxjs';

import { UiSettingsClientContract } from 'src/core/public';
// @ts-ignore

import { compareFilters } from './lib/compare_filters';
// @ts-ignore
import { mapAndFlattenFilters } from './lib/map_and_flatten_filters';
// @ts-ignore
import { uniqFilters } from './lib/uniq_filters';

import { extractTimeFilter } from './lib/extract_time_filter';
import { changeTimeFilter } from './lib/change_time_filter';
import { onlyDisabledFiltersChanged } from './lib/only_disabled';
Expand Down Expand Up @@ -194,7 +191,10 @@ export class FilterManager {

public async addFiltersAndChangeTimeFilter(filters: Filter[]) {
const timeFilter = await extractTimeFilter(this.indexPatterns, filters);
if (timeFilter) changeTimeFilter(this.timefilter, timeFilter);

if (isRangeFilter(timeFilter)) {
changeTimeFilter(this.timefilter, timeFilter);
}
return this.addFilters(filters.filter(filter => filter !== timeFilter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
export { FilterManager } from './filter_manager';
export { FilterStateManager } from './filter_state_manager';

// @ts-ignore
export { uniqFilters } from './lib/uniq_filters';
export { onlyDisabledFiltersChanged } from './lib/only_disabled';

This file was deleted.

Loading

0 comments on commit 905d021

Please sign in to comment.