Skip to content

Commit

Permalink
refactor: 💡 improve Lens definitions of expression functions
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 5, 2020
1 parent 47d0906 commit 45fb211
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const toArray = <T>(query: undefined | T | T[]): T[] =>

export type ExpressionFunctionKibana = ExpressionFunctionDefinition<
'kibana',
// TODO: Get rid of the `null` type below.
ExpressionValueSearchContext | null,
object,
ExpressionValueSearchContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
* under the License.
*/

import { TimeRange, Query, esFilters } from 'src/plugins/data/common';
import { ExpressionValueBoxed } from '../types';
import { ExecutionContextSearch } from '../../execution/types';

const name = 'kibana_context';
export type KIBANA_CONTEXT_NAME = 'kibana_context';
export type ExpressionValueSearchContext = ExpressionValueBoxed<
'kibana_context',
ExecutionContextSearch
>;

export interface KibanaContext {
type: typeof name;
query?: Query | Query[];
filters?: esFilters.Filter[];
timeRange?: TimeRange;
}
// TODO: These two are exported for legacy reasons - remove them eventually.
export type KIBANA_CONTEXT_NAME = 'kibana_context';
export type KibanaContext = ExpressionValueSearchContext;

export const kibanaContext = {
name,
name: 'kibana_context',
from: {
null: () => {
return {
type: name,
type: 'kibana_context',
};
},
},
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/expressions/common/expression_types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@
* under the License.
*/

import { ExecutionContextSearch } from '../execution';

export type ExpressionValueUnboxed = any;

export type ExpressionValueBoxed<Type extends string = string, Value extends object = object> = {
type: Type;
} & Value;

export type ExpressionValueSearchContext = ExpressionValueBoxed<
'kibana_context',
ExecutionContextSearch
>;

export type ExpressionValue = ExpressionValueUnboxed | ExpressionValueBoxed;

export type ExpressionValueConverter<I extends ExpressionValue, O extends ExpressionValue> = (
Expand Down
16 changes: 9 additions & 7 deletions src/plugins/expressions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,42 +73,44 @@ export {
ExpressionValueConverter,
ExpressionValueError,
ExpressionValueNum,
ExpressionValueRender,
ExpressionValueSearchContext,
ExpressionValueUnboxed,
Filter,
Font,
FontLabel,
FontStyle,
FontValue,
FontWeight,
format,
formatExpression,
FunctionsRegistry,
IRegistry,
IInterpreterRenderHandlers,
InterpreterErrorType,
IRegistry,
KIBANA_CONTEXT_NAME,
KibanaContext,
KibanaDatatable,
KibanaDatatableColumn,
KibanaDatatableRow,
KnownTypeToString,
Overflow,
parse,
parseExpression,
PointSeries,
PointSeriesColumn,
PointSeriesColumnName,
PointSeriesColumns,
PointSeriesRow,
Range,
ExpressionValueRender as Render,
SerializedDatatable,
SerializedFieldFormat,
Style,
TextAlignment,
TextDecoration,
TypesRegistry,
TypeString,
TypeToString,
TypesRegistry,
UnmappedTypeStrings,
parse,
parseExpression,
format,
formatExpression,
ExpressionValueRender as Render,
} from '../common';
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface DatatableColumns {
}

interface Args {
title: string;
columns: DatatableColumns;
}

Expand All @@ -42,7 +43,7 @@ export const datatable: ExpressionFunctionDefinition<
KibanaDatatable,
Args,
DatatableRender
> = ({
> = {
name: 'lens_datatable',
type: 'render',
help: i18n.translate('xpack.lens.datatable.expressionHelpLabel', {
Expand All @@ -56,30 +57,28 @@ export const datatable: ExpressionFunctionDefinition<
}),
},
columns: {
types: ['lens_datatable_columns'],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
types: ['lens_datatable_columns'] as any,
help: '',
},
},
context: {
types: ['lens_multitable'],
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputTypes: ['lens_multitable'] as any,
fn(data: KibanaDatatable, args: Args) {
return {
type: 'render',
as: 'lens_datatable_renderer',
value: {
data,
data: {
...data,
type: 'lens_multitable',
tables: {},
},
args,
},
};
},
// TODO the typings currently don't support custom type args. As soon as they do, this can be removed
} as unknown) as ExpressionFunctionDefinition<
'lens_datatable',
KibanaDatatable,
Args,
DatatableRender
>;
};

type DatatableColumnsResult = DatatableColumns & { type: 'lens_datatable_columns' };

Expand All @@ -93,17 +92,15 @@ export const datatableColumns: ExpressionFunctionDefinition<
aliases: [],
type: 'lens_datatable_columns',
help: '',
context: {
types: ['null'],
},
inputTypes: ['null'],
args: {
columnIds: {
types: ['string'],
multi: true,
help: '',
},
},
fn: function fn(_context: unknown, args: DatatableColumns) {
fn: function fn(input: unknown, args: DatatableColumns) {
return {
type: 'lens_datatable_columns',
...args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { i18n } from '@kbn/i18n';
import {
ExpressionFunctionDefinition,
KibanaContext,
ExpressionValueSearchContext,
KibanaDatatable,
} from 'src/plugins/expressions/public';
import { LensMultiTable } from '../types';
Expand All @@ -20,7 +20,7 @@ interface MergeTables {

export const mergeTables: ExpressionFunctionDefinition<
'lens_merge_tables',
KibanaContext | null,
ExpressionValueSearchContext | null,
MergeTables,
LensMultiTable
> = {
Expand All @@ -41,28 +41,26 @@ export const mergeTables: ExpressionFunctionDefinition<
multi: true,
},
},
context: {
types: ['kibana_context', 'null'],
},
fn(ctx, { layerIds, tables }: MergeTables) {
inputTypes: ['kibana_context', 'null'],
fn(input, { layerIds, tables }) {
const resultTables: Record<string, KibanaDatatable> = {};
tables.forEach((table, index) => {
resultTables[layerIds[index]] = table;
});
return {
type: 'lens_multitable',
tables: resultTables,
dateRange: getDateRange(ctx),
dateRange: getDateRange(input),
};
},
};

function getDateRange(ctx?: KibanaContext | null) {
if (!ctx || !ctx.timeRange) {
function getDateRange(value?: ExpressionValueSearchContext | null) {
if (!value || !value.timeRange) {
return;
}

const dateRange = toAbsoluteDates({ fromDate: ctx.timeRange.from, toDate: ctx.timeRange.to });
const dateRange = toAbsoluteDates({ fromDate: value.timeRange.from, toDate: value.timeRange.to });

if (!dateRange) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,16 @@ export const autoDate: ExpressionFunctionDefinition<
name: 'lens_auto_date',
aliases: [],
help: '',
context: {
types: ['kibana_context', 'null'],
},
inputTypes: ['kibana_context', 'null'],
args: {
aggConfigs: {
types: ['string'],
default: '""',
help: '',
},
},
fn(ctx: KibanaContext, args: LensAutoDateProps) {
const interval = autoIntervalFromContext(ctx);
fn(input, args) {
const interval = autoIntervalFromContext(input);

if (!interval) {
return args.aggConfigs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ export const renameColumns: ExpressionFunctionDefinition<
}),
},
},
context: {
types: ['kibana_datatable'],
},
fn(data: KibanaDatatable, { idMap: encodedIdMap }: RemapArgs) {
inputTypes: ['kibana_datatable'],
fn(data, { idMap: encodedIdMap }) {
const idMap = JSON.parse(encodedIdMap) as Record<string, OriginalColumn>;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ export const metricChart: ExpressionFunctionDefinition<
'The display mode of the chart - reduced will only show the metric itself without min size',
},
},
context: {
types: ['lens_multitable'],
},
inputTypes: ['lens_multitable'],
fn(data, args) {
return {
type: 'render',
Expand Down
18 changes: 6 additions & 12 deletions x-pack/legacy/plugins/lens/public/xy_visualization_plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export const legendConfig: ExpressionFunctionDefinition<
aliases: [],
type: 'lens_xy_legendConfig',
help: `Configure the xy chart's legend`,
context: {
types: ['null'],
},
inputTypes: ['null'],
args: {
isVisible: {
types: ['boolean'],
Expand All @@ -52,7 +50,7 @@ export const legendConfig: ExpressionFunctionDefinition<
}),
},
},
fn: function fn(_context: unknown, args: LegendConfig) {
fn: function fn(input: unknown, args: LegendConfig) {
return {
type: 'lens_xy_legendConfig',
...args,
Expand Down Expand Up @@ -99,17 +97,15 @@ export const xConfig: ExpressionFunctionDefinition<
aliases: [],
type: 'lens_xy_xConfig',
help: `Configure the xy chart's x axis`,
context: {
types: ['null'],
},
inputTypes: ['null'],
args: {
...axisConfig,
accessor: {
types: ['string'],
help: 'The column to display on the x axis.',
},
},
fn: function fn(_context: unknown, args: XConfig) {
fn: function fn(input: unknown, args: XConfig) {
return {
type: 'lens_xy_xConfig',
...args,
Expand All @@ -129,9 +125,7 @@ export const layerConfig: ExpressionFunctionDefinition<
aliases: [],
type: 'lens_xy_layer',
help: `Configure a layer in the xy chart`,
context: {
types: ['null'],
},
inputTypes: ['null'],
args: {
...axisConfig,
layerId: {
Expand Down Expand Up @@ -177,7 +171,7 @@ export const layerConfig: ExpressionFunctionDefinition<
help: 'JSON key-value pairs of column ID to label',
},
},
fn: function fn(_context: unknown, args: LayerArgs) {
fn: function fn(input: unknown, args: LayerArgs) {
return {
type: 'lens_xy_layer',
...args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ export const xyChart: ExpressionFunctionDefinition<
multi: true,
},
},
context: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
types: ['lens_multitable', 'kibana_context', 'null'] as any,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputTypes: ['lens_multitable', 'kibana_context', 'null'] as any,
fn(data: LensMultiTable, args: XYArgs) {
return {
type: 'render',
Expand Down

0 comments on commit 45fb211

Please sign in to comment.