Merge pull request #319 from HaiDang666/266_final

Update: most upvoted segments on locked videos as locked submissions
This commit is contained in:
Ajay Ramachandran
2021-07-26 23:48:58 -04:00
committed by GitHub
2 changed files with 133 additions and 31 deletions

View File

@@ -10,7 +10,8 @@ interface ReputationDBResult {
votedSum: number,
lockedSum: number,
semiOldUpvotedSubmissions: number,
oldUpvotedSubmissions: number
oldUpvotedSubmissions: number,
mostUpvotedInLockedVideoSum: number
}
export async function getReputation(userID: UserID): Promise<number> {
@@ -28,43 +29,60 @@ export async function getReputation(userID: UserID): Promise<number> {
SUM(CASE WHEN "timeSubmitted" > 1596240000000 THEN "votes" ELSE 0 END) AS "votedSum",
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",
SUM(CASE WHEN "votes" > 0
AND NOT EXISTS (
SELECT * FROM "sponsorTimes" as c
WHERE (c."votes" > "a"."votes" OR c."locked" > "a"."locked") AND
c."videoID" = "a"."videoID" AND
c."category" = "a"."category" LIMIT 1)
AND EXISTS (
SELECT * FROM "lockCategories" as l
WHERE l."videoID" = "a"."videoID" AND l."category" = "a"."category" LIMIT 1)
THEN 1 ELSE 0 END) AS "mostUpvotedInLockedVideoSum"
FROM "sponsorTimes" as "a" WHERE "userID" = ?`, [userID, weekAgo, pastDate, userID]) as Promise<ReputationDBResult>;
const result = await QueryCacher.get(fetchFromDB, reputationKey(userID));
// Grace period
if (result.totalSubmissions < 5) {
return 0;
}
const downvoteRatio = result.downvotedSubmissions / result.totalSubmissions;
if (downvoteRatio > 0.3) {
return convertRange(Math.min(downvoteRatio, 0.7), 0.3, 0.7, -0.5, -2.5);
}
const nonSelfDownvoteRatio = result.nonSelfDownvotedSubmissions / result.totalSubmissions;
if (nonSelfDownvoteRatio > 0.05) {
return convertRange(Math.min(nonSelfDownvoteRatio, 0.4), 0.05, 0.4, -0.5, -2.5);
}
if (result.votedSum < 5) {
return 0;
}
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);
return calculateReputationFromMetrics(result);
}
// convert a number from one range to another.
function convertRange(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number {
const currentRange = currentMax - currentMin;
const targetRange = targetMax - targetMin;
return ((value - currentMin) / currentRange) * targetRange + targetMin;
}
export function calculateReputationFromMetrics(metrics: ReputationDBResult): number {
// Grace period
if (metrics.totalSubmissions < 5) {
return 0;
}
const downvoteRatio = metrics.downvotedSubmissions / metrics.totalSubmissions;
if (downvoteRatio > 0.3) {
return convertRange(Math.min(downvoteRatio, 0.7), 0.3, 0.7, -0.5, -2.5);
}
const nonSelfDownvoteRatio = metrics.nonSelfDownvotedSubmissions / metrics.totalSubmissions;
if (nonSelfDownvoteRatio > 0.05) {
return convertRange(Math.min(nonSelfDownvoteRatio, 0.4), 0.05, 0.4, -0.5, -2.5);
}
if (metrics.votedSum < 5) {
return 0;
}
if (metrics.oldUpvotedSubmissions < 3) {
if (metrics.semiOldUpvotedSubmissions > 3) {
return convertRange(Math.min(metrics.votedSum, 150), 5, 150, 0, 2) +
convertRange(Math.min((metrics.lockedSum ?? 0) + (metrics.mostUpvotedInLockedVideoSum ?? 0), 50), 0, 50, 0, 5);
} else {
return 0;
}
}
return convertRange(Math.min(metrics.votedSum, 150), 5, 150, 0, 7) +
convertRange(Math.min((metrics.lockedSum ?? 0) + (metrics.mostUpvotedInLockedVideoSum ?? 0), 50), 0, 50, 0, 20);
}