Make reputation take into account more recent segments

This commit is contained in:
Ajay Ramachandran
2021-07-12 11:05:12 -04:00
parent ef86fceedd
commit a23ec160c0
3 changed files with 19 additions and 8 deletions

View File

@@ -14,5 +14,5 @@ export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: S
} }
export function reputationKey(userID: UserID): string { export function reputationKey(userID: UserID): string {
return "reputation.user." + userID; return "reputation.user.v2." + userID;
} }

View File

@@ -7,12 +7,14 @@ interface ReputationDBResult {
totalSubmissions: number, totalSubmissions: number,
downvotedSubmissions: number, downvotedSubmissions: number,
nonSelfDownvotedSubmissions: number, nonSelfDownvotedSubmissions: number,
upvotedSum: number, votedSum: number,
lockedSum: number, lockedSum: number,
semiOldUpvotedSubmissions: number,
oldUpvotedSubmissions: number oldUpvotedSubmissions: number
} }
export async function getReputation(userID: UserID): Promise<number> { export async function getReputation(userID: UserID): Promise<number> {
const weekAgo = Date.now() - 1000 * 60 * 60 * 24 * 45; // 45 days ago
const pastDate = Date.now() - 1000 * 60 * 60 * 24 * 45; // 45 days ago const pastDate = Date.now() - 1000 * 60 * 60 * 24 * 45; // 45 days ago
// 1596240000000 is August 1st 2020, a little after auto upvote was disabled // 1596240000000 is August 1st 2020, a little after auto upvote was disabled
const fetchFromDB = () => db.prepare("get", const fetchFromDB = () => db.prepare("get",
@@ -23,10 +25,11 @@ export async function getReputation(userID: UserID): Promise<number> {
WHERE b."userID" = ? WHERE b."userID" = ?
AND b."votes" > 0 AND b."category" = "a"."category" AND b."videoID" = "a"."videoID" LIMIT 1) AND b."votes" > 0 AND b."category" = "a"."category" AND b."videoID" = "a"."videoID" LIMIT 1)
THEN 1 ELSE 0 END) AS "nonSelfDownvotedSubmissions", THEN 1 ELSE 0 END) AS "nonSelfDownvotedSubmissions",
SUM(CASE WHEN "votes" > 0 AND "timeSubmitted" > 1596240000000 THEN "votes" ELSE 0 END) AS "upvotedSum", SUM(CASE WHEN "timeSubmitted" > 1596240000000 THEN "votes" ELSE 0 END) AS "votedSum",
SUM(locked) AS "lockedSum", SUM(locked) AS "lockedSum",
SUM(CASE WHEN "timeSubmitted" < ? AND "timeSubmitted" > 1596240000000 AND "votes" > 0 THEN 1 ELSE 0 END) AS "semiOldUpvotedSubmissions",
SUM(CASE WHEN "timeSubmitted" < ? AND "timeSubmitted" > 1596240000000 AND "votes" > 0 THEN 1 ELSE 0 END) AS "oldUpvotedSubmissions" SUM(CASE WHEN "timeSubmitted" < ? AND "timeSubmitted" > 1596240000000 AND "votes" > 0 THEN 1 ELSE 0 END) AS "oldUpvotedSubmissions"
FROM "sponsorTimes" as "a" WHERE "userID" = ?`, [userID, pastDate, userID]) as Promise<ReputationDBResult>; FROM "sponsorTimes" as "a" WHERE "userID" = ?`, [userID, weekAgo, pastDate, userID]) as Promise<ReputationDBResult>;
const result = await QueryCacher.get(fetchFromDB, reputationKey(userID)); const result = await QueryCacher.get(fetchFromDB, reputationKey(userID));
@@ -45,11 +48,19 @@ export async function getReputation(userID: UserID): Promise<number> {
return convertRange(Math.min(nonSelfDownvoteRatio, 0.4), 0.05, 0.4, -0.5, -2.5); return convertRange(Math.min(nonSelfDownvoteRatio, 0.4), 0.05, 0.4, -0.5, -2.5);
} }
if (result.oldUpvotedSubmissions < 3 || result.upvotedSum < 5) { if (result.votedSum < 5) {
return 0; return 0;
} }
return convertRange(Math.min(result.upvotedSum, 150), 5, 150, 0, 7) + convertRange(Math.min(result.lockedSum ?? 0, 50), 0, 50, 0, 20); if (result.oldUpvotedSubmissions < 3) {
if (result.semiOldUpvotedSubmissions > 3) {
return convertRange(Math.min(result.votedSum, 150), 5, 150, 0, 2) + convertRange(Math.min(result.lockedSum ?? 0, 50), 0, 50, 0, 5);
} else {
return 0;
}
}
return convertRange(Math.min(result.votedSum, 150), 5, 150, 0, 7) + convertRange(Math.min(result.lockedSum ?? 0, 50), 0, 50, 0, 20);
} }
function convertRange(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number { function convertRange(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number {

View File

@@ -114,11 +114,11 @@ describe('reputation', () => {
}); });
it("user with high reputation", async () => { it("user with high reputation", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighRep)), 0.24137931034482757); assert.strictEqual(await getReputation(getHash(userIDHighRep)), 0.19310344827586207);
}); });
it("user with high reputation and locked segments", async () => { it("user with high reputation and locked segments", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.8413793103448277); assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.793103448275862);
}); });
}); });