From ca6f523bc5d23d9508981dae4f78b482d707dcd1 Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Mon, 28 Aug 2023 11:09:11 +0330 Subject: [PATCH] Added fill snapshot round number --- src/repositories/powerSnapshotRepository.ts | 8 ++++++ src/services/cronJobs/updatePowerRoundJob.ts | 6 +++++ src/services/powerSnapshotServices.ts | 26 ++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/services/powerSnapshotServices.ts diff --git a/src/repositories/powerSnapshotRepository.ts b/src/repositories/powerSnapshotRepository.ts index fa38e17ab..d673bb25c 100644 --- a/src/repositories/powerSnapshotRepository.ts +++ b/src/repositories/powerSnapshotRepository.ts @@ -1,6 +1,14 @@ import { PowerSnapshot } from '../entities/powerSnapshot'; import { AppDataSource } from '../orm'; +export const findInCompletePowerSnapShots = async (): Promise< + PowerSnapshot[] +> => { + return PowerSnapshot.createQueryBuilder() + .where('"roundNumber" IS NULL') + .getMany(); +}; + export const updatePowerSnapShots = async (params: { roundNumber: number; powerSnapshot: PowerSnapshot; diff --git a/src/services/cronJobs/updatePowerRoundJob.ts b/src/services/cronJobs/updatePowerRoundJob.ts index b43922c5c..7ed77cec4 100644 --- a/src/services/cronJobs/updatePowerRoundJob.ts +++ b/src/services/cronJobs/updatePowerRoundJob.ts @@ -19,6 +19,7 @@ import { } from '../../repositories/previousRoundRankRepository'; import { getNotificationAdapter } from '../../adapters/adaptersFactory'; import { sleep } from '../../utils/utils'; +import { fillIncompletePowerSnapshots } from '../powerSnapshotServices'; const cronJobTime = (config.get('UPDATE_POWER_ROUND_CRONJOB_EXPRESSION') as string) || @@ -30,6 +31,8 @@ export const runUpdatePowerRoundCronJob = () => { cronJobTime, ); schedule(cronJobTime, async () => { + const fillSnapshotsRoundNumberPromise = fillIncompletePowerSnapshots(); + const currentRound = await getPowerRound(); const powerRound = getRoundNumberByDate(new Date()).round - 1; logger.debug('runUpdatePowerRoundCronJob', { @@ -46,6 +49,9 @@ export const runUpdatePowerRoundCronJob = () => { await setPowerRound(powerRound); oldBottomRank = await getBottomRank(); } + + await fillSnapshotsRoundNumberPromise; + await Promise.all([ refreshProjectPowerView(), refreshProjectFuturePowerView(), diff --git a/src/services/powerSnapshotServices.ts b/src/services/powerSnapshotServices.ts new file mode 100644 index 000000000..fcc19395a --- /dev/null +++ b/src/services/powerSnapshotServices.ts @@ -0,0 +1,26 @@ +import { + findInCompletePowerSnapShots, + updatePowerSnapShots, +} from '../repositories/powerSnapshotRepository'; +import { getTimestampInSeconds } from '../utils/utils'; +import { getRoundNumberByDate } from '../utils/powerBoostingUtils'; +import { logger } from '../utils/logger'; + +export const fillIncompletePowerSnapshots = async (): Promise => { + const incompletePowerSnapshots = await findInCompletePowerSnapShots(); + logger.debug( + 'fillIncompletePowerSnapshots incompletePowerSnapshots', + JSON.stringify(incompletePowerSnapshots, null, 2), + ); + for (const powerSnapshot of incompletePowerSnapshots) { + try { + const roundNumber = getRoundNumberByDate(powerSnapshot.time).round; + await updatePowerSnapShots({ + powerSnapshot, + roundNumber, + }); + } catch (e) { + logger.error('fillIncompletePowerSnapshots error', e); + } + } +};