Add job score calculation

This commit is contained in:
2024-05-03 09:43:47 -06:00
parent 860ea64d8d
commit c7befa4d1c
2 changed files with 29 additions and 3 deletions

View File

@@ -38,6 +38,7 @@ export class JobsController {
todoCount: await this.jobsService.getTodoItemCount(jobName), todoCount: await this.jobsService.getTodoItemCount(jobName),
claimedCount: await this.jobsService.getClaimedItemCount(jobName), claimedCount: await this.jobsService.getClaimedItemCount(jobName),
doneCount: await this.jobsService.getDoneItemCount(jobName), doneCount: await this.jobsService.getDoneItemCount(jobName),
doneScore: await this.jobsService.getDoneItemScoresTotal(jobName),
}; };
} }
@@ -50,6 +51,7 @@ export class JobsController {
todoCount: await this.jobsService.getTodoItemCount(jobName), todoCount: await this.jobsService.getTodoItemCount(jobName),
claimedCount: await this.jobsService.getClaimedItemCount(jobName), claimedCount: await this.jobsService.getClaimedItemCount(jobName),
doneCount: await this.jobsService.getDoneItemCount(jobName), doneCount: await this.jobsService.getDoneItemCount(jobName),
doneScore: await this.jobsService.getDoneItemScoresTotal(jobName),
}; };
} }

View File

@@ -19,7 +19,11 @@ export const privateMetadataKeys = ['claimSecret'];
export type PublicJobMetadata = Omit<JobMetadata, typeof privateMetadataKeys[number]>; export type PublicJobMetadata = Omit<JobMetadata, typeof privateMetadataKeys[number]>;
type Leaderboard = { completeCounts: { [claimer: string]: number }, claimCounts: { [claimer: string]: number } } type Leaderboard = {
completeCounts: { [claimer: string]: number },
claimCounts: { [claimer: string]: number }
doneScores: { [claimer: string]: number }
}
@Injectable() @Injectable()
export class JobsService { export class JobsService {
@@ -104,8 +108,9 @@ export class JobsService {
} }
const completeCounts = await this.getCompleteCounts(jobName); const completeCounts = await this.getCompleteCounts(jobName);
const claimCounts = await this.getClaimCounts(jobName); const claimCounts = await this.getClaimCounts(jobName);
this.cacheManager.set(`leaderboard:${jobName}`, { completeCounts, claimCounts }, 200); const doneScores = await this.getDoneItemScores(jobName);
return { completeCounts, claimCounts }; this.cacheManager.set(`leaderboard:${jobName}`, { completeCounts, claimCounts, doneScores }, 200);
return { completeCounts, claimCounts, doneScores };
} }
@@ -160,6 +165,25 @@ export class JobsService {
return this.redis.llen(this.doneListNameBuilder(jobName)); return this.redis.llen(this.doneListNameBuilder(jobName));
} }
async getDoneItemScoresTotal(jobName: string) {
const doneItems = await this.getDoneItems(jobName);
const scores = doneItems.map(item => JSON.parse(item).data?.score || 1);
return scores.reduce((acc: number, val: number) => acc + val, 0);
}
// Sum the "score" property of each item in the done list and
// accumulate based on claimer
async getDoneItemScores(jobName: string) {
const doneItems = await this.getDoneItems(jobName);
const scores = doneItems.map(item => JSON.parse(item).data?.score || 1);
const claimers = doneItems.map(item => JSON.parse(item).client);
const scoreMap = claimers.reduce((acc: any, val: any, idx: number) => {
acc[val] = (acc[val] || 0) + scores[idx];
return acc;
}, {});
return scoreMap;
}
async getJobs() { async getJobs() {
return this.redis.keys('job:*'); return this.redis.keys('job:*');
} }