Skip to content

Commit

Permalink
cache all nested formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Sep 12, 2022
1 parent 591a9b1 commit 0ac8278
Showing 1 changed file with 26 additions and 26 deletions.
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 0ac8278

Please sign in to comment.