Skip to content

Commit

Permalink
Update async await and add time measure
Browse files Browse the repository at this point in the history
  • Loading branch information
thongnguyen5 authored and thongnguyendev committed Apr 13, 2024
1 parent bd3ec89 commit acf9369
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/apps/onebox/src/modules/monitor/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { ProjectMemberRepository } from '@app/shared_modules/project/repositories/project.member.repository';
import { ProjectRepository } from '@app/shared_modules/project/repositories/project.repository';
import { WebhookService } from '@app/shared_modules/webhook/webhook.service';
import { measureTime } from '@app/utils/time.utils';
import { Injectable } from '@nestjs/common';
import { Builder } from 'builder-pattern';
import { ProjectService } from '../project/project.service';
Expand Down Expand Up @@ -89,12 +90,17 @@ export class MonitorService {
return Builder<DeleteMonitorResponseDto>().success(true).build();
}
await this.projectService.checkProjectPermission(user, monitor.projectId);
await this.webhookService.deleteWebhook(monitor.webhookId);
await this.monitorRepository.deleteMonitor(monitor.monitorId);
await this.projectRepository.increaseMonitorCount(monitor.projectId, -1);
await MonitorAddressRepository.getRepository(
monitor.network,
).deleteAllMonitorAddress(monitor.monitorId);
await measureTime('delete_monitor_resource', async () => {
Promise.all([
this.webhookService.deleteWebhook(monitor.webhookId),
this.monitorRepository.deleteMonitor(monitor.monitorId),
this.projectRepository.increaseMonitorCount(monitor.projectId, -1),
MonitorAddressRepository.getRepository(
monitor.network,
).deleteAllMonitorAddress(monitor.monitorId),
]);
});

return Builder<DeleteMonitorResponseDto>().success(true).build();
}

Expand Down
12 changes: 12 additions & 0 deletions app/libs/utils/src/time.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Logger } from '@nestjs/common';

export async function measureTime<T>(
label: string,
fn: () => Promise<T>,
): Promise<T> {
const start = performance.now();
const result = await fn();
const end = performance.now();
Logger.log(`Latency for [${label}]: ${(end - start).toFixed(3)}ms`);
return result;
}

0 comments on commit acf9369

Please sign in to comment.