Skip to content

Commit

Permalink
fix: save snapshot timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Sep 4, 2024
1 parent 2aae390 commit a7e9a63
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions apps/service-auth/src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, number>>([
Expand All @@ -37,7 +53,7 @@ export class TasksService {
},
},
{
$limit: 1000,
$limit: limit,
},
{
$group: {
Expand All @@ -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;
}, {}),
},
},
])
Expand Down

0 comments on commit a7e9a63

Please sign in to comment.