Skip to content

Commit

Permalink
Merge pull request #1106 from Giveth/feat/addFillSnapshotAgain
Browse files Browse the repository at this point in the history
Added fill snapshot round number
  • Loading branch information
aminlatifi authored Aug 28, 2023
2 parents d3dba84 + ca6f523 commit 8568a89
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/repositories/powerSnapshotRepository.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/services/cronJobs/updatePowerRoundJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand All @@ -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', {
Expand All @@ -46,6 +49,9 @@ export const runUpdatePowerRoundCronJob = () => {
await setPowerRound(powerRound);
oldBottomRank = await getBottomRank();
}

await fillSnapshotsRoundNumberPromise;

await Promise.all([
refreshProjectPowerView(),
refreshProjectFuturePowerView(),
Expand Down
26 changes: 26 additions & 0 deletions src/services/powerSnapshotServices.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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);
}
}
};

0 comments on commit 8568a89

Please sign in to comment.