mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 14:37:17 +03:00
Add bulk rating fetching
This commit is contained in:
@@ -17,11 +17,24 @@ interface DBRating {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getRating(req: Request, res: Response): Promise<Response> {
|
export async function getRating(req: Request, res: Response): Promise<Response> {
|
||||||
let hashPrefix = req.params.prefix as VideoIDHash;
|
let hashPrefixes: VideoIDHash[] = [];
|
||||||
if (!hashPrefix || !hashPrefixTester(hashPrefix)) {
|
try {
|
||||||
|
hashPrefixes = req.query.hashPrefixes
|
||||||
|
? JSON.parse(req.query.hashPrefixes as string)
|
||||||
|
: Array.isArray(req.query.prefix)
|
||||||
|
? req.query.prefix
|
||||||
|
: [req.query.prefix ?? req.params.prefix];
|
||||||
|
if (!Array.isArray(hashPrefixes)) {
|
||||||
|
return res.status(400).send("hashPrefixes parameter does not match format requirements.");
|
||||||
|
}
|
||||||
|
|
||||||
|
hashPrefixes.map((hashPrefix) => hashPrefix?.toLowerCase());
|
||||||
|
} catch(error) {
|
||||||
|
return res.status(400).send("Bad parameter: hashPrefixes (invalid JSON)");
|
||||||
|
}
|
||||||
|
if (hashPrefixes.some((hashPrefix) => !hashPrefix || !hashPrefixTester(hashPrefix))) {
|
||||||
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
||||||
}
|
}
|
||||||
hashPrefix = hashPrefix.toLowerCase() as VideoIDHash;
|
|
||||||
|
|
||||||
let types: RatingType[] = [];
|
let types: RatingType[] = [];
|
||||||
try {
|
try {
|
||||||
@@ -44,7 +57,7 @@ export async function getRating(req: Request, res: Response): Promise<Response>
|
|||||||
const service: Service = getService(req.query.service, req.body.service);
|
const service: Service = getService(req.query.service, req.body.service);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ratings = (await getRatings(hashPrefix, service))
|
const ratings = (await getRatings(hashPrefixes, service))
|
||||||
.filter((rating) => types.includes(rating.type))
|
.filter((rating) => types.includes(rating.type))
|
||||||
.map((rating) => ({
|
.map((rating) => ({
|
||||||
videoID: rating.videoID,
|
videoID: rating.videoID,
|
||||||
@@ -53,23 +66,25 @@ export async function getRating(req: Request, res: Response): Promise<Response>
|
|||||||
type: rating.type,
|
type: rating.type,
|
||||||
count: rating.count
|
count: rating.count
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return res.status((ratings.length) ? 200 : 404)
|
return res.status((ratings.length) ? 200 : 404)
|
||||||
.send(ratings ?? []);
|
.send(ratings ?? []);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
|
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRatings(hashPrefix: VideoIDHash, service: Service): Promise<DBRating[]> {
|
function getRatings(hashPrefixes: VideoIDHash[], service: Service): Promise<DBRating[]> {
|
||||||
const fetchFromDB = () => db
|
const fetchFromDB = (hashPrefixes: VideoIDHash[]) => db
|
||||||
.prepare(
|
.prepare(
|
||||||
"all",
|
"all",
|
||||||
`SELECT "videoID", "hashedVideoID", "type", "count" FROM "ratings" WHERE "hashedVideoID" LIKE ? AND "service" = ? ORDER BY "hashedVideoID"`,
|
`SELECT "videoID", "hashedVideoID", "type", "count" FROM "ratings" WHERE "hashedVideoID" ~* ? AND "service" = ? ORDER BY "hashedVideoID"`,
|
||||||
[`${hashPrefix}%`, service]
|
[`^(?:${hashPrefixes.join("|")})`, service]
|
||||||
) as Promise<DBRating[]>;
|
) as Promise<DBRating[]>;
|
||||||
|
|
||||||
return (hashPrefix.length === 4)
|
return (hashPrefixes.every((hashPrefix) => hashPrefix.length === 4))
|
||||||
? QueryCacher.get(fetchFromDB, ratingHashKey(hashPrefix, service))
|
? QueryCacher.getAndSplit(fetchFromDB, (prefix) => ratingHashKey(prefix, service), "hashedVideoID", hashPrefixes)
|
||||||
: fetchFromDB();
|
: fetchFromDB(hashPrefixes);
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
|
|||||||
if (!err && reply) {
|
if (!err && reply) {
|
||||||
try {
|
try {
|
||||||
Logger.debug(`Got data from redis: ${reply}`);
|
Logger.debug(`Got data from redis: ${reply}`);
|
||||||
|
|
||||||
return JSON.parse(reply);
|
return JSON.parse(reply);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// If all else, continue on to fetching from the database
|
// If all else, continue on to fetching from the database
|
||||||
@@ -22,6 +23,53 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets from redis for all specified values and splits the result before adding it to redis cache
|
||||||
|
*/
|
||||||
|
async function getAndSplit<T, U>(fetchFromDB: (values: U[]) => Promise<Array<T>>, keyGenerator: (value: U) => string, splitKey: string, values: U[]): Promise<Array<T>> {
|
||||||
|
const cachedValues = await Promise.all(values.map(async (value) => {
|
||||||
|
const key = keyGenerator(value);
|
||||||
|
const { err, reply } = await redis.getAsync(key);
|
||||||
|
|
||||||
|
if (!err && reply) {
|
||||||
|
try {
|
||||||
|
Logger.debug(`Got data from redis: ${reply}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
value,
|
||||||
|
result: JSON.parse(reply)
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
value,
|
||||||
|
result: null
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
|
||||||
|
const data = await fetchFromDB(
|
||||||
|
cachedValues.filter((cachedValue) => cachedValue.result === null)
|
||||||
|
.map((cachedValue) => cachedValue.value));
|
||||||
|
|
||||||
|
new Promise(() => {
|
||||||
|
const newResults: Record<string, T[]> = {};
|
||||||
|
for (const item of data) {
|
||||||
|
const key = (item as unknown as Record<string, string>)[splitKey];
|
||||||
|
newResults[key] ??= [];
|
||||||
|
newResults[key].push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const key in newResults) {
|
||||||
|
redis.setAsync(keyGenerator(key as unknown as U), JSON.stringify(newResults[key]));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return data.concat(cachedValues.map((cachedValue) => cachedValue.result).filter((result) => result !== null));
|
||||||
|
}
|
||||||
|
|
||||||
function clearSegmentCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID?: UserID; }): void {
|
function clearSegmentCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID?: UserID; }): void {
|
||||||
if (videoInfo) {
|
if (videoInfo) {
|
||||||
redis.delAsync(skipSegmentsKey(videoInfo.videoID, videoInfo.service));
|
redis.delAsync(skipSegmentsKey(videoInfo.videoID, videoInfo.service));
|
||||||
@@ -38,6 +86,7 @@ function clearRatingCache(videoInfo: { hashedVideoID: VideoIDHash; service: Serv
|
|||||||
|
|
||||||
export const QueryCacher = {
|
export const QueryCacher = {
|
||||||
get,
|
get,
|
||||||
|
getAndSplit,
|
||||||
clearSegmentCache,
|
clearSegmentCache,
|
||||||
clearRatingCache
|
clearRatingCache
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user