optimizing space call

This commit is contained in:
hannathkadher
2025-02-03 00:13:34 +04:00
parent 87c2722313
commit 7f69867e8a
3 changed files with 51 additions and 12 deletions

View File

@ -78,7 +78,6 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
@OneToOne(() => TagEntity, (tag) => tag.device, { @OneToOne(() => TagEntity, (tag) => tag.device, {
nullable: true, nullable: true,
}) })
@JoinColumn({ name: 'tag_id' })
tag: TagEntity; tag: TagEntity;
constructor(partial: Partial<DeviceEntity>) { constructor(partial: Partial<DeviceEntity>) {

View File

@ -7,6 +7,8 @@ import {
SpaceModelEntity, SpaceModelEntity,
SpaceModelRepository, SpaceModelRepository,
} from '@app/common/modules/space-model'; } from '@app/common/modules/space-model';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
@Injectable() @Injectable()
export class ValidationService { export class ValidationService {
@ -14,6 +16,8 @@ export class ValidationService {
private readonly projectService: ProjectService, private readonly projectService: ProjectService,
private readonly communityService: CommunityService, private readonly communityService: CommunityService,
private readonly spaceRepository: SpaceRepository, private readonly spaceRepository: SpaceRepository,
private readonly projectRepository: ProjectRepository,
private readonly communityRepository: CommunityRepository,
private readonly spaceModelRepository: SpaceModelRepository, private readonly spaceModelRepository: SpaceModelRepository,
) {} ) {}
@ -30,6 +34,31 @@ export class ValidationService {
return { community: community.data, project: project }; return { community: community.data, project: project };
} }
async checkCommunityAndProjectSpaceExistence(
communityUuid: string,
projectUuid: string,
spaceUuid?: string,
): Promise<void> {
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( async validateSpaceWithinCommunityAndProject(
communityUuid: string, communityUuid: string,
projectUuid: string, projectUuid: string,
@ -50,10 +79,10 @@ export class ValidationService {
'tags', 'tags',
'subspaces.tags', 'subspaces.tags',
'subspaces.devices', 'subspaces.devices',
'devices', //'devices',
'devices.productDevice', //'devices.productDevice',
'devices.tag', //'devices.tag',
'devices.subspace', // 'devices.subspace',
], ],
}); });

View File

@ -29,14 +29,22 @@ export class SubspaceDeviceService {
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params; 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, communityUuid,
projectUuid, projectUuid,
spaceUuid, 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) => { const safeFetch = async (device: any) => {
try { try {
const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya( const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya(
@ -55,12 +63,16 @@ export class SubspaceDeviceService {
}; };
} catch (error) { } catch (error) {
console.warn( console.warn(
`Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`, `⚠️ Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`,
); );
return null; return null;
} }
}; };
const detailedDevices = await Promise.all(subspace.devices.map(safeFetch)); 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({ return new SuccessResponseDto({
data: detailedDevices.filter(Boolean), // Remove nulls data: detailedDevices.filter(Boolean), // Remove nulls
@ -74,7 +86,7 @@ export class SubspaceDeviceService {
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } = const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
params; params;
try { try {
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,
@ -127,12 +139,11 @@ export class SubspaceDeviceService {
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } = const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid, projectUuid } =
params; params;
try { try {
await this.validationService.validateSpaceWithinCommunityAndProject( await this.validationService.checkCommunityAndProjectSpaceExistence(
communityUuid, communityUuid,
projectUuid, projectUuid,
spaceUuid, spaceUuid,
); );
const subspace = await this.findSubspace(subSpaceUuid); const subspace = await this.findSubspace(subSpaceUuid);
const device = await this.findDevice(deviceUuid); const device = await this.findDevice(deviceUuid);
@ -207,7 +218,7 @@ export class SubspaceDeviceService {
private async findDevice(deviceUuid: string) { private async findDevice(deviceUuid: string) {
const device = await this.deviceRepository.findOne({ const device = await this.deviceRepository.findOne({
where: { uuid: deviceUuid }, where: { uuid: deviceUuid },
relations: ['subspace', 'tag', 'tag.space', 'tag.subspace'], relations: ['subspace', , 'tag', 'tag.space', 'tag.subspace'],
}); });
if (!device) { if (!device) {
this.throwNotFound('Device', deviceUuid); this.throwNotFound('Device', deviceUuid);