Skip to content

Commit

Permalink
Move isFilterable to NP
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Nov 20, 2019
1 parent 6b66b2b commit 5ccf9bf
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@

import dateMath from '@elastic/datemath';
import { Ipv4Address } from '../../../../../../../../plugins/kibana_utils/public';
import { isFilterable } from '../../../../index_patterns';
import { FILTER_OPERATORS, Operator } from './filter_operators';
import { esFilters, IIndexPattern, IFieldType } from '../../../../../../../../plugins/data/public';
import {
esFilters,
IIndexPattern,
IFieldType,
isFilterable,
} from '../../../../../../../../plugins/data/public';

export function getFieldFromFilter(filter: esFilters.FieldFilter, indexPattern: IIndexPattern) {
return indexPattern.fields.find(field => field.name === filter.meta.key);
Expand Down
1 change: 0 additions & 1 deletion src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
isFilterable,
IndexPatternSelect,
validateIndexPattern,
ILLEGAL_CHARACTERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export {
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
isFilterable,
validateIndexPattern,
} from './utils';

Expand Down
48 changes: 0 additions & 48 deletions src/legacy/core_plugins/data/public/index_patterns/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@ import {
CONTAINS_SPACES,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
isFilterable,
validateIndexPattern,
} from './utils';

import { Field } from './fields';

const mockField = {
name: 'foo',
scripted: false,
searchable: true,
type: 'string',
} as Field;

describe('Index Pattern Utils', () => {
describe('Validation', () => {
it('should not allow space in the pattern', () => {
Expand All @@ -52,42 +42,4 @@ describe('Index Pattern Utils', () => {
expect(validateIndexPattern('my-pattern-*')).toEqual({});
});
});

describe('isFilterable', () => {
describe('types', () => {
it('should return true for filterable types', () => {
['string', 'number', 'date', 'ip', 'boolean'].forEach(type => {
expect(isFilterable({ ...mockField, type })).toBe(true);
});
});

it('should return false for filterable types if the field is not searchable', () => {
['string', 'number', 'date', 'ip', 'boolean'].forEach(type => {
expect(isFilterable({ ...mockField, type, searchable: false })).toBe(false);
});
});

it('should return false for un-filterable types', () => {
[
'geo_point',
'geo_shape',
'attachment',
'murmur3',
'_source',
'unknown',
'conflict',
].forEach(type => {
expect(isFilterable({ ...mockField, type })).toBe(false);
});
});
});

it('should return true for scripted fields', () => {
expect(isFilterable({ ...mockField, scripted: true, searchable: false })).toBe(true);
});

it('should return true for the _id field', () => {
expect(isFilterable({ ...mockField, name: '_id' })).toBe(true);
});
});
});
12 changes: 0 additions & 12 deletions src/legacy/core_plugins/data/public/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import { find, get } from 'lodash';

import { getFilterableKbnTypeNames, IFieldType } from '../../../../../plugins/data/public';

import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';

export const ILLEGAL_CHARACTERS = 'ILLEGAL_CHARACTERS';
Expand Down Expand Up @@ -106,16 +104,6 @@ export function validateIndexPattern(indexPattern: string) {
return errors;
}

const filterableTypes = getFilterableKbnTypeNames();

export function isFilterable(field: IFieldType): boolean {
return (
field.name === '_id' ||
field.scripted ||
Boolean(field.searchable && filterableTypes.includes(field.type))
);
}

export function getFromSavedObject(savedObject: any) {
if (get(savedObject, 'attributes.fields') === undefined) {
return;
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/index_patterns/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
isFilterable,
IndexPatternSelect,
validateIndexPattern,
ILLEGAL_CHARACTERS,
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
isFilterable,
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/index_patterns/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export * from './types';
export { isFilterable } from './utils';
30 changes: 30 additions & 0 deletions src/plugins/data/common/index_patterns/fields/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 { getFilterableKbnTypeNames, IFieldType } from '../..';

const filterableTypes = getFilterableKbnTypeNames();

export function isFilterable(field: IFieldType): boolean {
return (
field.name === '_id' ||
field.scripted ||
Boolean(field.searchable && filterableTypes.includes(field.type))
);
}
60 changes: 60 additions & 0 deletions src/plugins/data/common/index_patterns/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 { isFilterable } from '.';
import { IFieldType } from './fields';

const mockField = {
name: 'foo',
scripted: false,
searchable: true,
type: 'string',
} as IFieldType;

describe('isFilterable', () => {
describe('types', () => {
it('should return true for filterable types', () => {
['string', 'number', 'date', 'ip', 'boolean'].forEach(type => {
expect(isFilterable({ ...mockField, type })).toBe(true);
});
});

it('should return false for filterable types if the field is not searchable', () => {
['string', 'number', 'date', 'ip', 'boolean'].forEach(type => {
expect(isFilterable({ ...mockField, type, searchable: false })).toBe(false);
});
});

it('should return false for un-filterable types', () => {
['geo_point', 'geo_shape', 'attachment', 'murmur3', '_source', 'unknown', 'conflict'].forEach(
type => {
expect(isFilterable({ ...mockField, type })).toBe(false);
}
);
});
});

it('should return true for scripted fields', () => {
expect(isFilterable({ ...mockField, scripted: true, searchable: false })).toBe(true);
});

it('should return true for the _id field', () => {
expect(isFilterable({ ...mockField, name: '_id' })).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from 'react';
import { flatten } from 'lodash';
import { escapeKuery } from './escape_kuery';
import { sortPrefixFirst } from 'ui/utils/sort_prefix_first';
import { isFilterable } from 'ui/index_patterns';
import { isFilterable } from '../../../../../../src/plugins/data/public';
import { FormattedMessage } from '@kbn/i18n/react';


Expand Down

0 comments on commit 5ccf9bf

Please sign in to comment.