Skip to content

Commit

Permalink
[Data Plugin] Allow server-side date formatters to accept custom time…
Browse files Browse the repository at this point in the history
…zone

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.
  • Loading branch information
tsullivan committed Jul 6, 2020
1 parent 89dcdbb commit 6aacfbd
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
BoolFormat,
BytesFormat,
ColorFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
NumberFormat,
Expand All @@ -40,7 +39,6 @@ export const baseFormatters: FieldFormatInstanceType[] = [
BoolFormat,
BytesFormat,
ColorFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
NumberFormat,
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

export { UrlFormat } from './url';
export { BytesFormat } from './bytes';
export { DateNanosFormat } from './date_nanos';
export { RelativeDateFormat } from './relative_date';
export { DurationFormat } from './duration';
export { IpFormat } from './ip';
Expand Down
20 changes: 18 additions & 2 deletions src/plugins/data/common/field_formats/field_formats_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class FieldFormatsRegistry {
protected defaultMap: Record<string, FieldFormatConfig> = {};
protected metaParamsOptions: Record<string, any> = {};
protected getConfig?: FieldFormatsGetConfigFn;
protected customParams: Record<string, any> = {};
// overriden on the public contract
public deserialize: (mapping: SerializedFieldFormat) => IFieldFormat = () => {
return new (FieldFormat.from(identity))();
Expand All @@ -57,6 +58,13 @@ export class FieldFormatsRegistry {
this.metaParamsOptions = metaParamsOptions;
}

/*
* Allow use-case specific params that are reflected in getInstance / getDefaultInstancePlain
*/
setCustomParams(params: Record<string, any>) {
this.customParams = params;
}

/**
* Get the id of the default type for this field type
* using the format:defaultTypeMap config map
Expand Down Expand Up @@ -157,7 +165,11 @@ export class FieldFormatsRegistry {
* @return {FieldFormat}
*/
getInstance = memoize(
(formatId: FieldFormatId, params: Record<string, any> = {}): FieldFormat => {
(formatId: FieldFormatId, instanceParams: Record<string, any> = {}): FieldFormat => {
const params = {
...instanceParams,
...this.customParams,
};
const ConcreteFieldFormat = this.getType(formatId);

if (!ConcreteFieldFormat) {
Expand All @@ -182,8 +194,12 @@ export class FieldFormatsRegistry {
*/
getDefaultInstancePlain(fieldType: KBN_FIELD_TYPES, esTypes?: ES_FIELD_TYPES[]): FieldFormat {
const conf = this.getDefaultConfig(fieldType, esTypes);
const defaultParams = {
...conf.params,
...this.customParams,
};

return this.getInstance(conf.id, conf.params);
return this.getInstance(conf.id, defaultParams);
}
/**
* Returns a cache key built by the given variables for caching in memoized
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export {
BoolFormat,
BytesFormat,
ColorFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
NumberFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/

import { i18n } from '@kbn/i18n';
import moment, { Moment } from 'moment';
import { memoize, noop } from 'lodash';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { FieldFormat } from '../field_format';
import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types';
import moment, { Moment } from 'moment';
import {
FieldFormat,
FIELD_FORMAT_IDS,
KBN_FIELD_TYPES,
TextContextTypeConvert,
} from '../../../common';

/**
* Analyse the given moment.js format pattern for the fractional sec part (S,SS,SSS...)
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { DateFormat } from './date';
export { DateNanosFormat } from './date_nanos';
2 changes: 1 addition & 1 deletion src/plugins/data/public/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
*/

export { FieldFormatsService, FieldFormatsSetup, FieldFormatsStart } from './field_formats_service';
export { DateFormat } from './converters';
export { DateFormat, DateNanosFormat } from './converters';
export { baseFormattersPublic } from './constants';
3 changes: 1 addition & 2 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ import {
BoolFormat,
BytesFormat,
ColorFormat,
DateNanosFormat,
DurationFormat,
IpFormat,
NumberFormat,
Expand All @@ -170,7 +169,7 @@ import {
TruncateFormat,
} from '../common/field_formats';

import { DateFormat } from './field_formats';
import { DateNanosFormat, DateFormat } from './field_formats';
export { baseFormattersPublic } from './field_formats';

// Field formats helpers namespace:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import moment from 'moment-timezone';
import { DateNanosFormat, analysePatternForFract, formatWithNanos } from './date_nanos_server';

describe('Date Nanos Format', () => {
let convert: Function;
let mockConfig: Record<string, any>;

beforeEach(() => {
mockConfig = {};
mockConfig.dateNanosFormat = 'MMMM Do YYYY, HH:mm:ss.SSSSSSSSS';
mockConfig['dateFormat:tz'] = 'Browser';

const getConfig = (key: string) => mockConfig[key];
const date = new DateNanosFormat({}, getConfig);

convert = date.convert.bind(date);
});

test('should inject fractional seconds into formatted timestamp', () => {
[
{
input: '2019-05-20T14:04:56.357001234Z',
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
expected: 'May 20, 2019 @ 14:04:56.357001234',
},
{
input: '2019-05-05T14:04:56.357111234Z',
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
expected: 'May 5, 2019 @ 14:04:56.357111234',
},
{
input: '2019-05-05T14:04:56.357Z',
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
expected: 'May 5, 2019 @ 14:04:56.357000000',
},
{
input: '2019-05-05T14:04:56Z',
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
expected: 'May 5, 2019 @ 14:04:56.000000000',
},
{
input: '2019-05-05T14:04:56.201900001Z',
pattern: 'MMM D, YYYY @ HH:mm:ss SSSS',
expected: 'May 5, 2019 @ 14:04:56 2019',
},
{
input: '2019-05-05T14:04:56.201900001Z',
pattern: 'SSSSSSSSS',
expected: '201900001',
},
].forEach((fixture) => {
const fracPattern = analysePatternForFract(fixture.pattern);
const momentDate = moment(fixture.input).utc();
const value = formatWithNanos(momentDate, fixture.input, fracPattern);
expect(value).toBe(fixture.expected);
});
});

test('decoding an undefined or null date should return an empty string', () => {
expect(convert(null)).toBe('-');
expect(convert(undefined)).toBe('-');
});

test('should clear the memoization cache after changing the date', () => {
function setDefaultTimezone() {
moment.tz.setDefault(mockConfig['dateFormat:tz']);
}

const dateTime = '2019-05-05T14:04:56.201900001Z';

mockConfig['dateFormat:tz'] = 'America/Chicago';
setDefaultTimezone();
const chicagoTime = convert(dateTime);

mockConfig['dateFormat:tz'] = 'America/Phoenix';
setDefaultTimezone();
const phoenixTime = convert(dateTime);

expect(chicagoTime).not.toBe(phoenixTime);
});

test('should return the value itself when it cannot successfully be formatted', () => {
const dateMath = 'now+1M/d';
expect(convert(dateMath)).toBe(dateMath);
});
});

describe('analysePatternForFract', () => {
test('analysePatternForFract using timestamp format containing fractional seconds', () => {
expect(analysePatternForFract('MMM, YYYY @ HH:mm:ss.SSS')).toMatchInlineSnapshot(`
Object {
"length": 3,
"pattern": "MMM, YYYY @ HH:mm:ss.SSS",
"patternEscaped": "MMM, YYYY @ HH:mm:ss.[SSS]",
"patternNanos": "SSS",
}
`);
});

test('analysePatternForFract using timestamp format without fractional seconds', () => {
expect(analysePatternForFract('MMM, YYYY @ HH:mm:ss')).toMatchInlineSnapshot(`
Object {
"length": 0,
"pattern": "MMM, YYYY @ HH:mm:ss",
"patternEscaped": "",
"patternNanos": "",
}
`);
});
});
Loading

0 comments on commit 6aacfbd

Please sign in to comment.