2020-10-27 18:08:01 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {
|
|
|
|
ExpressionFunctionDefinition,
|
|
|
|
Datatable,
|
|
|
|
DatatableColumn,
|
|
|
|
} from 'src/plugins/expressions/public';
|
|
|
|
|
2020-11-23 13:03:23 +01:00
|
|
|
export interface FormatColumnArgs {
|
2020-10-27 18:08:01 +01:00
|
|
|
format: string;
|
|
|
|
columnId: string;
|
|
|
|
decimals?: number;
|
|
|
|
parentFormat?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const supportedFormats: Record<
|
|
|
|
string,
|
|
|
|
{ decimalsToPattern: (decimals?: number) => string }
|
|
|
|
> = {
|
|
|
|
number: {
|
|
|
|
decimalsToPattern: (decimals = 2) => {
|
|
|
|
if (decimals === 0) {
|
|
|
|
return `0,0`;
|
|
|
|
}
|
|
|
|
return `0,0.${'0'.repeat(decimals)}`;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
percent: {
|
|
|
|
decimalsToPattern: (decimals = 2) => {
|
|
|
|
if (decimals === 0) {
|
|
|
|
return `0,0%`;
|
|
|
|
}
|
|
|
|
return `0,0.${'0'.repeat(decimals)}%`;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bytes: {
|
|
|
|
decimalsToPattern: (decimals = 2) => {
|
|
|
|
if (decimals === 0) {
|
|
|
|
return `0,0b`;
|
|
|
|
}
|
|
|
|
return `0,0.${'0'.repeat(decimals)}b`;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const formatColumn: ExpressionFunctionDefinition<
|
|
|
|
'lens_format_column',
|
|
|
|
Datatable,
|
2020-11-23 13:03:23 +01:00
|
|
|
FormatColumnArgs,
|
2020-10-27 18:08:01 +01:00
|
|
|
Datatable
|
|
|
|
> = {
|
|
|
|
name: 'lens_format_column',
|
|
|
|
type: 'datatable',
|
|
|
|
help: '',
|
|
|
|
args: {
|
|
|
|
format: {
|
|
|
|
types: ['string'],
|
|
|
|
help: '',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
columnId: {
|
|
|
|
types: ['string'],
|
|
|
|
help: '',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
decimals: {
|
|
|
|
types: ['number'],
|
|
|
|
help: '',
|
|
|
|
},
|
|
|
|
parentFormat: {
|
|
|
|
types: ['string'],
|
|
|
|
help: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputTypes: ['datatable'],
|
2020-11-23 13:03:23 +01:00
|
|
|
fn(input, { format, columnId, decimals, parentFormat }: FormatColumnArgs) {
|
2020-10-27 18:08:01 +01:00
|
|
|
return {
|
|
|
|
...input,
|
|
|
|
columns: input.columns.map((col) => {
|
|
|
|
if (col.id === columnId) {
|
|
|
|
if (!parentFormat) {
|
|
|
|
if (supportedFormats[format]) {
|
|
|
|
return withParams(col, {
|
|
|
|
id: format,
|
|
|
|
params: { pattern: supportedFormats[format].decimalsToPattern(decimals) },
|
|
|
|
});
|
|
|
|
} else if (format) {
|
|
|
|
return withParams(col, { id: format });
|
|
|
|
} else {
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedParentFormat = JSON.parse(parentFormat);
|
|
|
|
const parentFormatId = parsedParentFormat.id;
|
|
|
|
const parentFormatParams = parsedParentFormat.params ?? {};
|
|
|
|
|
|
|
|
if (!parentFormatId) {
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format && supportedFormats[format]) {
|
|
|
|
return withParams(col, {
|
|
|
|
id: parentFormatId,
|
|
|
|
params: {
|
|
|
|
id: format,
|
|
|
|
params: {
|
|
|
|
pattern: supportedFormats[format].decimalsToPattern(decimals),
|
|
|
|
},
|
|
|
|
...parentFormatParams,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (parentFormatParams) {
|
2020-11-12 16:49:47 +01:00
|
|
|
// if original format is already a nested one, we are just replacing the wrapper params
|
|
|
|
// otherwise wrapping it inside parentFormatId/parentFormatParams
|
|
|
|
const isNested = isNestedFormat(col.meta.params);
|
|
|
|
const innerParams = isNested
|
|
|
|
? col.meta.params?.params
|
|
|
|
: { id: col.meta.params?.id, params: col.meta.params?.params };
|
|
|
|
|
|
|
|
const formatId = isNested ? col.meta.params?.id : parentFormatId;
|
|
|
|
|
2020-10-27 18:08:01 +01:00
|
|
|
return withParams(col, {
|
|
|
|
...col.meta.params,
|
2020-11-12 16:49:47 +01:00
|
|
|
id: formatId,
|
2020-10-27 18:08:01 +01:00
|
|
|
params: {
|
|
|
|
...innerParams,
|
|
|
|
...parentFormatParams,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return col;
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-12 16:49:47 +01:00
|
|
|
function isNestedFormat(params: DatatableColumn['meta']['params']) {
|
|
|
|
// if there is a nested params object with an id, it's a nested format
|
|
|
|
return !!params?.params?.id;
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:08:01 +01:00
|
|
|
function withParams(col: DatatableColumn, params: Record<string, unknown>) {
|
|
|
|
return { ...col, meta: { ...col.meta, params } };
|
|
|
|
}
|