2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
2020-12-10 15:40:50 +01:00
|
|
|
import {
|
|
|
|
EsaggsExpressionFunctionDefinition,
|
|
|
|
IndexPatternLoadExpressionFunctionDefinition,
|
|
|
|
} from '../../../../../src/plugins/data/public';
|
|
|
|
import {
|
|
|
|
buildExpression,
|
|
|
|
buildExpressionFunction,
|
|
|
|
ExpressionAstExpression,
|
|
|
|
ExpressionAstExpressionBuilder,
|
|
|
|
ExpressionAstFunction,
|
|
|
|
} from '../../../../../src/plugins/expressions/public';
|
2019-10-09 21:20:55 +02:00
|
|
|
import { IndexPatternColumn } from './indexpattern';
|
2019-10-16 03:07:33 +02:00
|
|
|
import { operationDefinitionMap } from './operations';
|
2020-11-20 19:42:12 +01:00
|
|
|
import { IndexPattern, IndexPatternPrivateState, IndexPatternLayer } from './types';
|
2019-10-16 05:02:18 +02:00
|
|
|
import { OriginalColumn } from './rename_columns';
|
2020-04-30 02:10:14 +02:00
|
|
|
import { dateHistogramOperation } from './operations/definitions';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-12-10 15:40:50 +01:00
|
|
|
function getExpressionForLayer(
|
|
|
|
layer: IndexPatternLayer,
|
|
|
|
indexPattern: IndexPattern
|
|
|
|
): ExpressionAstExpression | null {
|
2020-11-20 19:42:12 +01:00
|
|
|
const { columns, columnOrder } = layer;
|
2019-09-17 20:57:53 +02:00
|
|
|
if (columnOrder.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:08:58 +02:00
|
|
|
const columnEntries = columnOrder.map((colId) => [colId, columns[colId]] as const);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
if (columnEntries.length) {
|
2020-12-10 15:40:50 +01:00
|
|
|
const aggs: ExpressionAstExpressionBuilder[] = [];
|
|
|
|
const expressions: ExpressionAstFunction[] = [];
|
2020-11-20 19:42:12 +01:00
|
|
|
columnEntries.forEach(([colId, col]) => {
|
|
|
|
const def = operationDefinitionMap[col.operationType];
|
|
|
|
if (def.input === 'fullReference') {
|
|
|
|
expressions.push(...def.toExpression(layer, colId, indexPattern));
|
|
|
|
} else {
|
2020-12-10 15:40:50 +01:00
|
|
|
aggs.push(
|
|
|
|
buildExpression({ type: 'expression', chain: [def.toEsAggsFn(col, colId, indexPattern)] })
|
|
|
|
);
|
2020-11-20 19:42:12 +01:00
|
|
|
}
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
|
2020-05-05 21:59:32 +02:00
|
|
|
const idMap = columnEntries.reduce((currentIdMap, [colId, column], index) => {
|
2019-11-13 17:00:02 +01:00
|
|
|
return {
|
|
|
|
...currentIdMap,
|
2020-10-29 15:48:49 +01:00
|
|
|
[`col-${columnEntries.length === 1 ? 0 : index}-${colId}`]: {
|
2020-05-05 21:59:32 +02:00
|
|
|
...column,
|
2019-11-13 17:00:02 +01:00
|
|
|
id: colId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, {} as Record<string, OriginalColumn>);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-10-27 18:08:01 +01:00
|
|
|
type FormattedColumn = Required<
|
|
|
|
Extract<
|
|
|
|
IndexPatternColumn,
|
|
|
|
| {
|
|
|
|
params?: {
|
|
|
|
format: unknown;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// when formatters are nested there's a slightly different format
|
|
|
|
| {
|
|
|
|
params: {
|
|
|
|
format?: unknown;
|
|
|
|
parentFormat?: unknown;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>;
|
2020-04-16 16:32:58 +02:00
|
|
|
const columnsWithFormatters = columnEntries.filter(
|
2020-10-27 18:08:01 +01:00
|
|
|
([, col]) =>
|
|
|
|
col.params &&
|
|
|
|
(('format' in col.params && col.params.format) ||
|
|
|
|
('parentFormat' in col.params && col.params.parentFormat))
|
2020-04-16 16:32:58 +02:00
|
|
|
) as Array<[string, FormattedColumn]>;
|
2020-12-10 15:40:50 +01:00
|
|
|
const formatterOverrides: ExpressionAstFunction[] = columnsWithFormatters.map(
|
2020-10-27 18:08:01 +01:00
|
|
|
([id, col]: [string, FormattedColumn]) => {
|
|
|
|
// TODO: improve the type handling here
|
|
|
|
const parentFormat = 'parentFormat' in col.params ? col.params!.parentFormat! : undefined;
|
|
|
|
const format = (col as FormattedColumn).params!.format;
|
|
|
|
|
2020-12-10 15:40:50 +01:00
|
|
|
const base: ExpressionAstFunction = {
|
2020-10-27 18:08:01 +01:00
|
|
|
type: 'function',
|
|
|
|
function: 'lens_format_column',
|
2020-04-16 16:32:58 +02:00
|
|
|
arguments: {
|
2020-10-27 18:08:01 +01:00
|
|
|
format: format ? [format.id] : [''],
|
|
|
|
columnId: [id],
|
|
|
|
decimals: typeof format?.params?.decimals === 'number' ? [format.params.decimals] : [],
|
|
|
|
parentFormat: parentFormat ? [JSON.stringify(parentFormat)] : [],
|
2020-04-16 16:32:58 +02:00
|
|
|
},
|
|
|
|
};
|
2020-10-27 18:08:01 +01:00
|
|
|
|
|
|
|
return base;
|
2020-04-16 16:32:58 +02:00
|
|
|
}
|
2020-10-27 18:08:01 +01:00
|
|
|
);
|
2020-04-16 16:32:58 +02:00
|
|
|
|
2020-11-30 16:08:19 +01:00
|
|
|
const firstDateHistogramColumn = columnEntries.find(
|
|
|
|
([, col]) => col.operationType === 'date_histogram'
|
|
|
|
);
|
|
|
|
|
|
|
|
const columnsWithTimeScale = firstDateHistogramColumn
|
|
|
|
? columnEntries.filter(
|
|
|
|
([, col]) =>
|
|
|
|
col.timeScale &&
|
|
|
|
operationDefinitionMap[col.operationType].timeScalingMode &&
|
|
|
|
operationDefinitionMap[col.operationType].timeScalingMode !== 'disabled'
|
|
|
|
)
|
|
|
|
: [];
|
2020-12-10 15:40:50 +01:00
|
|
|
const timeScaleFunctions: ExpressionAstFunction[] = columnsWithTimeScale.flatMap(
|
2020-11-30 16:08:19 +01:00
|
|
|
([id, col]) => {
|
2020-12-10 15:40:50 +01:00
|
|
|
const scalingCall: ExpressionAstFunction = {
|
2020-11-30 16:08:19 +01:00
|
|
|
type: 'function',
|
|
|
|
function: 'lens_time_scale',
|
|
|
|
arguments: {
|
|
|
|
dateColumnId: [firstDateHistogramColumn![0]],
|
|
|
|
inputColumnId: [id],
|
|
|
|
outputColumnId: [id],
|
|
|
|
targetUnit: [col.timeScale!],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-12-10 15:40:50 +01:00
|
|
|
const formatCall: ExpressionAstFunction = {
|
2020-11-30 16:08:19 +01:00
|
|
|
type: 'function',
|
|
|
|
function: 'lens_format_column',
|
|
|
|
arguments: {
|
|
|
|
format: [''],
|
|
|
|
columnId: [id],
|
|
|
|
parentFormat: [JSON.stringify({ id: 'suffix', params: { unit: col.timeScale } })],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return [scalingCall, formatCall];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-04-30 02:10:14 +02:00
|
|
|
const allDateHistogramFields = Object.values(columns)
|
2020-05-22 09:08:58 +02:00
|
|
|
.map((column) =>
|
2020-04-30 02:10:14 +02:00
|
|
|
column.operationType === dateHistogramOperation.type ? column.sourceField : null
|
|
|
|
)
|
|
|
|
.filter((field): field is string => Boolean(field));
|
|
|
|
|
2020-04-16 16:32:58 +02:00
|
|
|
return {
|
|
|
|
type: 'expression',
|
|
|
|
chain: [
|
2020-12-10 15:40:50 +01:00
|
|
|
buildExpressionFunction<EsaggsExpressionFunctionDefinition>('esaggs', {
|
|
|
|
index: buildExpression([
|
|
|
|
buildExpressionFunction<IndexPatternLoadExpressionFunctionDefinition>(
|
|
|
|
'indexPatternLoad',
|
|
|
|
{ id: indexPattern.id }
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
aggs,
|
|
|
|
metricsAtAllLevels: false,
|
|
|
|
partialRows: false,
|
|
|
|
timeFields: allDateHistogramFields,
|
|
|
|
}).toAst(),
|
2020-04-16 16:32:58 +02:00
|
|
|
{
|
|
|
|
type: 'function',
|
|
|
|
function: 'lens_rename_columns',
|
|
|
|
arguments: {
|
|
|
|
idMap: [JSON.stringify(idMap)],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...formatterOverrides,
|
2020-11-20 19:42:12 +01:00
|
|
|
...expressions,
|
2020-11-30 16:08:19 +01:00
|
|
|
...timeScaleFunctions,
|
2020-04-16 16:32:58 +02:00
|
|
|
],
|
|
|
|
};
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function toExpression(state: IndexPatternPrivateState, layerId: string) {
|
|
|
|
if (state.layers[layerId]) {
|
|
|
|
return getExpressionForLayer(
|
2020-11-20 19:42:12 +01:00
|
|
|
state.layers[layerId],
|
|
|
|
state.indexPatterns[state.layers[layerId].indexPatternId]
|
2019-09-17 20:57:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|