Skip to content

Commit a45c140

Browse files
[Data Plugin] Allow server-side date formatters to accept custom timezone (#70668)
* [Data Plugin] Allow server-side date formatters to accept custom timezone When Advanced Settings shows the date format timezone to be "Browser," this means nothing to field formatters in the server-side context. The field formatters need a way to accept custom format parameters. This allows a server-side module that creates a FieldFormatMap to set a timezone as a custom parameter. When custom formatting parameters exist, they get combined with the defaults. * add more to tests - need help though * simplify changes * api doc changes * fix src/plugins/data/public/field_formats/constants.ts * rerun api changes * re-use public code in server, add test * fix path for tests * weird api change needed but no real diff * 3td time api doc chagens * move shared code to common Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 71f9dda commit a45c140

20 files changed

Lines changed: 263 additions & 88 deletions

docs/development/plugins/data/public/kibana-plugin-plugins-data-public.baseformatterspublic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
<b>Signature:</b>
88

99
```typescript
10-
baseFormattersPublic: (import("../../common").FieldFormatInstanceType | typeof DateFormat)[]
10+
baseFormattersPublic: (import("../../common").FieldFormatInstanceType | typeof DateNanosFormat | typeof DateFormat)[]
1111
```

docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fieldFormats: {
1313
BoolFormat: typeof BoolFormat;
1414
BytesFormat: typeof BytesFormat;
1515
ColorFormat: typeof ColorFormat;
16-
DateNanosFormat: typeof DateNanosFormat;
1716
DurationFormat: typeof DurationFormat;
1817
IpFormat: typeof IpFormat;
1918
NumberFormat: typeof NumberFormat;

src/plugins/data/common/field_formats/constants/base_formatters.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
BoolFormat,
2424
BytesFormat,
2525
ColorFormat,
26-
DateNanosFormat,
2726
DurationFormat,
2827
IpFormat,
2928
NumberFormat,
@@ -40,7 +39,6 @@ export const baseFormatters: FieldFormatInstanceType[] = [
4039
BoolFormat,
4140
BytesFormat,
4241
ColorFormat,
43-
DateNanosFormat,
4442
DurationFormat,
4543
IpFormat,
4644
NumberFormat,

src/plugins/data/common/field_formats/converters/date_nanos.test.ts renamed to src/plugins/data/common/field_formats/converters/date_nanos_shared.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import moment from 'moment-timezone';
21-
import { DateNanosFormat, analysePatternForFract, formatWithNanos } from './date_nanos';
21+
import { DateNanosFormat, analysePatternForFract, formatWithNanos } from './date_nanos_shared';
2222

2323
describe('Date Nanos Format', () => {
2424
let convert: Function;

src/plugins/data/common/field_formats/converters/date_nanos.ts renamed to src/plugins/data/common/field_formats/converters/date_nanos_shared.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
*/
1919

2020
import { i18n } from '@kbn/i18n';
21-
import moment, { Moment } from 'moment';
2221
import { memoize, noop } from 'lodash';
23-
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
24-
import { FieldFormat } from '../field_format';
25-
import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types';
22+
import moment, { Moment } from 'moment';
23+
import { FieldFormat, FIELD_FORMAT_IDS, KBN_FIELD_TYPES, TextContextTypeConvert } from '../../';
2624

2725
/**
2826
* Analyse the given moment.js format pattern for the fractional sec part (S,SS,SSS...)
@@ -76,9 +74,9 @@ export class DateNanosFormat extends FieldFormat {
7674
});
7775
static fieldType = KBN_FIELD_TYPES.DATE;
7876

79-
private memoizedConverter: Function = noop;
80-
private memoizedPattern: string = '';
81-
private timeZone: string = '';
77+
protected memoizedConverter: Function = noop;
78+
protected memoizedPattern: string = '';
79+
protected timeZone: string = '';
8280

8381
getParamDefaults() {
8482
return {

src/plugins/data/common/field_formats/converters/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
export { UrlFormat } from './url';
2121
export { BytesFormat } from './bytes';
22-
export { DateNanosFormat } from './date_nanos';
2322
export { RelativeDateFormat } from './relative_date';
2423
export { DurationFormat } from './duration';
2524
export { IpFormat } from './ip';

src/plugins/data/common/field_formats/field_formats_registry.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,18 @@ export class FieldFormatsRegistry {
180180
* @param {ES_FIELD_TYPES[]} esTypes
181181
* @return {FieldFormat}
182182
*/
183-
getDefaultInstancePlain(fieldType: KBN_FIELD_TYPES, esTypes?: ES_FIELD_TYPES[]): FieldFormat {
183+
getDefaultInstancePlain(
184+
fieldType: KBN_FIELD_TYPES,
185+
esTypes?: ES_FIELD_TYPES[],
186+
params: Record<string, any> = {}
187+
): FieldFormat {
184188
const conf = this.getDefaultConfig(fieldType, esTypes);
189+
const instanceParams = {
190+
...conf.params,
191+
...params,
192+
};
185193

186-
return this.getInstance(conf.id, conf.params);
194+
return this.getInstance(conf.id, instanceParams);
187195
}
188196
/**
189197
* Returns a cache key built by the given variables for caching in memoized

src/plugins/data/common/field_formats/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export {
2727
BoolFormat,
2828
BytesFormat,
2929
ColorFormat,
30-
DateNanosFormat,
3130
DurationFormat,
3231
IpFormat,
3332
NumberFormat,

src/plugins/data/public/field_formats/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
*/
1919

2020
import { baseFormatters } from '../../common';
21-
import { DateFormat } from './converters/date';
21+
import { DateFormat, DateNanosFormat } from './converters';
2222

23-
export const baseFormattersPublic = [DateFormat, ...baseFormatters];
23+
export const baseFormattersPublic = [DateFormat, DateNanosFormat, ...baseFormatters];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export { DateNanosFormat } from '../../../common/field_formats/converters/date_nanos_shared';

0 commit comments

Comments
 (0)