Optimize metadata queries (#7013)

In this PR:

1. Refactor guards to avoid duplicated queries: WorkspaceAuthGuard and
UserAuthGuard only check for existence of workspace and user in the
request without querying the database
This commit is contained in:
Charles Bochet
2024-09-13 19:11:32 +02:00
committed by Charles Bochet
parent cf8b1161cc
commit 523df5398a
132 changed files with 818 additions and 6372 deletions

View File

@ -4,7 +4,9 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { LogExecutionTime } from 'src/engine/decorators/observability/log-execution-time.decorator';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { generateObjectMetadataMap } from 'src/engine/metadata-modules/utils/generate-object-metadata-map.util';
import {
WorkspaceMetadataCacheException,
WorkspaceMetadataCacheExceptionCode,
@ -23,6 +25,7 @@ export class WorkspaceMetadataCacheService {
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
) {}
@LogExecutionTime()
async recomputeMetadataCache(
workspaceId: string,
force = false,
@ -45,7 +48,7 @@ export class WorkspaceMetadataCacheService {
}
const isAlreadyCaching =
await this.workspaceCacheStorageService.getObjectMetadataCollectionOngoingCachingLock(
await this.workspaceCacheStorageService.getObjectMetadataOngoingCachingLock(
workspaceId,
currentDatabaseVersion,
);
@ -68,25 +71,31 @@ export class WorkspaceMetadataCacheService {
currentDatabaseVersion,
);
const freshObjectMetadataCollection =
await this.objectMetadataRepository.find({
where: { workspaceId },
relations: [
'fields.object',
'fields',
'fields.fromRelationMetadata',
'fields.toRelationMetadata',
'fields.fromRelationMetadata.toObjectMetadata',
],
});
console.time('fetching object metadata');
const objectMetadataItems = await this.objectMetadataRepository.find({
where: { workspaceId },
relations: [
'fields',
'fields.fromRelationMetadata',
'fields.toRelationMetadata',
],
});
await this.workspaceCacheStorageService.setObjectMetadataCollection(
console.timeEnd('fetching object metadata');
console.time('generating object metadata map');
const freshObjectMetadataMap =
generateObjectMetadataMap(objectMetadataItems);
console.timeEnd('generating object metadata map');
await this.workspaceCacheStorageService.setObjectMetadataMap(
workspaceId,
currentDatabaseVersion,
freshObjectMetadataCollection,
freshObjectMetadataMap,
);
await this.workspaceCacheStorageService.removeObjectMetadataCollectionOngoingCachingLock(
await this.workspaceCacheStorageService.removeObjectMetadataOngoingCachingLock(
workspaceId,
currentDatabaseVersion,
);