-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Wylie Conlon <wylieconlon@gmail.com> Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
- Loading branch information
1 parent
9ed004c
commit 06278dc
Showing
17 changed files
with
384 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 0 additions & 100 deletions
100
x-pack/plugins/lens/public/editor_frame_service/format_column.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
x-pack/plugins/lens/public/indexpattern_datasource/format_column.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* 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'; | ||
|
||
interface FormatColumn { | ||
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, | ||
FormatColumn, | ||
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'], | ||
fn(input, { format, columnId, decimals, parentFormat }: FormatColumn) { | ||
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) { | ||
const innerParams = (col.meta.params?.params as Record<string, unknown>) ?? {}; | ||
return withParams(col, { | ||
...col.meta.params, | ||
params: { | ||
...innerParams, | ||
...parentFormatParams, | ||
}, | ||
}); | ||
} | ||
} | ||
return col; | ||
}), | ||
}; | ||
}, | ||
}; | ||
|
||
function withParams(col: DatatableColumn, params: Record<string, unknown>) { | ||
return { ...col, meta: { ...col.meta, params } }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.