Skip to content

Commit

Permalink
[Lens] unify expression search context type (#172738)
Browse files Browse the repository at this point in the history
## Summary

While working on #172710 I noticed
how loose our search context types were. This leeway seems like overkill
given how we actually use the expressions framework.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
drewdaemon and kibanamachine authored Dec 11, 2023
1 parent 305b6da commit 570937f
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 85 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-es-query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ export {
isDataViewFieldSubtypeMulti,
isDataViewFieldSubtypeNested,
} from './src/utils';

export type { ExecutionContextSearch } from './src/expressions/types';
16 changes: 16 additions & 0 deletions packages/kbn-es-query/src/expressions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { Filter, Query, TimeRange } from '../filters';

export interface ExecutionContextSearch {
filters?: Filter[];
query?: Query | Query[];
timeRange?: TimeRange;
disableWarningToasts?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import { Datatable, ExecutionContext } from '@kbn/expressions-plugin/common';
import { Adapters } from '@kbn/inspector-plugin/common';
import { SerializableRecord } from '@kbn/utility-types';
import { TrendlineArguments } from '../types';
import { metricTrendlineFunction } from './metric_trendline_function';

const fakeContext = {} as ExecutionContext<Adapters, SerializableRecord>;
const fakeContext = {} as ExecutionContext<Adapters>;
const fakeInput = {} as Datatable;
const metricTrendline = (args: TrendlineArguments) =>
metricTrendlineFunction().fn(fakeInput, args, fakeContext);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/search/expressions/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { i18n } from '@kbn/i18n';
import { ExecutionContext, ExpressionFunctionDefinition } from '@kbn/expressions-plugin/common';
import { Adapters } from '@kbn/inspector-plugin/common';
import { ExpressionValueSearchContext, ExecutionContextSearch } from './kibana_context_type';
import { ExpressionValueSearchContext } from './kibana_context_type';

const toArray = <T>(query: undefined | T | T[]): T[] =>
!query ? [] : Array.isArray(query) ? query : [query];
Expand All @@ -20,7 +20,7 @@ export type ExpressionFunctionKibana = ExpressionFunctionDefinition<
ExpressionValueSearchContext | null,
object,
ExpressionValueSearchContext,
ExecutionContext<Adapters, ExecutionContextSearch>
ExecutionContext<Adapters>
>;

export const kibana: ExpressionFunctionKibana = {
Expand Down
10 changes: 2 additions & 8 deletions src/plugins/data/common/search/expressions/kibana_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

import { ExpressionFunctionDefinition, ExecutionContext } from '@kbn/expressions-plugin/common';
import { Adapters } from '@kbn/inspector-plugin/common';
import {
KibanaTimerangeOutput,
ExecutionContextSearch,
KibanaContext,
KibanaFilter,
KibanaQueryOutput,
} from '../..';
import { KibanaTimerangeOutput, KibanaContext, KibanaFilter, KibanaQueryOutput } from '../..';

interface Arguments {
q?: KibanaQueryOutput[] | null;
Expand All @@ -28,5 +22,5 @@ export type ExpressionFunctionKibanaContext = ExpressionFunctionDefinition<
KibanaContext | null,
Arguments,
Promise<KibanaContext>,
ExecutionContext<Adapters, ExecutionContextSearch>
ExecutionContext<Adapters>
>;
12 changes: 2 additions & 10 deletions src/plugins/data/common/search/expressions/kibana_context_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@
* 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, ExecutionContextSearch } from '@kbn/es-query';
import { ExpressionValueBoxed } from '@kbn/expressions-plugin/common';
import { Query, TimeRange } from '../../query';
import { Query } from '../../query';
import { DataViewField } from '../..';

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

export type ExpressionValueSearchContext = ExpressionValueBoxed<
'kibana_context',
ExecutionContextSearch
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export type {
OptionedValueProp,
ParsedInterval,
// expressions
ExecutionContextSearch,
ExpressionFunctionKql,
ExpressionFunctionLucene,
ExpressionFunctionKibana,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { ExecutionContext } from '@kbn/expressions-plugin/common';
import moment from 'moment';
import { ESCalendarInterval, ESFixedInterval, roundDateToESInterval } from '@elastic/charts';
import { Adapters } from '@kbn/inspector-plugin/common';
import { SerializableRecord } from '@kbn/utility-types';
import { IUiSettingsClient } from '@kbn/core-ui-settings-browser';
import { i18n } from '@kbn/i18n';
import { handleRequest } from './handle_request';
Expand Down Expand Up @@ -71,7 +70,7 @@ export const requestEventAnnotations = (
abortSignal,
getSearchSessionId,
getExecutionContext,
}: ExecutionContext<Adapters, SerializableRecord>,
}: ExecutionContext<Adapters>,
getStartDependencies: () => Promise<FetchEventAnnotationsStartDependencies>
) => {
return defer(async () => {
Expand Down
1 change: 0 additions & 1 deletion src/plugins/event_annotation/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@kbn/core",
"@kbn/expressions-plugin",
"@kbn/data-plugin",
"@kbn/utility-types",
"@kbn/i18n",
"@kbn/data-views-plugin",
"@kbn/inspector-plugin",
Expand Down
7 changes: 2 additions & 5 deletions src/plugins/expressions/common/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Side Public License, v 1.
*/

import type { SerializableRecord } from '@kbn/utility-types';
import type { KibanaRequest } from '@kbn/core/server';
import type { KibanaExecutionContext } from '@kbn/core/public';

import { Adapters, RequestAdapter } from '@kbn/inspector-plugin/common';
import { ExecutionContextSearch } from '@kbn/es-query';
import { Datatable, ExpressionType } from '../expression_types';
import { TablesAdapter } from '../util/tables_adapter';
import { ExpressionsInspectorAdapter } from '../util';
Expand All @@ -19,10 +19,7 @@ import { ExpressionsInspectorAdapter } from '../util';
* `ExecutionContext` is an object available to all functions during a single execution;
* it provides various methods to perform side-effects.
*/
export interface ExecutionContext<
InspectorAdapters extends Adapters = Adapters,
ExecutionContextSearch extends SerializableRecord = SerializableRecord
> {
export interface ExecutionContext<InspectorAdapters extends Adapters = Adapters> {
/**
* Get search context of the expression.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { Observable } from 'rxjs';
import type { Logger } from '@kbn/logging';
import type { SerializableRecord } from '@kbn/utility-types';
import type { KibanaRequest } from '@kbn/core/server';
import type { KibanaExecutionContext } from '@kbn/core/public';

Expand All @@ -19,6 +18,7 @@ import {
VersionedState,
} from '@kbn/kibana-utils-plugin/common';
import { Adapters } from '@kbn/inspector-plugin/common/adapters';
import { ExecutionContextSearch } from '@kbn/es-query';
import { Executor } from '../executor';
import { AnyExpressionRenderDefinition, ExpressionRendererRegistry } from '../expression_renderers';
import { ExpressionAstExpression } from '../ast';
Expand Down Expand Up @@ -130,7 +130,7 @@ export interface ExpressionsServiceSetup {
}

export interface ExpressionExecutionParams {
searchContext?: SerializableRecord;
searchContext?: ExecutionContextSearch;

variables?: Record<string, unknown>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('ExpressionRenderer', () => {
reload$={refreshSubject}
expression=""
debounce={1000}
searchContext={{ from: 'now-15m', to: 'now' }}
searchContext={{ timeRange: { from: 'now-15m', to: 'now' } }}
/>
);

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

import type { SerializableRecord } from '@kbn/utility-types';
import type { KibanaExecutionContext } from '@kbn/core/public';
import { Adapters } from '@kbn/inspector-plugin/public';
import { ExecutionContextSearch } from '@kbn/es-query';
import {
IInterpreterRenderHandlers,
ExpressionValue,
Expand All @@ -35,7 +35,7 @@ export interface ExpressionInterpreter {
}

export interface IExpressionLoaderParams {
searchContext?: SerializableRecord;
searchContext?: ExecutionContextSearch;
context?: ExpressionValue;
variables?: Record<string, unknown>;
// Enables debug tracking on each expression in the AST
Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@kbn/core-execution-context-common",
"@kbn/tinymath",
"@kbn/panel-loader",
"@kbn/es-query",
],
"exclude": [
"target/**/*",
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/vis_types/vega/public/vega_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { get } from 'lodash';
import { i18n } from '@kbn/i18n';
import { ExecutionContextSearch } from '@kbn/data-plugin/public';
import {
ExecutionContext,
ExpressionFunctionDefinition,
Expand Down Expand Up @@ -40,7 +39,7 @@ export type VegaExpressionFunctionDefinition = ExpressionFunctionDefinition<
Input,
Arguments,
Output,
ExecutionContext<VegaInspectorAdapters, ExecutionContextSearch>
ExecutionContext<VegaInspectorAdapters>
>;

export const createVegaFn = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

import { SerializableRecord } from '@kbn/utility-types';
import { functionWrapper } from '@kbn/presentation-util-plugin/test_helpers';
import { getFunctionErrors } from '../../../i18n';
import { csv } from './csv';
Expand Down Expand Up @@ -40,7 +39,7 @@ one,1
two,2
fourty two,42`,
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
)
).toEqual(expected);
});
Expand All @@ -56,7 +55,7 @@ two\t2
fourty two\t42`,
delimiter: '\t',
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
)
).toEqual(expected);

Expand All @@ -70,7 +69,7 @@ two%SPLIT%2
fourty two%SPLIT%42`,
delimiter: '%SPLIT%',
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
)
).toEqual(expected);
});
Expand All @@ -83,7 +82,7 @@ fourty two%SPLIT%42`,
data: `name,number\rone,1\rtwo,2\rfourty two,42`,
newline: '\r',
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
)
).toEqual(expected);
});
Expand All @@ -107,7 +106,7 @@ fourty two%SPLIT%42`,
data: `foo," bar ", baz, " buz "
1,2,3,4`,
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
)
).toEqual(expectedResult);
});
Expand Down Expand Up @@ -135,7 +134,7 @@ fourty two%SPLIT%42`,
1," best ",3, " ok"
" good", bad, better , " worst " `,
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
)
).toEqual(expectedResult);
});
Expand All @@ -150,7 +149,7 @@ one|1
two.2
fourty two,42`,
},
{} as ExecutionContext<Adapters, SerializableRecord>
{} as ExecutionContext<Adapters>
);
}).toThrow(new RegExp(errors.invalidInputCSV().message));
});
Expand Down
Loading

0 comments on commit 570937f

Please sign in to comment.