mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 06:27:10 +03:00
Add postgres request time stats
This commit is contained in:
@@ -16,6 +16,13 @@ types.setTypeParser(20, function(val) {
|
|||||||
return parseInt(val, 10);
|
return parseInt(val, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
interface PostgresStats {
|
||||||
|
activeRequests: number;
|
||||||
|
avgReadTime: number;
|
||||||
|
avgWriteTime: number;
|
||||||
|
avgFailedTime: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface DatabaseConfig {
|
export interface DatabaseConfig {
|
||||||
dbSchemaFileName: string,
|
dbSchemaFileName: string,
|
||||||
dbSchemaFolder: string,
|
dbSchemaFolder: string,
|
||||||
@@ -34,6 +41,10 @@ export class Postgres implements IDatabase {
|
|||||||
private lastPoolReadFail = 0;
|
private lastPoolReadFail = 0;
|
||||||
|
|
||||||
activePostgresRequests = 0;
|
activePostgresRequests = 0;
|
||||||
|
readResponseTime: number[] = [];
|
||||||
|
writeResponseTime: number[] = [];
|
||||||
|
failedResponseTime: number[] = [];
|
||||||
|
maxStoredTimes = 200;
|
||||||
|
|
||||||
constructor(private config: DatabaseConfig) {}
|
constructor(private config: DatabaseConfig) {}
|
||||||
|
|
||||||
@@ -110,6 +121,7 @@ export class Postgres implements IDatabase {
|
|||||||
throw new Error("Too many active postgres requests");
|
throw new Error("Too many active postgres requests");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const start = Date.now();
|
||||||
const pendingQueries: PromiseWithState<QueryResult<any>>[] = [];
|
const pendingQueries: PromiseWithState<QueryResult<any>>[] = [];
|
||||||
let tries = 0;
|
let tries = 0;
|
||||||
let lastPool: Pool = null;
|
let lastPool: Pool = null;
|
||||||
@@ -128,6 +140,8 @@ export class Postgres implements IDatabase {
|
|||||||
else if (this.config.postgres.timeout) currentPromises.push(savePromiseState(timeoutPomise(this.config.postgres.timeout)));
|
else if (this.config.postgres.timeout) currentPromises.push(savePromiseState(timeoutPomise(this.config.postgres.timeout)));
|
||||||
const queryResult = await nextFulfilment(currentPromises);
|
const queryResult = await nextFulfilment(currentPromises);
|
||||||
|
|
||||||
|
this.updateResponseTime(type, start);
|
||||||
|
|
||||||
this.activePostgresRequests--;
|
this.activePostgresRequests--;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "get": {
|
case "get": {
|
||||||
@@ -156,6 +170,7 @@ export class Postgres implements IDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.updateResponseTime(type, start, this.failedResponseTime);
|
||||||
this.activePostgresRequests--;
|
this.activePostgresRequests--;
|
||||||
Logger.error(`prepare (postgres) try ${tries}: ${err}`);
|
Logger.error(`prepare (postgres) try ${tries}: ${err}`);
|
||||||
}
|
}
|
||||||
@@ -236,6 +251,25 @@ export class Postgres implements IDatabase {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private updateResponseTime(type: string, start: number, customArray?: number[]): void {
|
||||||
|
const responseTime = Date.now() - start;
|
||||||
|
|
||||||
|
const array = customArray ?? (this.isReadQuery(type) ?
|
||||||
|
this.readResponseTime : this.writeResponseTime);
|
||||||
|
|
||||||
|
array.push(responseTime);
|
||||||
|
if (array.length > this.maxStoredTimes) array.shift();
|
||||||
|
}
|
||||||
|
|
||||||
|
getStats(): PostgresStats {
|
||||||
|
return {
|
||||||
|
activeRequests: this.activePostgresRequests,
|
||||||
|
avgReadTime: this.readResponseTime.length > 0 ? this.readResponseTime.reduce((a, b) => a + b, 0) / this.readResponseTime.length : 0,
|
||||||
|
avgWriteTime: this.writeResponseTime.length > 0 ? this.writeResponseTime.reduce((a, b) => a + b, 0) / this.writeResponseTime.length : 0,
|
||||||
|
avgFailedTime: this.failedResponseTime.length > 0 ? this.failedResponseTime.reduce((a, b) => a + b, 0) / this.failedResponseTime.length : 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
highLoad() {
|
highLoad() {
|
||||||
return this.activePostgresRequests > this.config.postgres.highLoadThreshold;
|
return this.activePostgresRequests > this.config.postgres.highLoadThreshold;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export async function getStatus(req: Request, res: Response): Promise<Response>
|
|||||||
loadavg: os.loadavg().slice(1), // only return 5 & 15 minute load average
|
loadavg: os.loadavg().slice(1), // only return 5 & 15 minute load average
|
||||||
statusRequests,
|
statusRequests,
|
||||||
hostname: os.hostname(),
|
hostname: os.hostname(),
|
||||||
activePostgresRequests: (db as Postgres)?.activePostgresRequests,
|
postgresStats: (db as Postgres)?.getStats(),
|
||||||
redisStats: getRedisStats(),
|
redisStats: getRedisStats(),
|
||||||
};
|
};
|
||||||
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
||||||
|
|||||||
Reference in New Issue
Block a user