[Lens] Fix escaping of field names (#63509)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
af09fedaf2
commit
c21cec7abd
|
@ -257,12 +257,54 @@ describe('IndexPattern Data Source', () => {
|
|||
const state = stateFromPersistedState(queryPersistedState);
|
||||
|
||||
expect(indexPatternDatasource.toExpression(state, 'first')).toMatchInlineSnapshot(`
|
||||
"esaggs
|
||||
index=\\"1\\"
|
||||
metricsAtAllLevels=false
|
||||
partialRows=false
|
||||
includeFormatHints=true
|
||||
aggConfigs={lens_auto_date aggConfigs='[{\\"id\\":\\"col1\\",\\"enabled\\":true,\\"type\\":\\"count\\",\\"schema\\":\\"metric\\",\\"params\\":{}},{\\"id\\":\\"col2\\",\\"enabled\\":true,\\"type\\":\\"date_histogram\\",\\"schema\\":\\"segment\\",\\"params\\":{\\"field\\":\\"timestamp\\",\\"useNormalizedEsInterval\\":true,\\"interval\\":\\"1d\\",\\"drop_partials\\":false,\\"min_doc_count\\":0,\\"extended_bounds\\":{}}}]'} | lens_rename_columns idMap='{\\"col-0-col1\\":{\\"label\\":\\"Count of records\\",\\"dataType\\":\\"number\\",\\"isBucketed\\":false,\\"sourceField\\":\\"Records\\",\\"operationType\\":\\"count\\",\\"id\\":\\"col1\\"},\\"col-1-col2\\":{\\"label\\":\\"Date\\",\\"dataType\\":\\"date\\",\\"isBucketed\\":true,\\"operationType\\":\\"date_histogram\\",\\"sourceField\\":\\"timestamp\\",\\"params\\":{\\"interval\\":\\"1d\\"},\\"id\\":\\"col2\\"}}' "
|
||||
Object {
|
||||
"chain": Array [
|
||||
Object {
|
||||
"arguments": Object {
|
||||
"aggConfigs": Array [
|
||||
Object {
|
||||
"chain": Array [
|
||||
Object {
|
||||
"arguments": Object {
|
||||
"aggConfigs": Array [
|
||||
"[{\\"id\\":\\"col1\\",\\"enabled\\":true,\\"type\\":\\"count\\",\\"schema\\":\\"metric\\",\\"params\\":{}},{\\"id\\":\\"col2\\",\\"enabled\\":true,\\"type\\":\\"date_histogram\\",\\"schema\\":\\"segment\\",\\"params\\":{\\"field\\":\\"timestamp\\",\\"useNormalizedEsInterval\\":true,\\"interval\\":\\"1d\\",\\"drop_partials\\":false,\\"min_doc_count\\":0,\\"extended_bounds\\":{}}}]",
|
||||
],
|
||||
},
|
||||
"function": "lens_auto_date",
|
||||
"type": "function",
|
||||
},
|
||||
],
|
||||
"type": "expression",
|
||||
},
|
||||
],
|
||||
"includeFormatHints": Array [
|
||||
true,
|
||||
],
|
||||
"index": Array [
|
||||
"1",
|
||||
],
|
||||
"metricsAtAllLevels": Array [
|
||||
false,
|
||||
],
|
||||
"partialRows": Array [
|
||||
false,
|
||||
],
|
||||
},
|
||||
"function": "esaggs",
|
||||
"type": "function",
|
||||
},
|
||||
Object {
|
||||
"arguments": Object {
|
||||
"idMap": Array [
|
||||
"{\\"col-0-col1\\":{\\"label\\":\\"Count of records\\",\\"dataType\\":\\"number\\",\\"isBucketed\\":false,\\"sourceField\\":\\"Records\\",\\"operationType\\":\\"count\\",\\"id\\":\\"col1\\"},\\"col-1-col2\\":{\\"label\\":\\"Date\\",\\"dataType\\":\\"date\\",\\"isBucketed\\":true,\\"operationType\\":\\"date_histogram\\",\\"sourceField\\":\\"timestamp\\",\\"params\\":{\\"interval\\":\\"1d\\"},\\"id\\":\\"col2\\"}}",
|
||||
],
|
||||
},
|
||||
"function": "lens_rename_columns",
|
||||
"type": "function",
|
||||
},
|
||||
],
|
||||
"type": "expression",
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import { Ast, ExpressionFunctionAST } from '@kbn/interpreter/common';
|
||||
import { IndexPatternColumn } from './indexpattern';
|
||||
import { operationDefinitionMap } from './operations';
|
||||
import { IndexPattern, IndexPatternPrivateState } from './types';
|
||||
|
@ -14,7 +15,7 @@ function getExpressionForLayer(
|
|||
indexPattern: IndexPattern,
|
||||
columns: Record<string, IndexPatternColumn>,
|
||||
columnOrder: string[]
|
||||
) {
|
||||
): Ast | null {
|
||||
if (columnOrder.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
@ -40,29 +41,70 @@ function getExpressionForLayer(
|
|||
};
|
||||
}, {} as Record<string, OriginalColumn>);
|
||||
|
||||
const formatterOverrides = columnEntries
|
||||
.map(([id, col]) => {
|
||||
const format = col.params && 'format' in col.params ? col.params.format : undefined;
|
||||
if (!format) {
|
||||
return null;
|
||||
}
|
||||
const base = `| lens_format_column format="${format.id}" columnId="${id}"`;
|
||||
if (typeof format.params?.decimals === 'number') {
|
||||
return base + ` decimals=${format.params.decimals}`;
|
||||
}
|
||||
return base;
|
||||
})
|
||||
.filter(expr => !!expr)
|
||||
.join(' ');
|
||||
type FormattedColumn = Required<Extract<IndexPatternColumn, { params?: { format: unknown } }>>;
|
||||
|
||||
return `esaggs
|
||||
index="${indexPattern.id}"
|
||||
metricsAtAllLevels=false
|
||||
partialRows=false
|
||||
includeFormatHints=true
|
||||
aggConfigs={lens_auto_date aggConfigs='${JSON.stringify(
|
||||
aggs
|
||||
)}'} | lens_rename_columns idMap='${JSON.stringify(idMap)}' ${formatterOverrides}`;
|
||||
const columnsWithFormatters = columnEntries.filter(
|
||||
([, col]) => col.params && 'format' in col.params && col.params.format
|
||||
) as Array<[string, FormattedColumn]>;
|
||||
const formatterOverrides: ExpressionFunctionAST[] = columnsWithFormatters.map(([id, col]) => {
|
||||
const format = (col as FormattedColumn).params!.format;
|
||||
const base: ExpressionFunctionAST = {
|
||||
type: 'function',
|
||||
function: 'lens_format_column',
|
||||
arguments: {
|
||||
format: [format.id],
|
||||
columnId: [id],
|
||||
},
|
||||
};
|
||||
if (typeof format.params?.decimals === 'number') {
|
||||
return {
|
||||
...base,
|
||||
arguments: {
|
||||
...base.arguments,
|
||||
decimals: [format.params.decimals],
|
||||
},
|
||||
};
|
||||
}
|
||||
return base;
|
||||
});
|
||||
|
||||
return {
|
||||
type: 'expression',
|
||||
chain: [
|
||||
{
|
||||
type: 'function',
|
||||
function: 'esaggs',
|
||||
arguments: {
|
||||
index: [indexPattern.id],
|
||||
metricsAtAllLevels: [false],
|
||||
partialRows: [false],
|
||||
includeFormatHints: [true],
|
||||
aggConfigs: [
|
||||
{
|
||||
type: 'expression',
|
||||
chain: [
|
||||
{
|
||||
type: 'function',
|
||||
function: 'lens_auto_date',
|
||||
arguments: {
|
||||
aggConfigs: [JSON.stringify(aggs)],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'function',
|
||||
function: 'lens_rename_columns',
|
||||
arguments: {
|
||||
idMap: [JSON.stringify(idMap)],
|
||||
},
|
||||
},
|
||||
...formatterOverrides,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
Loading…
Reference in a new issue