mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-09-20 04:54:00 +02:00
Show total stats if not under high load
This commit is contained in:
parent
df76b5f053
commit
87e38c8bc4
7 changed files with 46 additions and 5 deletions
|
@ -78,7 +78,8 @@ addDefaults(config, {
|
|||
idleTimeoutMillis: 10000,
|
||||
maxTries: 3,
|
||||
maxActiveRequests: 0,
|
||||
timeout: 60000
|
||||
timeout: 60000,
|
||||
highLoadThreshold: 10
|
||||
},
|
||||
postgresReadOnly: {
|
||||
enabled: false,
|
||||
|
|
|
@ -7,6 +7,8 @@ export interface IDatabase {
|
|||
init(): Promise<void>;
|
||||
|
||||
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any | any[] | void>;
|
||||
|
||||
highLoad(): boolean;
|
||||
}
|
||||
|
||||
export type QueryType = "get" | "all" | "run";
|
|
@ -32,4 +32,8 @@ export class Mysql implements IDatabase {
|
|||
}
|
||||
}
|
||||
|
||||
highLoad() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -235,4 +235,8 @@ export class Postgres implements IDatabase {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
highLoad() {
|
||||
return this.activePostgresRequests > this.config.postgres.highLoadThreshold;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,6 +95,10 @@ export class Sqlite implements IDatabase {
|
|||
private static processUpgradeQuery(query: string): string {
|
||||
return query.replace(/^.*--!sqlite-ignore/gm, "");
|
||||
}
|
||||
|
||||
highLoad() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export interface SqliteConfig {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -25,6 +25,7 @@ export interface CustomPostgresConfig extends PoolConfig {
|
|||
export interface CustomWritePostgresConfig extends CustomPostgresConfig {
|
||||
maxActiveRequests: number;
|
||||
timeout: number;
|
||||
highLoadThreshold: number;
|
||||
}
|
||||
|
||||
export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {
|
||||
|
|
Loading…
Reference in a new issue