Skip to content

Commit

Permalink
Cache all nested formatters (#140480)
Browse files Browse the repository at this point in the history
* cache all nested formatters

* fix tests
  • Loading branch information
flash1293 authored Sep 13, 2022
1 parent 886d61a commit 340c6fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('getAggsFormats', () => {
);
expect(format.convert({ to: '2020-06-01' })).toBe('Before 2020-06-01');
expect(format.convert({ from: '2020-06-01' })).toBe('After 2020-06-01');
expect(getFormat).toHaveBeenCalledTimes(3);
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('date_range does not crash on empty value', () => {
Expand All @@ -62,7 +62,7 @@ describe('getAggsFormats', () => {
expect(format.convert({ type: 'range', to: '10.0.0.10' })).toBe('-Infinity to 10.0.0.10');
expect(format.convert({ type: 'range', from: '10.0.0.10' })).toBe('10.0.0.10 to Infinity');
format.convert({ type: 'mask', mask: '10.0.0.1/24' });
expect(getFormat).toHaveBeenCalledTimes(4);
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('ip_range does not crash on empty value', () => {
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('getAggsFormats', () => {
expect(format.convert('machine.os.keyword')).toBe('machine.os.keyword');
expect(format.convert('__other__')).toBe(mapping.params.otherBucketLabel);
expect(format.convert('__missing__')).toBe(mapping.params.missingBucketLabel);
expect(getFormat).toHaveBeenCalledTimes(3);
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('uses a default separator for multi terms', () => {
Expand Down
52 changes: 26 additions & 26 deletions src/plugins/data/common/search/aggs/utils/get_aggs_formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IFieldFormat,
SerializedFieldFormat,
} from '@kbn/field-formats-plugin/common';
import { SerializableRecord } from '@kbn/utility-types';
import { DateRange } from '../../expressions';
import { convertDateRangeToString } from '../buckets/lib/date_range';
import { convertIPRangeToString, IpRangeKey } from '../buckets/lib/ip_range';
Expand All @@ -35,8 +36,21 @@ type GetFieldFormat = (mapping: SerializedFieldFormat) => IFieldFormat;
* @internal
*/
export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInstanceType[] {
class FieldFormatWithCache extends FieldFormat {
protected formatCache: Map<SerializedFieldFormat, FieldFormat> = new Map();

protected getCachedFormat(fieldParams: SerializedFieldFormat<{}, SerializableRecord>) {
const isCached = this.formatCache.has(fieldParams);
const cachedFormat = this.formatCache.get(fieldParams) || getFieldFormat(fieldParams);
if (!isCached) {
this.formatCache.set(fieldParams, cachedFormat);
}
return cachedFormat;
}
}

return [
class AggsRangeFieldFormat extends FieldFormat {
class AggsRangeFieldFormat extends FieldFormatWithCache {
static id = 'range';
static hidden = true;

Expand All @@ -51,10 +65,7 @@ export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInsta
return range.label;
}
const nestedFormatter = params as SerializedFieldFormat;
const format = getFieldFormat({
id: nestedFormatter.id,
params: nestedFormatter.params,
});
const format = this.getCachedFormat(nestedFormatter);

const gte = '\u2265';
const lt = '\u003c';
Expand Down Expand Up @@ -88,7 +99,7 @@ export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInsta
});
};
},
class AggsDateRangeFieldFormat extends FieldFormat {
class AggsDateRangeFieldFormat extends FieldFormatWithCache {
static id = 'date_range';
static hidden = true;

Expand All @@ -98,14 +109,11 @@ export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInsta
}

const nestedFormatter = this._params as SerializedFieldFormat;
const format = getFieldFormat({
id: nestedFormatter.id,
params: nestedFormatter.params,
});
const format = this.getCachedFormat(nestedFormatter);
return convertDateRangeToString(range, format.convert.bind(format));
};
},
class AggsIpRangeFieldFormat extends FieldFormat {
class AggsIpRangeFieldFormat extends FieldFormatWithCache {
static id = 'ip_range';
static hidden = true;

Expand All @@ -115,20 +123,19 @@ export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInsta
}

const nestedFormatter = this._params as SerializedFieldFormat;
const format = getFieldFormat({
id: nestedFormatter.id,
params: nestedFormatter.params,
});
const format = this.getCachedFormat(nestedFormatter);
return convertIPRangeToString(range, format.convert.bind(format));
};
},
class AggsTermsFieldFormat extends FieldFormat {
class AggsTermsFieldFormat extends FieldFormatWithCache {
static id = 'terms';
static hidden = true;

convert = (val: string, type: FieldFormatsContentType) => {
const params = this._params;
const format = getFieldFormat({ id: `${params.id}`, params });
const format = this.getCachedFormat(
params as SerializedFieldFormat<{}, SerializableRecord>
);

if (val === '__other__') {
return `${params.otherBucketLabel}`;
Expand All @@ -141,21 +148,14 @@ export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInsta
};
getConverterFor = (type: FieldFormatsContentType) => (val: string) => this.convert(val, type);
},
class AggsMultiTermsFieldFormat extends FieldFormat {
class AggsMultiTermsFieldFormat extends FieldFormatWithCache {
static id = 'multi_terms';
static hidden = true;

private formatCache: Map<SerializedFieldFormat, FieldFormat> = new Map();

convert = (val: unknown, type: FieldFormatsContentType) => {
const params = this._params;
const formats = (params.paramsPerField as SerializedFieldFormat[]).map((fieldParams) => {
const isCached = this.formatCache.has(fieldParams);
const cachedFormat = this.formatCache.get(fieldParams) || getFieldFormat(fieldParams);
if (!isCached) {
this.formatCache.set(fieldParams, cachedFormat);
}
return cachedFormat;
return this.getCachedFormat(fieldParams);
});

if (String(val) === '__other__') {
Expand Down

0 comments on commit 340c6fb

Please sign in to comment.