Skip to content

Commit

Permalink
purge tokens using raw query
Browse files Browse the repository at this point in the history
  • Loading branch information
franzns committed Jan 15, 2024
1 parent 9fa413d commit 6422efc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
12 changes: 6 additions & 6 deletions modules/network/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,11 @@ export const mainnetNetworkConfig: NetworkConfig = {
name: 'sync-global-coingecko-prices',
interval: (env.DEPLOYMENT_ENV as DeploymentEnv) === 'canary' ? every(10, 'minutes') : every(2, 'minutes'),
},
// {
// name: 'global-purge-old-tokenprices',
// interval: every(1, 'days'),
// alarmEvaluationPeriod: 1,
// alarmDatapointsToAlarm: 1,
// },
{
name: 'global-purge-old-tokenprices',
interval: every(1, 'days'),
alarmEvaluationPeriod: 1,
alarmDatapointsToAlarm: 1,
},
],
};
25 changes: 4 additions & 21 deletions modules/token/lib/token-price.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,28 +256,11 @@ export class TokenPriceService {
}

public async purgeOldTokenPricesForAllChains(): Promise<number> {
const purgeBeforeTimestamp = moment().startOf('day').subtract(100, 'days').utc().unix();
const oldPrices = await prisma.prismaTokenPrice.findMany({
where: {
timestamp: { lt: purgeBeforeTimestamp },
},
});

// returns all non midnight prices
const tobeDeleted = _.uniq(oldPrices.filter((tokenPrice) => tokenPrice.timestamp % secondsPerDay !== 0));

//apparently prisma has a limitation on delete
const chunks = _.chunk(tobeDeleted, 1000);

for (const chunk of chunks) {
await prisma.prismaTokenPrice.deleteMany({
where: {
timestamp: { in: chunk.map((tokenPrice) => tokenPrice.timestamp) },
},
});
}
// DATE(to_timestamp(timestamp)) will return the midnight timestamp. We'll delete all prices that are not midnight timestamps AND are older than 100 days.
const deleted =
await prisma.$executeRaw`DELETE FROM "PrismaTokenPrice" WHERE DATE(to_timestamp(timestamp)) != to_timestamp(timestamp) AND to_timestamp(timestamp) < CURRENT_DATE - INTERVAL '100 days'`;

return tobeDeleted.length;
return deleted;
}

private async updateCandleStickData() {
Expand Down

0 comments on commit 6422efc

Please sign in to comment.