From c7befa4d1cd3c305c9dde1f8a4528afb076a535d Mon Sep 17 00:00:00 2001 From: Chip Wasson Date: Fri, 3 May 2024 09:43:47 -0600 Subject: [PATCH] Add job score calculation --- src/jobs/jobs.controller.ts | 2 ++ src/jobs/jobs.service.ts | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/jobs/jobs.controller.ts b/src/jobs/jobs.controller.ts index eeff362..50d15c5 100644 --- a/src/jobs/jobs.controller.ts +++ b/src/jobs/jobs.controller.ts @@ -38,6 +38,7 @@ export class JobsController { todoCount: await this.jobsService.getTodoItemCount(jobName), claimedCount: await this.jobsService.getClaimedItemCount(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), claimedCount: await this.jobsService.getClaimedItemCount(jobName), doneCount: await this.jobsService.getDoneItemCount(jobName), + doneScore: await this.jobsService.getDoneItemScoresTotal(jobName), }; } diff --git a/src/jobs/jobs.service.ts b/src/jobs/jobs.service.ts index 1cdeb08..d9fe971 100644 --- a/src/jobs/jobs.service.ts +++ b/src/jobs/jobs.service.ts @@ -19,7 +19,11 @@ export const privateMetadataKeys = ['claimSecret']; export type PublicJobMetadata = Omit; -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() export class JobsService { @@ -104,8 +108,9 @@ export class JobsService { } const completeCounts = await this.getCompleteCounts(jobName); const claimCounts = await this.getClaimCounts(jobName); - this.cacheManager.set(`leaderboard:${jobName}`, { completeCounts, claimCounts }, 200); - return { completeCounts, claimCounts }; + const doneScores = await this.getDoneItemScores(jobName); + 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)); } + 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() { return this.redis.keys('job:*'); }