Skip to content

Commit

Permalink
fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Nov 6, 2019
1 parent 280dc7e commit d60b7a3
Show file tree
Hide file tree
Showing 48 changed files with 94 additions and 102 deletions.
23 changes: 11 additions & 12 deletions packages/kbn-es-query/src/utils/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
* under the License.
*/

import _ from 'lodash';

import { pick, get, reduce, map } from 'lodash';

/** @deprecated
* @see src/plugins/data/public/es_query/filters/phrase_filter.ts
* Code was already moved into src/plugins/data/public.
* This method will be removed after moving es_query into new platform
* This method will be removed after moving 'es_query' into new platform
* */
export const getConvertedValueForField = (field, value) => {
if (typeof value !== 'boolean' && field.type === 'boolean') {
Expand All @@ -41,7 +40,7 @@ export const getConvertedValueForField = (field, value) => {
/** @deprecated
* @see src/plugins/data/public/es_query/filters/phrase_filter.ts
* Code was already moved into src/plugins/data/public.
* This method will be removed after moving es_query into new platform
* This method will be removed after moving 'es_query' into new platform
* */
export const buildInlineScriptForPhraseFilter = (scriptedField) => {
// We must wrap painless scripts in a lambda in case they're more than a simple expression
Expand All @@ -58,7 +57,7 @@ export const buildInlineScriptForPhraseFilter = (scriptedField) => {
/** @deprecated
* @see src/plugins/data/public/es_query/filters/phrase_filter.ts
* Code was already moved into src/plugins/data/public.
* This method will be removed after moving es_query into new platform
* This method will be removed after moving 'es_query' into new platform
* */
export function getPhraseScript(field, value) {
const convertedValue = getConvertedValueForField(field, value);
Expand All @@ -78,7 +77,7 @@ export function getPhraseScript(field, value) {
/** @deprecated
* @see src/plugins/data/public/es_query/filters/range_filter.ts
* Code was already moved into src/plugins/data/public.
* This method will be removed after moving es_query into new platform
* This method will be removed after moving 'kuery' into new platform
* */
export function getRangeScript(field, params) {
const operators = {
Expand All @@ -101,23 +100,23 @@ export function getRangeScript(field, params) {
lt: 'boolean lt(Supplier s, def v) {return s.get().toInstant().isBefore(Instant.parse(v))}',
};

const knownParams = _.pick(params, (val, key) => {
const knownParams = pick(params, (val, key) => {
return key in operators;
});
let script = _.map(knownParams, function (val, key) {
return '(' + field.script + ')' + _.get(operators, key) + key;
let script = map(knownParams, (val, key) => {
return '(' + field.script + ')' + get(operators, key) + key;
}).join(' && ');

// We must wrap painless scripts in a lambda in case they're more than a simple expression
if (field.lang === 'painless') {
const comp = field.type === 'date' ? dateComparators : comparators;
const currentComparators = _.reduce(
const currentComparators = reduce(
knownParams,
(acc, val, key) => acc.concat(_.get(comp, key)),
(acc, val, key) => acc.concat(get(comp, key)),
[]
).join(' ');

const comparisons = _.map(knownParams, function (val, key) {
const comparisons = map(knownParams, (val, key) => {
return `${key}(() -> { ${field.script} }, params.${key})`;
}).join(' && ');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface LatLon {
lon: number;
}

export function buildEmptyFilter(isPinned: boolean, index?: string): Filter {
export const buildEmptyFilter = (isPinned: boolean, index?: string): Filter => {
const meta: FilterMeta = {
disabled: false,
negate: false,
Expand All @@ -65,43 +65,43 @@ export function buildEmptyFilter(isPinned: boolean, index?: string): Filter {
const $state: FilterState = {
store: isPinned ? FilterStateStore.GLOBAL_STATE : FilterStateStore.APP_STATE,
};

return { meta, $state };
}
};

export function isFilterPinned(filter: Filter) {
export const isFilterPinned = (filter: Filter) => {
return filter.$state && filter.$state.store === FilterStateStore.GLOBAL_STATE;
}
};

export function toggleFilterDisabled(filter: Filter) {
export const toggleFilterDisabled = (filter: Filter) => {
const disabled = !filter.meta.disabled;
const meta = { ...filter.meta, disabled };

return { ...filter, meta };
}
};

export function toggleFilterNegated(filter: Filter) {
export const toggleFilterNegated = (filter: Filter) => {
const negate = !filter.meta.negate;
const meta = { ...filter.meta, negate };

return { ...filter, meta };
}
};

export function toggleFilterPinned(filter: Filter) {
export const toggleFilterPinned = (filter: Filter) => {
const store = isFilterPinned(filter) ? FilterStateStore.APP_STATE : FilterStateStore.GLOBAL_STATE;
const $state = { ...filter.$state, store };

return { ...filter, $state };
}
};

export function enableFilter(filter: Filter) {
return !filter.meta.disabled ? filter : toggleFilterDisabled(filter);
}
export const enableFilter = (filter: Filter) =>
!filter.meta.disabled ? filter : toggleFilterDisabled(filter);

export function disableFilter(filter: Filter) {
return filter.meta.disabled ? filter : toggleFilterDisabled(filter);
}
export const disableFilter = (filter: Filter) =>
filter.meta.disabled ? filter : toggleFilterDisabled(filter);

export function pinFilter(filter: Filter) {
return isFilterPinned(filter) ? filter : toggleFilterPinned(filter);
}
export const pinFilter = (filter: Filter) =>
isFilterPinned(filter) ? filter : toggleFilterPinned(filter);

export function unpinFilter(filter: Filter) {
return !isFilterPinned(filter) ? filter : toggleFilterPinned(filter);
}
export const unpinFilter = (filter: Filter) =>
!isFilterPinned(filter) ? filter : toggleFilterPinned(filter);
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export const getPhraseFilterValue = (filter: PhraseFilter): PhraseFilterValue =>
return isPlainObject(queryValue) ? queryValue.query : queryValue;
};

export function buildPhraseFilter(
export const buildPhraseFilter = (
field: Field,
value: any,
indexPattern: IndexPattern
): PhraseFilter {
): PhraseFilter => {
const convertedValue = getConvertedValueForField(field, value);

if (field.scripted) {
Expand All @@ -90,9 +90,9 @@ export function buildPhraseFilter(
},
} as PhraseFilter;
}
}
};

export function getPhraseScript(field: Field, value: string) {
export const getPhraseScript = (field: Field, value: string) => {
const convertedValue = getConvertedValueForField(field, value);
const script = buildInlineScriptForPhraseFilter(field);

Expand All @@ -105,12 +105,12 @@ export function getPhraseScript(field: Field, value: string) {
},
},
};
}
};

// See https://github.com/elastic/elasticsearch/issues/20941 and https://github.com/elastic/kibana/issues/8677
// and https://github.com/elastic/elasticsearch/pull/22201
// for the reason behind this change. Aggs now return boolean buckets with a key of 1 or 0.
export const getConvertedValueForField = (field: Field, value: string) => {
export const getConvertedValueForField = (field: Field, value: any) => {
if (typeof value !== 'boolean' && field.type === 'boolean') {
if ([1, 'true'].includes(value)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const isPhrasesFilter = (filter: any): filter is PhrasesFilter =>

// Creates a filter where the given field matches one or more of the given values
// params should be an array of values
export function buildPhrasesFilter(field: Field, params: any, indexPattern: IndexPattern) {
export const buildPhrasesFilter = (field: Field, params: any, indexPattern: IndexPattern) => {
const index = indexPattern.id;
const type = 'phrases';
const key = field.name;
Expand Down Expand Up @@ -67,4 +67,4 @@ export function buildPhrasesFilter(field: Field, params: any, indexPattern: Inde
},
},
} as PhrasesFilter;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ export const isQueryStringFilter = (filter: any): filter is QueryStringFilter =>
filter && filter.query && filter.query.query_string;

// Creates a filter corresponding to a raw Elasticsearch query DSL object
export function buildQueryFilter(
export const buildQueryFilter = (
query: QueryStringFilter['query'],
index: IndexPattern,
alias: string
) {
return {
) =>
({
query,
meta: {
index,
alias,
},
} as QueryStringFilter;
}
} as QueryStringFilter);
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import _, { get, keys } from 'lodash';
import { map, reduce, mapValues, get, keys, pick } from 'lodash';
import { Filter, FilterMeta } from './meta_filter';
import { Field, IndexPattern } from './types';

Expand Down Expand Up @@ -84,33 +84,27 @@ export const isScriptedRangeFilter = (filter: any): filter is RangeFilter => {
return hasRangeKeys(params);
};

function formatValue(field: Field, params: any[]) {
return _.map(params, (val: any, key: string) => get(operators, key) + format(field, val)).join(
' '
);
}
const formatValue = (field: Field, params: any[]) =>
map(params, (val: any, key: string) => get(operators, key) + format(field, val)).join(' ');

function format(field: Field, value: any) {
return field && field.format && field.format.convert ? field.format.convert(value) : value;
}
const format = (field: Field, value: any) =>
field && field.format && field.format.convert ? field.format.convert(value) : value;

// Creates a filter where the value for the given field is in the given range
// params should be an object containing `lt`, `lte`, `gt`, and/or `gte`
export function buildRangeFilter(
export const buildRangeFilter = (
field: Field,
params: RangeFilterParams,
indexPattern: IndexPattern,
formattedValue?: string
): RangeFilter {
): RangeFilter => {
const filter: any = { meta: { index: indexPattern.id, params: {} } };

if (formattedValue) {
filter.meta.formattedValue = formattedValue;
}

params = _.mapValues(params, value => {
return field.type === 'number' ? parseFloat(value) : value;
});
params = mapValues(params, value => (field.type === 'number' ? parseFloat(value) : value));

if ('gte' in params && 'gt' in params) throw new Error('gte and gt are mutually exclusive');
if ('lte' in params && 'lt' in params) throw new Error('lte and lt are mutually exclusive');
Expand Down Expand Up @@ -143,28 +137,28 @@ export function buildRangeFilter(
}

return filter as RangeFilter;
}
};

export function getRangeScript(field: IndexPattern, params: RangeFilterParams) {
const knownParams = _.pick(params, (val, key: any) => {
return key in operators;
});
let script = _.map(knownParams, function(val: any, key: string) {
return '(' + field.script + ')' + get(operators, key) + key;
}).join(' && ');
export const getRangeScript = (field: IndexPattern, params: RangeFilterParams) => {
const knownParams = pick(params, (val, key: any) => key in operators);
let script = map(
knownParams,
(val: any, key: string) => '(' + field.script + ')' + get(operators, key) + key
).join(' && ');

// We must wrap painless scripts in a lambda in case they're more than a simple expression
if (field.lang === 'painless') {
const comp = field.type === 'date' ? dateComparators : comparators;
const currentComparators = _.reduce(
const currentComparators = reduce(
knownParams,
(acc, val, key) => acc.concat(get(comp, key)),
[]
).join(' ');

const comparisons = _.map(knownParams, function(val, key) {
return `${key}(() -> { ${field.script} }, params.${key})`;
}).join(' && ');
const comparisons = map(
knownParams,
(val, key) => `${key}(() -> { ${field.script} }, params.${key})`
).join(' && ');

script = `${currentComparators}${comparisons}`;
}
Expand All @@ -176,4 +170,4 @@ export function getRangeScript(field: IndexPattern, params: RangeFilterParams) {
lang: field.lang,
},
};
}
};
File renamed without changes.
1 change: 1 addition & 0 deletions src/plugins/data/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
export * from './query';
export * from './field_formats';
export * from './kbn_field_types';
export * from './es_query';

export * from './types';
1 change: 0 additions & 1 deletion src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ export * from './types';

export { IRequestTypesMap, IResponseTypesMap } from './search';
export * from './search';
export * from './es_query';
export * from './query';
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Subscription } from 'rxjs';
import { FilterManager } from './filter_manager';
import { getFilter } from './test_helpers/get_stub_filter';
import { getFiltersArray } from './test_helpers/get_filters_array';
import { esFilters } from '../../es_query';
import { esFilters } from '../../../common/es_query';

import { coreMock } from '../../../../../core/public/mocks';
const setupMock = coreMock.createSetup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { mapAndFlattenFilters } from './lib/map_and_flatten_filters';
import { uniqFilters } from './lib/uniq_filters';
import { onlyDisabledFiltersChanged } from './lib/only_disabled';
import { PartitionedFilters } from './types';
import { esFilters } from '../../es_query';
import { esFilters } from '../../../common/es_query';

export class FilterManager {
private filters: esFilters.Filter[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { compareFilters } from './compare_filters';
import { esFilters } from '../../../es_query';
import { esFilters } from '../../../../common/es_query';

describe('filter manager utilities', () => {
describe('compare filters', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { defaults, isEqual, omit } from 'lodash';
import { esFilters } from '../../../es_query';
import { esFilters } from '../../../../common/es_query';

/**
* Compare two filters to see if they match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { dedupFilters } from './dedup_filters';
import { esFilters } from '../../../es_query';
import { esFilters } from '../../../../common/es_query';

describe('filter manager utilities', () => {
describe('dedupFilters(existing, filters)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { mapDefault } from './map_default';
import { esFilters } from '../../../../es_query';
import { esFilters } from '../../../../../common/es_query';

describe('filter manager utilities', () => {
describe('mapDefault()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { find, keys, get } from 'lodash';
import { esFilters } from '../../../../es_query';
import { esFilters } from '../../../../../common/es_query';

export const mapDefault = (filter: esFilters.Filter) => {
const metaProperty = /(^\$|meta)/;
Expand Down
Loading

0 comments on commit d60b7a3

Please sign in to comment.