Skip to content

Commit

Permalink
Merge pull request elastic#20 from stratoula/text-based-language-types
Browse files Browse the repository at this point in the history
Text based language types
  • Loading branch information
stratoula authored Jun 29, 2022
2 parents 2c0730e + 20246b1 commit 8e3f67c
Show file tree
Hide file tree
Showing 71 changed files with 369 additions and 246 deletions.
10 changes: 7 additions & 3 deletions packages/kbn-es-query/src/es_query/build_es_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SerializableRecord } from '@kbn/utility-types';
import { buildQueryFromKuery } from './from_kuery';
import { buildQueryFromFilters } from './from_filters';
import { buildQueryFromLucene } from './from_lucene';
import { Filter, Query } from '../filters';
import { Filter, Query, AggregateQuery } from '../filters';
import { BoolQuery, DataViewBase } from './types';
import type { KueryQueryOptions } from '../kuery';
import type { EsQueryFiltersConfig } from './from_filters';
Expand All @@ -31,6 +31,9 @@ function removeMatchAll<T>(filters: T[]) {
(filter) => !filter || typeof filter !== 'object' || !isEqual(filter, { match_all: {} })
);
}
function isOfQueryType(arg: Query | AggregateQuery): arg is Query {
return Boolean(arg && 'query' in arg);
}

