From acf93695c8fe7eaba434e254fb0bf415b13ae7aa Mon Sep 17 00:00:00 2001 From: "thong.nguyen5" Date: Sat, 13 Apr 2024 21:19:56 +0700 Subject: [PATCH] Update async await and add time measure --- .../src/modules/monitor/monitor.service.ts | 18 ++++++++++++------ app/libs/utils/src/time.utils.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 app/libs/utils/src/time.utils.ts diff --git a/app/apps/onebox/src/modules/monitor/monitor.service.ts b/app/apps/onebox/src/modules/monitor/monitor.service.ts index 45d4383..bdcd601 100644 --- a/app/apps/onebox/src/modules/monitor/monitor.service.ts +++ b/app/apps/onebox/src/modules/monitor/monitor.service.ts @@ -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'; @@ -89,12 +90,17 @@ export class MonitorService { return Builder().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().success(true).build(); } diff --git a/app/libs/utils/src/time.utils.ts b/app/libs/utils/src/time.utils.ts new file mode 100644 index 0000000..6a5c595 --- /dev/null +++ b/app/libs/utils/src/time.utils.ts @@ -0,0 +1,12 @@ +import { Logger } from '@nestjs/common'; + +export async function measureTime( + label: string, + fn: () => Promise, +): Promise { + const start = performance.now(); + const result = await fn(); + const end = performance.now(); + Logger.log(`Latency for [${label}]: ${(end - start).toFixed(3)}ms`); + return result; +}