mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 14:37:17 +03:00
Add traces to branding endpoint
This commit is contained in:
@@ -19,7 +19,7 @@ enum BrandingSubmissionType {
|
|||||||
Thumbnail = "thumbnail"
|
Thumbnail = "thumbnail"
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getVideoBranding(videoID: VideoID, service: Service, ip: IPAddress): Promise<BrandingResult> {
|
export async function getVideoBranding(res: Response, videoID: VideoID, service: Service, ip: IPAddress): Promise<BrandingResult> {
|
||||||
const getTitles = () => db.prepare(
|
const getTitles = () => db.prepare(
|
||||||
"all",
|
"all",
|
||||||
`SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID"
|
`SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID"
|
||||||
@@ -43,7 +43,13 @@ export async function getVideoBranding(videoID: VideoID, service: Service, ip: I
|
|||||||
thumbnails: await getThumbnails()
|
thumbnails: await getThumbnails()
|
||||||
});
|
});
|
||||||
|
|
||||||
const branding = await QueryCacher.get(getBranding, brandingKey(videoID, service));
|
const brandingTrace = await QueryCacher.getTraced(getBranding, brandingKey(videoID, service));
|
||||||
|
const branding = brandingTrace.data;
|
||||||
|
|
||||||
|
// Add trace info to request for debugging purposes
|
||||||
|
res.setHeader("X-Start-Time", brandingTrace.startTime);
|
||||||
|
res.setHeader("X-DB-Start-Time", brandingTrace.dbStartTime);
|
||||||
|
res.setHeader("X-End-Time", brandingTrace.endTime);
|
||||||
|
|
||||||
const cache = {
|
const cache = {
|
||||||
currentIP: null as Promise<HashedIP> | null
|
currentIP: null as Promise<HashedIP> | null
|
||||||
@@ -177,7 +183,7 @@ export async function getBranding(req: Request, res: Response) {
|
|||||||
|
|
||||||
const ip = getIP(req);
|
const ip = getIP(req);
|
||||||
try {
|
try {
|
||||||
const result = await getVideoBranding(videoID, service, ip);
|
const result = await getVideoBranding(res, videoID, service, ip);
|
||||||
|
|
||||||
const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404;
|
const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404;
|
||||||
return res.status(status).json(result);
|
return res.status(status).json(result);
|
||||||
|
|||||||
@@ -22,6 +22,41 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function getTraced<T>(fetchFromDB: () => Promise<T>, key: string): Promise<{
|
||||||
|
data: T;
|
||||||
|
startTime: number;
|
||||||
|
dbStartTime?: number;
|
||||||
|
endTime: number;
|
||||||
|
}> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const reply = await redis.get(key);
|
||||||
|
if (reply) {
|
||||||
|
Logger.debug(`Got data from redis: ${reply}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: JSON.parse(reply),
|
||||||
|
startTime: startTime,
|
||||||
|
endTime: Date.now()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (e) { } //eslint-disable-line no-empty
|
||||||
|
|
||||||
|
const dbStartTime = Date.now();
|
||||||
|
const data = await fetchFromDB();
|
||||||
|
|
||||||
|
redis.setEx(key, config.redis?.expiryTime, JSON.stringify(data)).catch((err) => Logger.error(err));
|
||||||
|
|
||||||
|
return {
|
||||||
|
data,
|
||||||
|
startTime: startTime,
|
||||||
|
dbStartTime: dbStartTime,
|
||||||
|
endTime: Date.now()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets from redis for all specified values and splits the result before adding it to redis cache
|
* Gets from redis for all specified values and splits the result before adding it to redis cache
|
||||||
*/
|
*/
|
||||||
@@ -117,6 +152,7 @@ function clearFeatureCache(userID: HashedUserID, feature: Feature): void {
|
|||||||
|
|
||||||
export const QueryCacher = {
|
export const QueryCacher = {
|
||||||
get,
|
get,
|
||||||
|
getTraced,
|
||||||
getAndSplit,
|
getAndSplit,
|
||||||
clearSegmentCache,
|
clearSegmentCache,
|
||||||
clearBrandingCache,
|
clearBrandingCache,
|
||||||
|
|||||||
Reference in New Issue
Block a user