mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 00:01:48 +08:00
FieldDisplay: add cache to reuse field value calculations (#35072)
* add timeline value cache * add timeline value cache * with console logs * cleanup Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
This commit is contained in:
@ -2,6 +2,8 @@ import {
|
|||||||
ApplyFieldOverrideOptions,
|
ApplyFieldOverrideOptions,
|
||||||
DataFrame,
|
DataFrame,
|
||||||
DataLink,
|
DataLink,
|
||||||
|
DisplayProcessor,
|
||||||
|
DisplayValue,
|
||||||
DynamicConfigValue,
|
DynamicConfigValue,
|
||||||
Field,
|
Field,
|
||||||
FieldColorModeId,
|
FieldColorModeId,
|
||||||
@ -187,6 +189,11 @@ export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFra
|
|||||||
timeZone: options.timeZone,
|
timeZone: options.timeZone,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Wrap the display with a cache to avoid double calls
|
||||||
|
if (newField.config.unit !== 'dateTimeFromNow') {
|
||||||
|
newField.display = cachingDisplayProcessor(newField.display, 2500);
|
||||||
|
}
|
||||||
|
|
||||||
// Attach data links supplier
|
// Attach data links supplier
|
||||||
newField.getLinks = getLinksSupplier(
|
newField.getLinks = getLinksSupplier(
|
||||||
newFrame,
|
newFrame,
|
||||||
@ -204,6 +211,24 @@ export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFra
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cachingDisplayProcessor(disp: DisplayProcessor, maxCacheSize = 2500): DisplayProcessor {
|
||||||
|
const cache = new Map<any, DisplayValue>();
|
||||||
|
|
||||||
|
return (value: any) => {
|
||||||
|
let v = cache.get(value);
|
||||||
|
if (!v) {
|
||||||
|
// Don't grow too big
|
||||||
|
if (cache.size === maxCacheSize) {
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
v = disp(value);
|
||||||
|
cache.set(value, v);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export interface FieldOverrideEnv extends FieldOverrideContext {
|
export interface FieldOverrideEnv extends FieldOverrideContext {
|
||||||
fieldConfigRegistry: FieldConfigOptionsRegistry;
|
fieldConfigRegistry: FieldConfigOptionsRegistry;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user