Skip to content

Commit

Permalink
feat: update deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nlatipov committed Apr 25, 2022
1 parent 6f35ee3 commit c1f0e57
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* Side Public License, v 1.
*/

import { IFieldType, indexPatterns as dataViewsUtils } from '@kbn/data-plugin/public';
import { indexPatterns as dataViewsUtils } from '@kbn/data-plugin/public';
import type { DataViewField } from '@kbn/data-views-plugin/public';
import { flatten } from 'lodash';
import { escapeKuery } from './lib/escape_kuery';
import { escapeKuery } from '@kbn/es-query';
import { sortPrefixFirst } from './sort_prefix_first';
import { QuerySuggestionField, QuerySuggestionTypes } from '../query_suggestion_provider';
import { KqlQuerySuggestionProvider } from './types';

const keywordComparator = (first: IFieldType, second: IFieldType) => {
const keywordComparator = (first: DataViewField, second: DataViewField) => {
const extensions = ['raw', 'keyword'];
if (extensions.map((ext) => `${first.name}.${ext}`).includes(second.name)) {
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
* Side Public License, v 1.
*/

import { escapeKuery } from '@kbn/es-query';

/**
* Escapes backslashes and double-quotes. (Useful when putting a string in quotes to use as a value
* in a KQL expression. See the QuotedCharacter rule in kuery.peg.)
*/
export function escapeQuotes(str: string) {
return str.replace(/[\\"]/g, '\\$&');
}

// Re-export this function from the @kbn/es-query package to avoid refactoring
export { escapeKuery };
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { flatten } from 'lodash';
import { CoreSetup } from '@kbn/core/public';
import { IFieldType, IIndexPattern } from '@kbn/data-plugin/public';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { escapeQuotes } from './lib/escape_kuery';
import { KqlQuerySuggestionProvider } from './types';
import type { UnifiedSearchPublicPluginStart } from '../../../types';
Expand Down Expand Up @@ -36,7 +36,7 @@ export const setupGetValueSuggestions: KqlQuerySuggestionProvider = (
): Promise<QuerySuggestion[]> => {
const fullFieldName = nestedPath ? `${nestedPath}.${fieldName}` : fieldName;

const dataViewFieldEntries: Array<[IIndexPattern, IFieldType]> = [];
const dataViewFieldEntries: Array<[DataView, DataViewField]> = [];
dataViews.forEach((dataView) => {
dataView.fields
.filter((field) => field.name === fullFieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { ValueSuggestionsMethod } from '@kbn/data-plugin/common';
import { IFieldType, IIndexPattern } from '@kbn/data-plugin/common';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';

export enum QuerySuggestionTypes {
Field = 'field',
Expand All @@ -24,7 +24,7 @@ export type QuerySuggestionGetFn = (
/** @public **/
export interface QuerySuggestionGetFnArgs {
language: string;
indexPatterns: IIndexPattern[];
indexPatterns: DataView[];
query: string;
selectionStart: number;
selectionEnd: number;
Expand All @@ -47,7 +47,7 @@ export interface QuerySuggestionBasic {
/** @public **/
export interface QuerySuggestionField extends QuerySuggestionBasic {
type: QuerySuggestionTypes.Field;
field: IFieldType;
field: DataViewField;
}

/** @public **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,10 @@ describe('FieldSuggestions', () => {
useTimeRange: false,
});

const customDataView = {
...stubDataView,
const customDataView = Object.assign({}, stubDataView, {
title: 'customDataView',
useTimeRange: false,
};
});

await getValueSuggestions({
indexPattern: customDataView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,24 @@ import { CoreSetup } from '@kbn/core/public';
import dateMath from '@kbn/datemath';
import { buildQueryFromFilters } from '@kbn/es-query';
import { memoize } from 'lodash';
import {
IIndexPattern,
IFieldType,
UI_SETTINGS,
ValueSuggestionsMethod,
} from '@kbn/data-plugin/common';
import { UI_SETTINGS, ValueSuggestionsMethod } from '@kbn/data-plugin/common';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { TimefilterSetup } from '@kbn/data-plugin/public';
import { AutocompleteUsageCollector } from '../collectors';

export type ValueSuggestionsGetFn = (args: ValueSuggestionsGetFnArgs) => Promise<any[]>;

interface ValueSuggestionsGetFnArgs {
indexPattern: IIndexPattern;
field: IFieldType;
indexPattern: DataView;
field: DataViewField;
query: string;
useTimeRange?: boolean;
boolFilter?: any[];
signal?: AbortSignal;
method?: ValueSuggestionsMethod;
}

const getAutocompleteTimefilter = ({ timefilter }: TimefilterSetup, dataView: IIndexPattern) => {
const getAutocompleteTimefilter = ({ timefilter }: TimefilterSetup, dataView: DataView) => {
const timeRange = timefilter.getTime();

// Use a rounded timerange so that memoizing works properly
Expand All @@ -51,7 +47,7 @@ export const setupValueSuggestionProvider = (
usageCollector,
}: { timefilter: TimefilterSetup; usageCollector?: AutocompleteUsageCollector }
): ValueSuggestionsGetFn => {
function resolver(title: string, field: IFieldType, query: string, filters: any[]) {
function resolver(title: string, field: DataViewField, query: string, filters: any[]) {
// Only cache results for a minute
const ttl = Math.floor(Date.now() / 1000 / 60);
return [ttl, query, title, field.name, JSON.stringify(filters)].join('|');
Expand All @@ -60,7 +56,7 @@ export const setupValueSuggestionProvider = (
const requestSuggestions = memoize(
<T = unknown>(
index: string,
field: IFieldType,
field: DataViewField,
query: string,
filters: any = [],
signal?: AbortSignal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { get } from 'lodash';
import React, { Component } from 'react';
import { XJsonLang } from '@kbn/monaco';
import { DataView, IFieldType } from '@kbn/data-views-plugin/common';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { getIndexPatternFromFilter as getDataViewFromFilter } from '@kbn/data-plugin/public';
import { CodeEditor } from '@kbn/kibana-react-plugin/public';
import { GenericComboBox, GenericComboBoxProps } from './generic_combo_box';
Expand All @@ -59,7 +59,7 @@ export interface Props {

interface State {
selectedDataView?: DataView;
selectedField?: IFieldType;
selectedField?: DataViewField;
selectedOperator?: Operator;
params: any;
useCustomLabel: boolean;
Expand Down Expand Up @@ -438,7 +438,7 @@ class FilterEditorUI extends Component<Props, State> {
this.setState({ selectedDataView, selectedField, selectedOperator, params });
};

private onFieldChange = ([selectedField]: IFieldType[]) => {
private onFieldChange = ([selectedField]: DataViewField[]) => {
const selectedOperator = undefined;
const params = undefined;
this.setState({ selectedField, selectedOperator, params });
Expand Down Expand Up @@ -520,7 +520,7 @@ function DataViewComboBox(props: GenericComboBoxProps<DataView>) {
return GenericComboBox(props);
}

function FieldComboBox(props: GenericComboBoxProps<IFieldType>) {
function FieldComboBox(props: GenericComboBoxProps<DataViewField>) {
return GenericComboBox(props);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import dateMath from '@kbn/datemath';
import { Filter, FieldFilter } from '@kbn/es-query';
import { ES_FIELD_TYPES } from '@kbn/field-types';
import isSemverValid from 'semver/functions/valid';
import { isFilterable, IFieldType, IpAddress } from '@kbn/data-plugin/common';
import { DataView } from '@kbn/data-views-plugin/common';
import { isFilterable, IpAddress } from '@kbn/data-plugin/common';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { FILTER_OPERATORS, Operator } from './filter_operators';

export function getFieldFromFilter(filter: FieldFilter, dataView: DataView) {
Expand All @@ -28,15 +28,15 @@ export function getFilterableFields(dataView: DataView) {
return dataView.fields.filter(isFilterable);
}

export function getOperatorOptions(field: IFieldType) {
export function getOperatorOptions(field: DataViewField) {
return FILTER_OPERATORS.filter((operator) => {
if (operator.field) return operator.field(field);
if (operator.fieldTypes) return operator.fieldTypes.includes(field.type);
return true;
});
}

export function validateParams(params: any, field: IFieldType) {
export function validateParams(params: any, field: DataViewField) {
switch (field.type) {
case 'date':
const moment = typeof params === 'string' ? dateMath.parse(params) : null;
Expand All @@ -59,7 +59,7 @@ export function validateParams(params: any, field: IFieldType) {

export function isFilterValid(
dataView?: DataView,
field?: IFieldType,
field?: DataViewField,
operator?: Operator,
params?: any
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { i18n } from '@kbn/i18n';
import { FILTERS } from '@kbn/es-query';
import { ES_FIELD_TYPES } from '@kbn/field-types';
import { IFieldType } from '@kbn/data-views-plugin/common';
import type { DataViewField } from '@kbn/data-views-plugin/public';

export interface Operator {
message: string;
Expand All @@ -25,7 +25,7 @@ export interface Operator {
* A filter predicate for a field,
* takes precedence over {@link fieldTypes}
*/
field?: (field: IFieldType) => boolean;
field?: (field: DataViewField) => boolean;
}

export const isOperator = {
Expand Down Expand Up @@ -68,7 +68,7 @@ export const isBetweenOperator = {
}),
type: FILTERS.RANGE,
negate: false,
field: (field: IFieldType) => {
field: (field: DataViewField) => {
if (['number', 'number_range', 'date', 'date_range', 'ip', 'ip_range'].includes(field.type))
return true;

Expand All @@ -84,7 +84,7 @@ export const isNotBetweenOperator = {
}),
type: FILTERS.RANGE,
negate: true,
field: (field: IFieldType) => {
field: (field: DataViewField) => {
if (['number', 'number_range', 'date', 'date_range', 'ip', 'ip_range'].includes(field.type))
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import React from 'react';
import { withKibana, KibanaReactContextValue } from '@kbn/kibana-react-plugin/public';
import { IFieldType, UI_SETTINGS } from '@kbn/data-plugin/common';
import { DataView } from '@kbn/data-views-plugin/common';
import { UI_SETTINGS } from '@kbn/data-plugin/common';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { IDataPluginServices } from '@kbn/data-plugin/public';
import { debounce } from 'lodash';

Expand All @@ -18,7 +18,7 @@ import { getAutocomplete } from '../../services';
export interface PhraseSuggestorProps {
kibana: KibanaReactContextValue<IDataPluginServices>;
indexPattern: DataView;
field: IFieldType;
field: DataViewField;
timeRangeForSuggestionsOverride?: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { InjectedIntl, injectI18n } from '@kbn/i18n-react';
import { get } from 'lodash';
import React from 'react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { IFieldType } from '@kbn/data-plugin/common';
import type { DataViewField } from '@kbn/data-views-plugin/public';
import { ValueInputType } from './value_input_type';

interface RangeParams {
Expand All @@ -23,7 +23,7 @@ interface RangeParams {
type RangeParamsPartial = Partial<RangeParams>;

interface Props {
field: IFieldType;
field: DataViewField;
value?: RangeParams;
onChange: (params: RangeParamsPartial) => void;
intl: InjectedIntl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { EuiFieldNumber, EuiFieldText, EuiSelect } from '@elastic/eui';
import { InjectedIntl, injectI18n } from '@kbn/i18n-react';
import { isEmpty } from 'lodash';
import React, { Component } from 'react';
import { IFieldType } from '@kbn/data-views-plugin/common';
import type { DataViewField } from '@kbn/data-views-plugin/public';
import { validateParams } from './lib/filter_editor_utils';

interface Props {
value?: string | number;
field: IFieldType;
field: DataViewField;
onChange: (value: string | number | boolean) => void;
onBlur?: (value: string | number | boolean) => void;
placeholder: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { coreMock } from '@kbn/core/server/mocks';
import { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server';
import { ConfigSchema } from '../../config';
import type { DeeplyMockedKeys } from '@kbn/utility-types/jest';
import type { FieldSpec } from '@kbn/data-views-plugin/common';
import { termsAggSuggestions } from './terms_agg';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { duration } from 'moment';
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('terms agg suggestions', () => {
'fieldName',
'query',
[],
{ name: 'field_name', type: 'string' }
{ name: 'field_name', type: 'string' } as FieldSpec
);

const [[args]] = esClientMock.search.mock.calls;
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/unified_search/server/autocomplete/terms_agg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import { get, map } from 'lodash';
import { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { IFieldType, getFieldSubtypeNested } from '@kbn/data-plugin/common';
import { getFieldSubtypeNested } from '@kbn/data-plugin/common';
import type { FieldSpec } from '@kbn/data-views-plugin/common';
import { ConfigSchema } from '../../config';
import { findIndexPatternById as findDataViewById, getFieldByName } from '../data_views';

Expand All @@ -21,7 +22,7 @@ export async function termsAggSuggestions(
fieldName: string,
query: string,
filters?: estypes.QueryDslQueryContainer[],
field?: IFieldType,
field?: FieldSpec,
abortSignal?: AbortSignal
) {
const autocompleteSearchOptions = {
Expand Down Expand Up @@ -54,11 +55,11 @@ export async function termsAggSuggestions(
async function getBody(
// eslint-disable-next-line @typescript-eslint/naming-convention
{ timeout, terminate_after }: Record<string, any>,
field: IFieldType | string,
field: FieldSpec | string,
query: string,
filters: estypes.QueryDslQueryContainer[] = []
) {
const isFieldObject = (f: any): f is IFieldType => Boolean(f && f.name);
const isFieldObject = (f: any): f is FieldSpec => Boolean(f && f.name);

// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#_standard_operators
const getEscapedQuery = (q: string = '') =>
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/unified_search/server/autocomplete/terms_enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { IFieldType } from '@kbn/data-plugin/common';
import type { FieldSpec } from '@kbn/data-views-plugin/common';
import { findIndexPatternById as findDataViewById, getFieldByName } from '../data_views';
import { ConfigSchema } from '../../config';

Expand All @@ -20,7 +20,7 @@ export async function termsEnumSuggestions(
fieldName: string,
query: string,
filters?: estypes.QueryDslQueryContainer[],
field?: IFieldType,
field?: FieldSpec,
abortSignal?: AbortSignal
) {
const { tiers } = config.autocomplete.valueSuggestions;
Expand Down

0 comments on commit c1f0e57

Please sign in to comment.