mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 22:17:14 +03:00
Show total stats if not under high load
This commit is contained in:
@@ -78,7 +78,8 @@ addDefaults(config, {
|
|||||||
idleTimeoutMillis: 10000,
|
idleTimeoutMillis: 10000,
|
||||||
maxTries: 3,
|
maxTries: 3,
|
||||||
maxActiveRequests: 0,
|
maxActiveRequests: 0,
|
||||||
timeout: 60000
|
timeout: 60000,
|
||||||
|
highLoadThreshold: 10
|
||||||
},
|
},
|
||||||
postgresReadOnly: {
|
postgresReadOnly: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ export interface IDatabase {
|
|||||||
init(): Promise<void>;
|
init(): Promise<void>;
|
||||||
|
|
||||||
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any | any[] | void>;
|
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any | any[] | void>;
|
||||||
|
|
||||||
|
highLoad(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type QueryType = "get" | "all" | "run";
|
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;
|
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 {
|
private static processUpgradeQuery(query: string): string {
|
||||||
return query.replace(/^.*--!sqlite-ignore/gm, "");
|
return query.replace(/^.*--!sqlite-ignore/gm, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
highLoad() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SqliteConfig {
|
export interface SqliteConfig {
|
||||||
|
|||||||
@@ -12,13 +12,26 @@ let firefoxUsersCache = 0;
|
|||||||
let apiUsersCache = 0;
|
let apiUsersCache = 0;
|
||||||
let lastUserCountCheck = 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();
|
updateExtensionUsers();
|
||||||
|
|
||||||
export async function getTotalStats(req: Request, res: Response): Promise<void> {
|
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",
|
const row = await getStats(!!req.query.countContributingUsers);
|
||||||
SUM("views") as "viewCount", SUM(("endTime" - "startTime") / 60 * "views") as "minutesSaved" FROM "sponsorTimes" WHERE "shadowHidden" != 1 AND "votes" >= 0 AND "actionType" != 'chapter'`, []);
|
lastFetch = row;
|
||||||
|
|
||||||
if (row !== undefined) {
|
if (row !== undefined) {
|
||||||
const extensionUsers = chromeUsersCache + firefoxUsersCache;
|
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() {
|
function updateExtensionUsers() {
|
||||||
if (config.userCounterURL) {
|
if (config.userCounterURL) {
|
||||||
axios.get(`${config.userCounterURL}/api/v1/userCount`)
|
axios.get(`${config.userCounterURL}/api/v1/userCount`)
|
||||||
@@ -68,7 +93,7 @@ function updateExtensionUsers() {
|
|||||||
const userDownloadsStartIndex = body.indexOf(matchingString);
|
const userDownloadsStartIndex = body.indexOf(matchingString);
|
||||||
if (userDownloadsStartIndex >= 0) {
|
if (userDownloadsStartIndex >= 0) {
|
||||||
const closingQuoteIndex = body.indexOf('"', userDownloadsStartIndex + matchingStringLen);
|
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);
|
chromeUsersCache = parseInt(userDownloadsStr);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ export interface CustomPostgresConfig extends PoolConfig {
|
|||||||
export interface CustomWritePostgresConfig extends CustomPostgresConfig {
|
export interface CustomWritePostgresConfig extends CustomPostgresConfig {
|
||||||
maxActiveRequests: number;
|
maxActiveRequests: number;
|
||||||
timeout: number;
|
timeout: number;
|
||||||
|
highLoadThreshold: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {
|
export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {
|
||||||
|
|||||||
Reference in New Issue
Block a user