From 7f69867e8a611d735ce6b39e77393d1c75320d46 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 3 Feb 2025 00:13:34 +0400 Subject: [PATCH] optimizing space call --- .../modules/device/entities/device.entity.ts | 1 - .../services/space-validation.service.ts | 37 +++++++++++++++++-- .../subspace/subspace-device.service.ts | 25 +++++++++---- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/libs/common/src/modules/device/entities/device.entity.ts b/libs/common/src/modules/device/entities/device.entity.ts index f2a5a1e..bdd1ce5 100644 --- a/libs/common/src/modules/device/entities/device.entity.ts +++ b/libs/common/src/modules/device/entities/device.entity.ts @@ -78,7 +78,6 @@ export class DeviceEntity extends AbstractEntity { @OneToOne(() => TagEntity, (tag) => tag.device, { nullable: true, }) - @JoinColumn({ name: 'tag_id' }) tag: TagEntity; constructor(partial: Partial) { diff --git a/src/space/services/space-validation.service.ts b/src/space/services/space-validation.service.ts index 4a3e32a..304586b 100644 --- a/src/space/services/space-validation.service.ts +++ b/src/space/services/space-validation.service.ts @@ -7,6 +7,8 @@ import { SpaceModelEntity, SpaceModelRepository, } from '@app/common/modules/space-model'; +import { ProjectRepository } from '@app/common/modules/project/repositiories'; +import { CommunityRepository } from '@app/common/modules/community/repositories'; @Injectable() export class ValidationService { @@ -14,6 +16,8 @@ export class ValidationService { private readonly projectService: ProjectService, private readonly communityService: CommunityService, private readonly spaceRepository: SpaceRepository, + private readonly projectRepository: ProjectRepository, + private readonly communityRepository: CommunityRepository, private readonly spaceModelRepository: SpaceModelRepository, ) {} @@ -30,6 +34,31 @@ export class ValidationService { return { community: community.data, project: project }; } + async checkCommunityAndProjectSpaceExistence( + communityUuid: string, + projectUuid: string, + spaceUuid?: string, + ): Promise { + const [projectExists, communityExists, spaceExists] = await Promise.all([ + this.projectRepository.exists({ where: { uuid: projectUuid } }), + this.communityRepository.exists({ + where: { uuid: communityUuid, project: { uuid: projectUuid } }, + }), + spaceUuid + ? this.spaceRepository.exists({ + where: { uuid: spaceUuid }, + }) + : Promise.resolve(true), + ]); + + if (!projectExists) + throw new HttpException(`Project not found`, HttpStatus.NOT_FOUND); + if (!communityExists) + throw new HttpException(`Community not found`, HttpStatus.NOT_FOUND); + if (spaceUuid && !spaceExists) + throw new HttpException(`Space not found`, HttpStatus.NOT_FOUND); + } + async validateSpaceWithinCommunityAndProject( communityUuid: string, projectUuid: string, @@ -50,10 +79,10 @@ export class ValidationService { 'tags', 'subspaces.tags', 'subspaces.devices', - 'devices', - 'devices.productDevice', - 'devices.tag', - 'devices.subspace', + //'devices', + //'devices.productDevice', + //'devices.tag', + // 'devices.subspace', ], }); diff --git a/src/space/services/subspace/subspace-device.service.ts b/src/space/services/subspace/subspace-device.service.ts index e993716..9ee9b9b 100644 --- a/src/space/services/subspace/subspace-device.service.ts +++ b/src/space/services/subspace/subspace-device.service.ts @@ -29,14 +29,22 @@ export class SubspaceDeviceService { ): Promise { const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params; - await this.validationService.validateSpaceWithinCommunityAndProject( + console.time('Total Execution Time'); // ⏳ Start total execution time + console.time('Validation Time'); + + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, ); - const subspace = await this.findSubspaceWithDevices(subSpaceUuid); + console.timeEnd('Validation Time'); // ⏳ Log validation time + console.time('Subspace Query Time'); + const subspace = await this.findSubspaceWithDevices(subSpaceUuid); + console.timeEnd('Subspace Query Time'); // ⏳ Log subspace fetching time + + console.time('Fetching Tuya Details Time'); const safeFetch = async (device: any) => { try { const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya( @@ -55,12 +63,16 @@ export class SubspaceDeviceService { }; } catch (error) { console.warn( - `Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`, + `⚠️ Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`, ); return null; } }; + const detailedDevices = await Promise.all(subspace.devices.map(safeFetch)); + console.timeEnd('Fetching Tuya Details Time'); // ⏳ Log total Tuya fetching time + + console.timeEnd('Total Execution Time'); // ⏳ End total execution time return new SuccessResponseDto({ data: detailedDevices.filter(Boolean), // Remove nulls @@ -74,7 +86,7 @@ export class SubspaceDeviceService { const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } = params; try { - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, @@ -127,12 +139,11 @@ export class SubspaceDeviceService { const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } = params; try { - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, ); - const subspace = await this.findSubspace(subSpaceUuid); const device = await this.findDevice(deviceUuid); @@ -207,7 +218,7 @@ export class SubspaceDeviceService { private async findDevice(deviceUuid: string) { const device = await this.deviceRepository.findOne({ where: { uuid: deviceUuid }, - relations: ['subspace', 'tag', 'tag.space', 'tag.subspace'], + relations: ['subspace', , 'tag', 'tag.space', 'tag.subspace'], }); if (!device) { this.throwNotFound('Device', deviceUuid);