diff --git a/apps/service-auth/src/tasks/tasks.service.ts b/apps/service-auth/src/tasks/tasks.service.ts index 96cb365..e6f074a 100644 --- a/apps/service-auth/src/tasks/tasks.service.ts +++ b/apps/service-auth/src/tasks/tasks.service.ts @@ -15,14 +15,30 @@ export class TasksService { ) {} public async backfillLegendTrophyThreshold() { - const thresholds = await this.getTrophyThresholds(); - await this.redis.set('RAW:LEGEND-TROPHY-THRESHOLD', JSON.stringify(thresholds), { - EX: 60 * 60 * 24, - }); - return thresholds; // legend-trophy-threshold + const thresholds = await this.getTrophyThresholds(50000); + const timestamp = new Date().toISOString(); + const keyPrefix = 'RAW:LEGEND-TROPHY-THRESHOLD'; + const key = `${keyPrefix}:${timestamp.slice(0, 10)}`; + const payload = JSON.stringify({ timestamp: new Date().toISOString(), thresholds }); + + await this.redis + .multi() + .set(keyPrefix, payload, { + EX: 60 * 60 * 24 + 60 * 5, // 1 day + 5 minutes + }) + .set(key, payload, { + EX: 30 * 60 * 60 * 24 + 60 * 5, // 30 days + 5 minutes + }) + .exec(); + + return thresholds; } - public async getTrophyThresholds() { + public async getTrophyThresholds(limit = 10000) { + const ranks = [ + 1, 3, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, + ].filter((rank) => rank <= limit); + const seasonId = this.clashClient.util.getSeasonId(); const [result] = await this.legendAttacksCollection .aggregate>([ @@ -37,7 +53,7 @@ export class TasksService { }, }, { - $limit: 1000, + $limit: limit, }, { $group: { @@ -50,33 +66,12 @@ export class TasksService { { $project: { _id: 0, - '1': { - $arrayElemAt: ['$trophies', 0], - }, - '3': { - $arrayElemAt: ['$trophies', 2], - }, - '10': { - $arrayElemAt: ['$trophies', 9], - }, - '20': { - $arrayElemAt: ['$trophies', 19], - }, - '50': { - $arrayElemAt: ['$trophies', 49], - }, - '100': { - $arrayElemAt: ['$trophies', 99], - }, - '200': { - $arrayElemAt: ['$trophies', 199], - }, - '500': { - $arrayElemAt: ['$trophies', 499], - }, - '1000': { - $arrayElemAt: ['$trophies', 999], - }, + ...ranks.reduce((acc, rank) => { + acc[rank.toString()] = { + $arrayElemAt: ['$trophies', rank - 1], + }; + return acc; + }, {}), }, }, ])