From d00573736687153e8ed6d834e9a47ba7365234e3 Mon Sep 17 00:00:00 2001 From: Ramin Date: Fri, 14 Jun 2024 18:46:20 +0330 Subject: [PATCH] optimize updateProjectStatistics --- src/repositories/donationRepository.ts | 28 +++++++++++++++++++++----- src/services/projectService.ts | 11 ++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/repositories/donationRepository.ts b/src/repositories/donationRepository.ts index 41e57c01e..efcda616c 100644 --- a/src/repositories/donationRepository.ts +++ b/src/repositories/donationRepository.ts @@ -476,6 +476,29 @@ export async function countUniqueDonorsForRound(params: { .uniqueDonorsCount; } +export async function qfRoundStats(params: { + projectId: number; + qfRoundId: number; +}): Promise<{ uniqueDonorsCount: number; sumValueUsd: number }> { + const { projectId, qfRoundId } = params; + const result = await ProjectEstimatedMatchingView.createQueryBuilder( + 'projectEstimatedMatchingView', + ) + .select('projectEstimatedMatchingView.sumValueUsd') + .where('projectEstimatedMatchingView.projectId = :projectId', { + projectId, + }) + .andWhere('projectEstimatedMatchingView.qfRoundId = :qfRoundId', { + qfRoundId, + }) + .getOne(); + const { uniqueDonorsCount = 0, sumValueUsd = 0 } = result || {}; + return { + uniqueDonorsCount, + sumValueUsd, + }; +} + export async function sumDonationValueUsdForQfRound(params: { projectId: number; qfRoundId: number; @@ -491,12 +514,7 @@ export async function sumDonationValueUsdForQfRound(params: { .andWhere('projectEstimatedMatchingView.qfRoundId = :qfRoundId', { qfRoundId, }) - .cache( - `sumDonationValueUsdForQfRound-${projectId}-${qfRoundId}`, - Number(process.env.PROJECT_QFROUND_DONATION_SUMMARY_CACHE_TIME || 60000), - ) .getOne(); - return result?.sumValueUsd || 0; } diff --git a/src/services/projectService.ts b/src/services/projectService.ts index 8c3dcc9b1..b0f0e3e77 100644 --- a/src/services/projectService.ts +++ b/src/services/projectService.ts @@ -1,8 +1,7 @@ import { Project } from '../entities/project'; import { countUniqueDonorsAndSumDonationValueUsd, - countUniqueDonorsForRound, - sumDonationValueUsdForQfRound, + qfRoundStats, } from '../repositories/donationRepository'; import { findActiveQfRound } from '../repositories/qfRoundRepository'; import { logger } from '../utils/logger'; @@ -35,14 +34,12 @@ export const updateProjectStatistics = async (projectId: number) => { let sumDonationValueUsdForActiveQfRound = 0, countUniqueDonorsForActiveQfRound = 0; if (activeQfRound) { - sumDonationValueUsdForActiveQfRound = await sumDonationValueUsdForQfRound({ - projectId, - qfRoundId: activeQfRound.id, - }); - countUniqueDonorsForActiveQfRound = await countUniqueDonorsForRound({ + const qfRoundResult = await qfRoundStats({ projectId, qfRoundId: activeQfRound.id, }); + sumDonationValueUsdForActiveQfRound = qfRoundResult.sumValueUsd; + countUniqueDonorsForActiveQfRound = qfRoundResult.uniqueDonorsCount; } const { totalDonations, uniqueDonors } =