Update query and test

This commit is contained in:
Haidang666
2021-07-24 15:09:30 +07:00
parent 2773c5f500
commit 0eb298a943
2 changed files with 14 additions and 13 deletions

View File

@@ -33,7 +33,9 @@ export async function getReputation(userID: UserID): Promise<number> {
SUM(CASE WHEN "votes" > 0 SUM(CASE WHEN "votes" > 0
AND NOT EXISTS ( AND NOT EXISTS (
SELECT * FROM "sponsorTimes" as c SELECT * FROM "sponsorTimes" as c
WHERE c."videoID" = "a"."videoID" AND c."votes" > "a"."votes" LIMIT 1) WHERE (c."votes" > "a"."votes" OR c."locked" > "a"."locked") AND
c."videoID" = "a"."videoID" AND
c."category" = "a"."category" LIMIT 1)
AND EXISTS ( AND EXISTS (
SELECT * FROM "lockCategories" as l SELECT * FROM "lockCategories" as l
WHERE l."videoID" = "a"."videoID" AND l."category" = "a"."category" LIMIT 1) WHERE l."videoID" = "a"."videoID" AND l."category" = "a"."category" LIMIT 1)
@@ -42,7 +44,7 @@ export async function getReputation(userID: UserID): Promise<number> {
const result = await QueryCacher.get(fetchFromDB, reputationKey(userID)); const result = await QueryCacher.get(fetchFromDB, reputationKey(userID));
return calculateFromMetrics(result); return calculateReputationFromMetrics(result);
} }
// convert a number from one range to another. // convert a number from one range to another.
@@ -52,7 +54,7 @@ function convertRange(value: number, currentMin: number, currentMax: number, tar
return ((value - currentMin) / currentRange) * targetRange + targetMin; return ((value - currentMin) / currentRange) * targetRange + targetMin;
} }
export function calculateFromMetrics(metrics: ReputationDBResult): number { export function calculateReputationFromMetrics(metrics: ReputationDBResult): number {
// Grace period // Grace period
if (metrics.totalSubmissions < 5) { if (metrics.totalSubmissions < 5) {
return 0; return 0;

View File

@@ -2,7 +2,7 @@ import assert from "assert";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { UserID } from "../../src/types/user.model"; import { UserID } from "../../src/types/user.model";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import { getReputation, calculateFromMetrics } from "../../src/utils/reputation"; import { getReputation, calculateReputationFromMetrics } from "../../src/utils/reputation";
const userIDLowSubmissions = "reputation-lowsubmissions" as UserID; const userIDLowSubmissions = "reputation-lowsubmissions" as UserID;
const userIDHighDownvotes = "reputation-highdownvotes" as UserID; const userIDHighDownvotes = "reputation-highdownvotes" as UserID;
@@ -130,7 +130,7 @@ describe("reputation", () => {
mostUpvotedInLockedVideoSum: 0 mostUpvotedInLockedVideoSum: 0
}; };
assert.strictEqual(await getReputation(getHash(userIDHighDownvotes)), calculateFromMetrics(metrics)); assert.strictEqual(await getReputation(getHash(userIDHighDownvotes)), calculateReputationFromMetrics(metrics));
}); });
it("user with high non self downvote ratio", async () => { it("user with high non self downvote ratio", async () => {
@@ -145,7 +145,7 @@ describe("reputation", () => {
oldUpvotedSubmissions: 1, oldUpvotedSubmissions: 1,
mostUpvotedInLockedVideoSum: 0 mostUpvotedInLockedVideoSum: 0
}; };
assert.strictEqual(await getReputation(getHash(userIDHighNonSelfDownvotes)), calculateFromMetrics(metrics)); assert.strictEqual(await getReputation(getHash(userIDHighNonSelfDownvotes)), calculateReputationFromMetrics(metrics));
}); });
it("user with mostly new submissions", async () => { it("user with mostly new submissions", async () => {
@@ -161,7 +161,6 @@ describe("reputation", () => {
}); });
it("user with high reputation", async () => { it("user with high reputation", async () => {
// 0.19310344827586207
const metrics = { const metrics = {
totalSubmissions: 8, totalSubmissions: 8,
downvotedSubmissions: 1, downvotedSubmissions: 1,
@@ -172,12 +171,11 @@ describe("reputation", () => {
oldUpvotedSubmissions: 5, oldUpvotedSubmissions: 5,
mostUpvotedInLockedVideoSum: 0 mostUpvotedInLockedVideoSum: 0
}; };
assert.strictEqual(await getReputation(getHash(userIDHighRep)), calculateReputationFromMetrics(metrics));
assert.strictEqual(await getReputation(getHash(userIDHighRep)), calculateFromMetrics(metrics)); 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 () => {
// 1.793103448275862
const metrics = { const metrics = {
totalSubmissions: 8, totalSubmissions: 8,
downvotedSubmissions: 1, downvotedSubmissions: 1,
@@ -188,11 +186,11 @@ describe("reputation", () => {
oldUpvotedSubmissions: 5, oldUpvotedSubmissions: 5,
mostUpvotedInLockedVideoSum: 0 mostUpvotedInLockedVideoSum: 0
}; };
assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), calculateFromMetrics(metrics)); assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), calculateReputationFromMetrics(metrics));
assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.793103448275862);
}); });
it("user with most upvoted segments in locked video", async () => { it("user with most upvoted segments in locked video", async () => {
// 6.158620689655172
const metrics = { const metrics = {
totalSubmissions: 10, totalSubmissions: 10,
downvotedSubmissions: 1, downvotedSubmissions: 1,
@@ -203,7 +201,8 @@ describe("reputation", () => {
oldUpvotedSubmissions: 6, oldUpvotedSubmissions: 6,
mostUpvotedInLockedVideoSum: 2 mostUpvotedInLockedVideoSum: 2
}; };
assert.strictEqual(await getReputation(getHash(userIDHaveMostUpvotedInLockedVideo)), calculateFromMetrics(metrics)); assert.strictEqual(await getReputation(getHash(userIDHaveMostUpvotedInLockedVideo)), calculateReputationFromMetrics(metrics));
assert.strictEqual(await getReputation(getHash(userIDHaveMostUpvotedInLockedVideo)), 6.158620689655172);
}); });
}); });