Show total stats if not under high load

This commit is contained in:
Ajay
2022-11-20 00:47:41 -05:00
parent df76b5f053
commit 87e38c8bc4
7 changed files with 46 additions and 5 deletions

View File

@@ -12,13 +12,26 @@ let firefoxUsersCache = 0;
let apiUsersCache = 0;
let lastUserCountCheck = 0;
interface DBStatsData {
userCount: number,
viewCount: number,
totalSubmissions: number,
minutesSaved: number
}
let lastFetch: DBStatsData = {
userCount: 0,
viewCount: 0,
totalSubmissions: 0,
minutesSaved: 0
};
updateExtensionUsers();
export async function getTotalStats(req: Request, res: Response): Promise<void> {
const userCountQuery = `(SELECT COUNT(*) FROM (SELECT DISTINCT "userID" from "sponsorTimes") t) "userCount",`;
const row = await db.prepare("get", `SELECT ${req.query.countContributingUsers ? userCountQuery : ""} COUNT(*) as "totalSubmissions",
SUM("views") as "viewCount", SUM(("endTime" - "startTime") / 60 * "views") as "minutesSaved" FROM "sponsorTimes" WHERE "shadowHidden" != 1 AND "votes" >= 0 AND "actionType" != 'chapter'`, []);
const row = await getStats(!!req.query.countContributingUsers);
lastFetch = row;
if (row !== undefined) {
const extensionUsers = chromeUsersCache + firefoxUsersCache;
@@ -43,6 +56,18 @@ export async function getTotalStats(req: Request, res: Response): Promise<void>
}
}
function getStats(countContributingUsers: boolean): Promise<DBStatsData> {
if (db.highLoad()) {
return Promise.resolve(lastFetch);
} else {
const userCountQuery = `(SELECT COUNT(*) FROM (SELECT DISTINCT "userID" from "sponsorTimes") t) "userCount",`;
return db.prepare("get", `SELECT ${countContributingUsers ? userCountQuery : ""} COUNT(*) as "totalSubmissions",
SUM("views") as "viewCount", SUM(("endTime" - "startTime") / 60 * "views") as "minutesSaved" FROM "sponsorTimes" WHERE "shadowHidden" != 1 AND "votes" >= 0 AND "actionType" != 'chapter'`, []);
}
}
function updateExtensionUsers() {
if (config.userCounterURL) {
axios.get(`${config.userCounterURL}/api/v1/userCount`)
@@ -68,7 +93,7 @@ function updateExtensionUsers() {
const userDownloadsStartIndex = body.indexOf(matchingString);
if (userDownloadsStartIndex >= 0) {
const closingQuoteIndex = body.indexOf('"', userDownloadsStartIndex + matchingStringLen);
const userDownloadsStr = body.substr(userDownloadsStartIndex + matchingStringLen, closingQuoteIndex - userDownloadsStartIndex).replace(",","").replace(".","");
const userDownloadsStr = body.substr(userDownloadsStartIndex + matchingStringLen, closingQuoteIndex - userDownloadsStartIndex).replace(",", "").replace(".", "");
chromeUsersCache = parseInt(userDownloadsStr);
}
else {