/**
* @param indexPattern
Expand All @@ -44,7 +47,7 @@ function removeMatchAll<T>(filters: T[]) {
*/
export function buildEsQuery(
indexPattern: DataViewBase | undefined,
queries: Query | Query[],
queries: Query | AggregateQuery | Array<Query | AggregateQuery>,
filters: Filter | Filter[],
config: EsQueryConfig = {
allowLeadingWildcards: false,
Expand All @@ -55,7 +58,8 @@ export function buildEsQuery(
queries = Array.isArray(queries) ? queries : [queries];
filters = Array.isArray(filters) ? filters : [filters];

const validQueries = queries.filter((query) => has(query, 'query'));
const isOfQueryTypeQueries = queries.filter(isOfQueryType);
const validQueries = isOfQueryTypeQueries.filter((query) => has(query, 'query'));
const queriesByLanguage = groupBy(validQueries, 'language');
const kueryQuery = buildQueryFromKuery(
indexPattern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { Filter, Query, BoolQuery, TimeRange } from '@kbn/es-query';
import type { Filter, Query, BoolQuery, TimeRange, AggregateQuery } from '@kbn/es-query';
import { FieldSpec, DataView, DataViewField } from '@kbn/data-views-plugin/common';

import { DataControlInput } from '../../types';
Expand Down Expand Up @@ -47,7 +47,7 @@ export type OptionsListRequest = Omit<
runPastTimeout?: boolean;
dataView: DataView;
filters?: Filter[];
query?: Query;
query?: Query | AggregateQuery;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/controls/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { Filter, Query, TimeRange } from '@kbn/es-query';
import type { Filter, Query, TimeRange, AggregateQuery } from '@kbn/es-query';
import { EmbeddableInput } from '@kbn/embeddable-plugin/common/types';

export type ControlWidth = 'small' | 'medium' | 'large';
Expand All @@ -20,7 +20,7 @@ export interface ParentIgnoreSettings {
}

export type ControlInput = EmbeddableInput & {
query?: Query;
query?: Query | AggregateQuery;
filters?: Filter[];
timeRange?: TimeRange;
controlStyle?: ControlStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RangeFilterParams,
Filter,
Query,
AggregateQuery,
} from '@kbn/es-query';
import React from 'react';
import ReactDOM from 'react-dom';
Expand Down Expand Up @@ -246,7 +247,7 @@ export class RangeSliderEmbeddable extends Embeddable<RangeSliderEmbeddableInput
dataView: DataView;
field: DataViewField;
filters: Filter[];
query?: Query;
query?: Query | AggregateQuery;
}) => {
const searchSource = await this.dataService.searchSource.create();
searchSource.setField('size', 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Start as InspectorStartContract } from '@kbn/inspector-plugin/public';

import { ControlGroupContainer } from '@kbn/controls-plugin/public';
import { UiActionsStart } from '../../services/ui_actions';
import { RefreshInterval, TimeRange, Query, Filter } from '../../services/data';
import { RefreshInterval, TimeRange, Query, Filter, AggregateQuery } from '../../services/data';
import {
ViewMode,
Container,
Expand Down Expand Up @@ -70,7 +70,7 @@ interface IndexSignature {

export interface InheritedChildInput extends IndexSignature {
filters: Filter[];
query: Query;
query: Query | AggregateQuery;
timeRange: TimeRange;
refreshConfig?: RefreshInterval;
viewMode: ViewMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { debounceTime, tap } from 'rxjs/operators';
import { compareFilters, COMPARE_ALL_OPTIONS, type Filter } from '@kbn/es-query';
import { replaceUrlHashQuery } from '@kbn/kibana-utils-plugin/public';
import { DashboardContainer } from '../embeddable';
import { Query } from '../../services/data';
import type { Query, AggregateQuery } from '../../services/data';
import { DashboardConstants, DashboardSavedObject } from '../..';
import {
setControlGroupState,
Expand Down Expand Up @@ -41,7 +41,7 @@ type ApplyStateChangesToContainerProps = SyncDashboardContainerCommon & {
};

type ApplyContainerChangesToStateProps = SyncDashboardContainerCommon & {
applyFilters: (query: Query, filters: Filter[]) => void;
applyFilters: (query: Query | AggregateQuery, filters: Filter[]) => void;
};

type SyncDashboardContainerProps = SyncDashboardContainerCommon & ApplyContainerChangesToStateProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import _ from 'lodash';
import { merge } from 'rxjs';
import { debounceTime, finalize, map, switchMap, tap } from 'rxjs/operators';

import { setQuery } from '../state';
import { DashboardBuildContext, DashboardState } from '../../types';
import { DashboardSavedObject } from '../../saved_dashboards';
Expand All @@ -18,6 +17,7 @@ import {
syncQueryStateWithUrl,
connectToQueryState,
Filter,
AggregateQuery,
Query,
waitUntilNextSessionCompletes$,
GlobalQueryStateFromUrl,
Expand Down Expand Up @@ -57,7 +57,7 @@ export const syncDashboardFilterState = ({
});

// this callback will be used any time new filters and query need to be applied.
const applyFilters = (query: Query, filters: Filter[]) => {
const applyFilters = (query: Query | AggregateQuery, filters: Filter[]) => {
savedDashboard.searchSource.setField('query', query);
savedDashboard.searchSource.setField('filter', filters);
dispatchDashboardStateChange(setQuery(query));
Expand All @@ -70,7 +70,7 @@ export const syncDashboardFilterState = ({
);

// starts syncing app filters between dashboard state and filterManager
const intermediateFilterState: { filters: Filter[]; query: Query } = {
const intermediateFilterState: { filters: Filter[]; query: Query | AggregateQuery } = {
query: initialDashboardState.query ?? queryString.getDefaultQuery(),
filters: initialDashboardState.filters ?? [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { PersistableControlGroupInput } from '@kbn/controls-plugin/common';

import { Filter, Query, TimeRange } from '../../services/data';
import { Filter, Query, TimeRange, AggregateQuery } from '../../services/data';
import { ViewMode } from '../../services/embeddable';
import { DashboardOptions, DashboardPanelMap, DashboardState } from '../../types';

Expand Down Expand Up @@ -84,7 +84,10 @@ export const dashboardStateSlice = createSlice({
setViewMode: (state, action: PayloadAction<ViewMode>) => {
state.viewMode = action.payload;
},
setFiltersAndQuery: (state, action: PayloadAction<{ filters: Filter[]; query: Query }>) => {
setFiltersAndQuery: (
state,
action: PayloadAction<{ filters: Filter[]; query: Query | AggregateQuery }>
) => {
state.filters = action.payload.filters;
state.query = action.payload.query;
},
Expand All @@ -97,7 +100,7 @@ export const dashboardStateSlice = createSlice({
setTitle: (state, action: PayloadAction<string>) => {
state.description = action.payload;
},
setQuery: (state, action: PayloadAction<Query>) => {
setQuery: (state, action: PayloadAction<Query | AggregateQuery>) => {
state.query = action.payload;
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/dashboard/public/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { SerializableRecord } from '@kbn/utility-types';
import { flow } from 'lodash';
import type { Filter, TimeRange, Query } from '@kbn/es-query';
import type { Filter, TimeRange, Query, AggregateQuery } from '@kbn/es-query';
import type { GlobalQueryStateFromUrl, RefreshInterval } from '@kbn/data-plugin/public';
import type { LocatorDefinition, LocatorPublic } from '@kbn/share-plugin/public';
import { SerializableControlGroupInput } from '@kbn/controls-plugin/common';
Expand Down Expand Up @@ -65,7 +65,7 @@ export type DashboardAppLocatorParams = {
* Optionally set a query. NOTE: if given and used in conjunction with `dashboardId`, and the
* saved dashboard has a query saved with it, this will _replace_ that query.
*/
query?: Query;
query?: Query | AggregateQuery;
/**
* If not given, will use the uiSettings configuration for `storeInSessionStorage`. useHash determines
* whether to hash the data in the url to avoid url length issues.
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/dashboard/public/services/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* Side Public License, v 1.
*/

export type { Query, TimeRange, Filter } from '@kbn/es-query';
export type { Query, TimeRange, Filter, AggregateQuery } from '@kbn/es-query';
export * from '@kbn/data-plugin/public';
6 changes: 3 additions & 3 deletions src/plugins/dashboard/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { EmbeddableStart } from './services/embeddable';
import { DashboardSessionStorage } from './application/lib';
import { UsageCollectionSetup } from './services/usage_collection';
import { NavigationPublicPluginStart } from './services/navigation';
import { Query, RefreshInterval, TimeRange } from './services/data';
import { Query, RefreshInterval, TimeRange, AggregateQuery } from './services/data';
import { DashboardPanelState, SavedDashboardPanel } from '../common/types';
import { SavedObjectsTaggingApi } from './services/saved_objects_tagging_oss';
import { DataPublicPluginStart, DataViewsContract } from './services/data';
Expand All @@ -58,7 +58,7 @@ export interface DashboardPanelMap {
* DashboardState contains all pieces of tracked state for an individual dashboard
*/
export interface DashboardState {
query: Query;
query: Query | AggregateQuery;
title: string;
tags: string[];
filters: Filter[];
Expand Down Expand Up @@ -96,7 +96,7 @@ export interface DashboardContainerInput extends ContainerInput {
viewMode: ViewMode;
filters: Filter[];
title: string;
query: Query;
query: Query | AggregateQuery;
panels: {
[panelId: string]: DashboardPanelState<EmbeddableInput & { [k: string]: unknown }>;
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/query/query_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { Filter } from '@kbn/es-query';
import type { TimeRange, RefreshInterval } from './timefilter/types';
import type { Query } from './types';
import type { Query, AggregateQuery } from './types';

/**
* All query state service state
Expand All @@ -22,5 +22,5 @@ export type QueryState = {
time?: TimeRange;
refreshInterval?: RefreshInterval;
filters?: Filter[];
query?: Query;
query?: Query | AggregateQuery;
};
4 changes: 2 additions & 2 deletions src/plugins/data/common/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { Query, Filter } from '@kbn/es-query';
import type { Query, Filter, AggregateQuery } from '@kbn/es-query';
import type { RefreshInterval, TimeRange } from './timefilter/types';

export type { RefreshInterval, TimeRange, TimeRangeBounds } from './timefilter/types';
Expand All @@ -24,7 +24,7 @@ export interface SavedQuery {
export interface SavedQueryAttributes {
title: string;
description: string;
query: Query;
query: Query | AggregateQuery;
filters?: Filter[];
timefilter?: SavedQueryTimeFilter;
}
9 changes: 6 additions & 3 deletions src/plugins/data/common/search/expressions/kibana_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition, ExecutionContext } from '@kbn/expressions-plugin/common';
import { Adapters } from '@kbn/inspector-plugin/common';
import { Filter } from '@kbn/es-query';
import { Query, uniqFilters } from '@kbn/es-query';
import { Query, uniqFilters, AggregateQuery } from '@kbn/es-query';
import { unboxExpressionValue } from '@kbn/expressions-plugin/common';
import { SavedObjectReference } from '@kbn/core/types';
import { SavedObjectsClientCommon } from '@kbn/data-views-plugin/common';
Expand Down Expand Up @@ -41,8 +41,11 @@ export type ExpressionFunctionKibanaContext = ExpressionFunctionDefinition<
const getParsedValue = (data: any, defaultValue: any) =>
typeof data === 'string' && data.length ? JSON.parse(data) || defaultValue : defaultValue;

const mergeQueries = (first: Query | Query[] = [], second: Query | Query[]) =>
uniqBy<Query>(
const mergeQueries = (
first: Query | AggregateQuery | Array<Query | AggregateQuery> = [],
second: Query | AggregateQuery | Array<Query | AggregateQuery>
) =>
uniqBy<Query | AggregateQuery>(
[...(Array.isArray(first) ? first : [first]), ...(Array.isArray(second) ? second : [second])],
(n: any) => JSON.stringify(n.query)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Filter } from '@kbn/es-query';
import { Filter, AggregateQuery } from '@kbn/es-query';
import { ExpressionValueBoxed, ExpressionValueFilter } from '@kbn/expressions-plugin/common';
import { Query, TimeRange } from '../../query';
import { adaptToExpressionValueFilter, DataViewField } from '../..';

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type ExecutionContextSearch = {
filters?: Filter[];
query?: Query | Query[];
query?: Query | AggregateQuery | Array<Query | AggregateQuery>;
timeRange?: TimeRange;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { QueryStringManager } from './query_string_manager';
import { Storage } from '@kbn/kibana-utils-plugin/public/storage';
import { StubBrowserStorage } from '@kbn/test-jest-helpers';
import { coreMock } from '@kbn/core/public/mocks';
import { Query } from '../../../common/query';
import { Query, AggregateQuery } from '../../../common/query';

describe('QueryStringManager', () => {
let service: QueryStringManager;
Expand All @@ -24,7 +24,7 @@ describe('QueryStringManager', () => {

test('getUpdates$ is a cold emits only after query changes', () => {
const obs$ = service.getUpdates$();
const emittedValues: Query[] = [];
const emittedValues: Array<Query | AggregateQuery> = [];
obs$.subscribe((v) => {
emittedValues.push(v);
});
Expand Down
26 changes: 8 additions & 18 deletions src/plugins/data/public/query/query_string/query_string_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ import { BehaviorSubject } from 'rxjs';
import { skip } from 'rxjs/operators';
import { PublicMethodsOf } from '@kbn/utility-types';
import { CoreStart } from '@kbn/core/public';
import type { Query } from '@kbn/es-query';
import type { Query, AggregateQuery } from '@kbn/es-query';
import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public';
import { isEqual } from 'lodash';
import { KIBANA_USER_QUERY_LANGUAGE_KEY, UI_SETTINGS } from '../../../common';

function isOfQueryType(arg: any): arg is Query {
return Boolean(arg && 'query' in arg);
}

export class QueryStringManager {
private query$: BehaviorSubject<Query>;
private query$: BehaviorSubject<Query | AggregateQuery>;

constructor(
private readonly storage: IStorageWrapper,
private readonly uiSettings: CoreStart['uiSettings']
) {
this.query$ = new BehaviorSubject<Query>(this.getDefaultQuery());
this.query$ = new BehaviorSubject<Query | AggregateQuery>(this.getDefaultQuery());
}

private getDefaultLanguage() {
Expand All @@ -43,7 +39,7 @@ export class QueryStringManager {
};
}

public formatQuery(query: Query | string | undefined): Query {
public formatQuery(query: Query | AggregateQuery | string | undefined): Query | AggregateQuery {
if (!query) {
return this.getDefaultQuery();
} else if (typeof query === 'string') {
Expand All @@ -60,23 +56,17 @@ export class QueryStringManager {
return this.query$.asObservable().pipe(skip(1));
};

public getQuery = (): Query => {
public getQuery = (): Query | AggregateQuery => {
return this.query$.getValue();
};

/**
* Updates the query.
* @param {Query} query
* @param {Query | AggregateQuery} query
*/
public setQuery = (query: Query) => {
public setQuery = (query: Query | AggregateQuery) => {
const curQuery = this.query$.getValue();
const isOfTypeQuery = isOfQueryType(query);
if (
(isOfTypeQuery && query?.language !== curQuery.language) ||
query?.query !== curQuery.query
) {
this.query$.next(query);
} else if (!isOfTypeQuery && !isEqual(query, curQuery)) {
if (!isEqual(query, curQuery)) {
this.query$.next(query);
}
};
Expand Down
Loading

0 comments on commit 8e3f67c

Please sign in to comment.