Skip to content

Commit 660e108

Browse files
authored
[Embeddable] embeddable schema cache (#271959)
Dashboard transforms call `getTransforms` for each panel in the dashboard. The current implementation of this function builds the embeddable schema on each call. This can put a lot of CPU and memory pressure on the Kibana server for large dashboards. Instead, embeddable schemas should only be created once.
1 parent 7769304 commit 660e108

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

  • src/platform/plugins/shared/embeddable/server/embeddable_transforms

src/platform/plugins/shared/embeddable/server/embeddable_transforms/registry.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import type { ObjectType } from '@kbn/config-schema';
10+
import type { ObjectType, Type } from '@kbn/config-schema';
1111
import type { getDrilldownRegistry } from '../drilldowns/registry';
1212
import type { EmbeddableServerDefinition } from './types';
1313

1414
export function getEmbeddableServerRegistry(
1515
drilldownRegistry: ReturnType<typeof getDrilldownRegistry>
1616
) {
1717
const registry: { [key: string]: EmbeddableServerDefinition<any, any> } = {};
18+
const schemaCache: Record<string, Type<object> | undefined> = {};
19+
20+
function getCachedSchema(type: string) {
21+
if (schemaCache[type]) {
22+
return schemaCache[type];
23+
}
24+
25+
const { getSchema } = registry[type] ?? {};
26+
const schema = getSchema?.(drilldownRegistry.getSchema);
27+
schemaCache[type] = schema;
28+
return schema;
29+
}
1830

1931
return {
2032
registerEmbeddableServerDefinition: (
@@ -30,7 +42,7 @@ export function getEmbeddableServerRegistry(
3042
getAllEmbeddableSchemas: () => {
3143
const schemas: { [key: string]: { schema: ObjectType; title: string } } = {};
3244
Object.entries(registry).forEach(([type, definition]) => {
33-
const schema = definition?.getSchema?.(drilldownRegistry.getSchema);
45+
const schema = getCachedSchema(type);
3446
if (schema) {
3547
schemas[type] = {
3648
schema: schema as ObjectType,
@@ -41,10 +53,11 @@ export function getEmbeddableServerRegistry(
4153
return schemas;
4254
},
4355
getEmbeddableTransforms: (type: string) => {
44-
const { getTransforms, getSchema, throwOnUnmappedPanel } = registry[type] ?? {};
56+
const { getTransforms, throwOnUnmappedPanel } = registry[type] ?? {};
57+
const schema = getCachedSchema(type);
4558
return {
4659
...getTransforms?.(drilldownRegistry.transforms),
47-
...(getSchema ? { schema: getSchema(drilldownRegistry.getSchema) } : {}),
60+
...(schema ? { schema } : {}),
4861
...(typeof throwOnUnmappedPanel === 'boolean' ? { throwOnUnmappedPanel } : {}),
4962
};
5063
},

0 commit comments

Comments
 (0)