mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-16 08:26:59 +03:00
add categoryStats and typeStats
This commit is contained in:
@@ -1,29 +1,68 @@
|
|||||||
import {db} from "../databases/databases";
|
import {db} from "../databases/databases";
|
||||||
import {getHash} from "../utils/getHash";
|
import {getHash} from "../utils/getHash";
|
||||||
import {Request, Response} from "express";
|
import {Request, Response} from "express";
|
||||||
import { HashedUserID, UserID } from "../types/user.model";
|
import {HashedUserID, UserID} from "../types/user.model";
|
||||||
import { Category } from "../types/segments.model";
|
|
||||||
import {config} from "../config";
|
import {config} from "../config";
|
||||||
const maxRewardTime = config.maxRewardTimePerSegmentInSeconds;
|
import { Logger } from "../utils/logger";
|
||||||
|
type nestedObj = Record<string, Record<string, number>>;
|
||||||
|
const maxRewardTimePerSegmentInSeconds = config.maxRewardTimePerSegmentInSeconds ?? 86400;
|
||||||
|
|
||||||
async function dbGetCategorySummary(userID: HashedUserID, category: Category): Promise<{ minutesSaved: number, segmentCount: number }> {
|
async function dbGetUserSummary(userID: HashedUserID, categoryStats: boolean, typeStats: boolean) {
|
||||||
|
let additionalQuery = "";
|
||||||
|
if (categoryStats) {
|
||||||
|
additionalQuery += `
|
||||||
|
SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as "categorySumSponsor",
|
||||||
|
SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as "categorySumIntro",
|
||||||
|
SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as "categorySumOutro",
|
||||||
|
SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as "categorySumInteraction",
|
||||||
|
SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as "categorySelfpromo",
|
||||||
|
SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as "categoryMusicOfftopic",
|
||||||
|
SUM(CASE WHEN category = 'preview' THEN 1 ELSE 0 END) as "categorySumPreview",
|
||||||
|
SUM(CASE WHEN category = 'poi_highlight' THEN 1 ELSE 0 END) as "categorySumHighlight",`;
|
||||||
|
}
|
||||||
|
if (typeStats) {
|
||||||
|
additionalQuery += `
|
||||||
|
SUM(CASE WHEN actionType = 'skip' THEN 1 ELSE 0 END) as 'typeSumSkip',
|
||||||
|
SUM(CASE WHEN actionType = 'mute' THEN 1 ELSE 0 END) as 'typeSumMute',`;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const row = await db.prepare("get",
|
const row = await db.prepare("get", `
|
||||||
`SELECT SUM(((CASE WHEN "endTime" - "startTime" > ? THEN ? ELSE "endTime" - "startTime" END) / 60) * "views") as "minutesSaved",
|
SELECT SUM(((CASE WHEN "endTime" - "startTime" > ? THEN ? ELSE "endTime" - "startTime" END) / 60) * "views") as "minutesSaved",
|
||||||
count(*) as "segmentCount" FROM "sponsorTimes"
|
${additionalQuery}
|
||||||
WHERE "userID" = ? AND "category" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [maxRewardTime, maxRewardTime, userID, category]);
|
count(*) as "segmentCount"
|
||||||
if (row.minutesSaved != null) {
|
FROM "sponsorTimes"
|
||||||
return {
|
WHERE "userID" = ? AND votes > -2 AND shadowHidden !=1`,
|
||||||
minutesSaved: row.minutesSaved,
|
[maxRewardTimePerSegmentInSeconds, maxRewardTimePerSegmentInSeconds, userID]);
|
||||||
segmentCount: row.segmentCount,
|
const source = (row.minutesSaved != null) ? row : {};
|
||||||
};
|
const handler = { get: (target: Record<string, any>, name: string) => target?.[name] || 0 };
|
||||||
} else {
|
const proxy = new Proxy(source, handler);
|
||||||
return {
|
const result = {} as nestedObj;
|
||||||
minutesSaved: 0,
|
|
||||||
segmentCount: 0,
|
result.overallStats = {
|
||||||
|
minutesSaved: proxy.minutesSaved,
|
||||||
|
segmentCount: proxy.segmentCount,
|
||||||
|
};
|
||||||
|
if (categoryStats) {
|
||||||
|
result.categoryCount = {
|
||||||
|
sponsor: proxy.categorySumSponsor,
|
||||||
|
intro: proxy.categorySumIntro,
|
||||||
|
outro: proxy.categorySumOutro,
|
||||||
|
interaction: proxy.categorySumInteraction,
|
||||||
|
selfpromo: proxy.categorySelfpromo,
|
||||||
|
music_offtopic: proxy.categoryMusicOfftopic,
|
||||||
|
preview: proxy.categorySumPreview,
|
||||||
|
poi_highlight: proxy.categorySumHighlight,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (typeStats) {
|
||||||
|
result.actionTypeCount = {
|
||||||
|
skip: proxy.typeSumSkip,
|
||||||
|
mute: proxy.typeSumMute,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return result;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
Logger.error(err as string);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,17 +79,18 @@ async function dbGetUsername(userID: HashedUserID) {
|
|||||||
export async function getUserStats(req: Request, res: Response): Promise<Response> {
|
export async function getUserStats(req: Request, res: Response): Promise<Response> {
|
||||||
const userID = req.query.userID as UserID;
|
const userID = req.query.userID as UserID;
|
||||||
const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID;
|
const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID;
|
||||||
|
const categoryStats = req.query.categoryStats == "true";
|
||||||
|
const typeStats = req.query.typeStats == "true";
|
||||||
|
|
||||||
if (hashedUserID == undefined) {
|
if (hashedUserID == undefined) {
|
||||||
//invalid request
|
//invalid request
|
||||||
return res.status(400).send("Invalid userID or publicUserID parameter");
|
return res.status(400).send("Invalid userID or publicUserID parameter");
|
||||||
}
|
}
|
||||||
|
const segmentSummary = await dbGetUserSummary(hashedUserID, categoryStats, typeStats);
|
||||||
const responseObj = {
|
const responseObj = {
|
||||||
userID: hashedUserID,
|
userID: hashedUserID,
|
||||||
userName: await dbGetUsername(hashedUserID),
|
userName: await dbGetUsername(hashedUserID),
|
||||||
} as Record<string, Record<string, number> | string >;
|
...segmentSummary,
|
||||||
for (const category of config.categoryList) {
|
} as Record<string, nestedObj | string>;
|
||||||
responseObj[category] = await dbGetCategorySummary(hashedUserID, category as Category);
|
|
||||||
}
|
|
||||||
return res.send(responseObj);
|
return res.send(responseObj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {Done, getbaseURL} from "../utils";
|
|||||||
import {db} from "../../src/databases/databases";
|
import {db} from "../../src/databases/databases";
|
||||||
import {getHash} from "../../src/utils/getHash";
|
import {getHash} from "../../src/utils/getHash";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
const includeAllStats = "&categoryStats=true&typeStats=true";
|
||||||
|
|
||||||
describe("getUserStats", () => {
|
describe("getUserStats", () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
@@ -31,30 +32,31 @@ describe("getUserStats", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get user info", (done: Done) => {
|
it("Should be able to get all user info", (done: Done) => {
|
||||||
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01`)
|
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01${includeAllStats}`)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = {
|
const expected = {
|
||||||
userName: "Username user 01",
|
userName: "Username user 01",
|
||||||
userID: getHash("getuserstats_user_01"),
|
userID: getHash("getuserstats_user_01"),
|
||||||
sponsor: {
|
categoryCount: {
|
||||||
minutesSaved: 1, segmentCount: 1,
|
sponsor: 1,
|
||||||
}, selfpromo: {
|
selfpromo: 1,
|
||||||
minutesSaved: 2, segmentCount: 1,
|
interaction: 1,
|
||||||
}, interaction: {
|
intro: 1,
|
||||||
minutesSaved: 3, segmentCount: 1,
|
outro: 1,
|
||||||
}, intro: {
|
preview: 1,
|
||||||
minutesSaved: 4, segmentCount: 1,
|
music_offtopic: 1,
|
||||||
}, outro: {
|
poi_highlight: 1,
|
||||||
minutesSaved: 5, segmentCount: 1,
|
|
||||||
}, preview: {
|
|
||||||
minutesSaved: 6, segmentCount: 1,
|
|
||||||
}, music_offtopic: {
|
|
||||||
minutesSaved: 7, segmentCount: 1,
|
|
||||||
}, poi_highlight: {
|
|
||||||
minutesSaved: 0, segmentCount: 1,
|
|
||||||
},
|
},
|
||||||
|
actionTypeCount: {
|
||||||
|
mute: 0,
|
||||||
|
skip: 8
|
||||||
|
},
|
||||||
|
overallStats: {
|
||||||
|
minutesSaved: 28,
|
||||||
|
segmentCount: 8
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
assert.deepStrictEqual(data, expected);
|
assert.deepStrictEqual(data, expected);
|
||||||
@@ -68,8 +70,8 @@ describe("getUserStats", () => {
|
|||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
for (const value in data) {
|
for (const value in data.overallStats) {
|
||||||
if (data[value]?.minutesSaved || data[value]?.segmentCount) {
|
if (data[value]) {
|
||||||
done(`returned non-zero for ${value}`);
|
done(`returned non-zero for ${value}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,8 +85,8 @@ describe("getUserStats", () => {
|
|||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
for (const value in data) {
|
for (const value in data.overallStats) {
|
||||||
if (data[value]?.minutesSaved || data[value]?.segmentCount) {
|
if (data[value]) {
|
||||||
done(`returned non-zero for ${value}`);
|
done(`returned non-zero for ${value}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,4 +94,18 @@ describe("getUserStats", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should not get extra stats if not requested", (done: Done) => {
|
||||||
|
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01`)
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
// check for categoryCount
|
||||||
|
if (data.categoryCount || data.actionTypeCount) {
|
||||||
|
done("returned extra stats");
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user