mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
optimizing space call
This commit is contained in:
@ -78,7 +78,6 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
@OneToOne(() => TagEntity, (tag) => tag.device, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'tag_id' })
|
||||
tag: TagEntity;
|
||||
|
||||
constructor(partial: Partial<DeviceEntity>) {
|
||||
|
@ -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<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(
|
||||
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',
|
||||
],
|
||||
});
|
||||
|
||||
|
@ -29,14 +29,22 @@ export class SubspaceDeviceService {
|
||||
): Promise<BaseResponseDto> {
|
||||
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);
|
||||
|
Reference in New Issue
Block a user