mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 09:07:47 +01:00
add redis status/min
This commit is contained in:
parent
ceabeefe21
commit
b9a620fc3b
3 changed files with 38 additions and 2 deletions
|
@ -2,6 +2,7 @@ import { db } from "../databases/databases";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
|
import redis from "../utils/redis";
|
||||||
|
|
||||||
export async function getStatus(req: Request, res: Response): Promise<Response> {
|
export async function getStatus(req: Request, res: Response): Promise<Response> {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
@ -9,13 +10,16 @@ export async function getStatus(req: Request, res: Response): Promise<Response>
|
||||||
value = Array.isArray(value) ? value[0] : value;
|
value = Array.isArray(value) ? value[0] : value;
|
||||||
try {
|
try {
|
||||||
const dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
const dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
||||||
|
const numberRequests = await redis.increment("statusRequest");
|
||||||
|
const statusRequests = numberRequests?.replies?.[0];
|
||||||
const statusValues: Record<string, any> = {
|
const statusValues: Record<string, any> = {
|
||||||
uptime: process.uptime(),
|
uptime: process.uptime(),
|
||||||
commit: (global as any).HEADCOMMIT || "unknown",
|
commit: (global as any).HEADCOMMIT || "unknown",
|
||||||
db: Number(dbVersion),
|
db: Number(dbVersion),
|
||||||
startTime,
|
startTime,
|
||||||
processTime: Date.now() - startTime,
|
processTime: Date.now() - startTime,
|
||||||
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
|
||||||
};
|
};
|
||||||
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -9,6 +9,7 @@ interface RedisSB {
|
||||||
setAsync?(key: string, value: string): Promise<{err: Error | null, reply: string | null}>;
|
setAsync?(key: string, value: string): Promise<{err: Error | null, reply: string | null}>;
|
||||||
delAsync?(...keys: [string]): Promise<Error | null>;
|
delAsync?(...keys: [string]): Promise<Error | null>;
|
||||||
close?(flush?: boolean): void;
|
close?(flush?: boolean): void;
|
||||||
|
increment?(key: string): Promise<{err: Error| null, replies: any[] | null}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
let exportObject: RedisSB = {
|
let exportObject: RedisSB = {
|
||||||
|
@ -20,6 +21,8 @@ let exportObject: RedisSB = {
|
||||||
new Promise((resolve) => resolve({ err: null, reply: undefined })),
|
new Promise((resolve) => resolve({ err: null, reply: undefined })),
|
||||||
delAsync: () =>
|
delAsync: () =>
|
||||||
new Promise((resolve) => resolve(null)),
|
new Promise((resolve) => resolve(null)),
|
||||||
|
increment: () =>
|
||||||
|
new Promise((resolve) => resolve({ err: null, replies: undefined })),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config.redis) {
|
if (config.redis) {
|
||||||
|
@ -31,7 +34,12 @@ if (config.redis) {
|
||||||
exportObject.setAsync = (key, value) => new Promise((resolve) => client.set(key, value, (err, reply) => resolve({ err, reply })));
|
exportObject.setAsync = (key, value) => new Promise((resolve) => client.set(key, value, (err, reply) => resolve({ err, reply })));
|
||||||
exportObject.delAsync = (...keys) => new Promise((resolve) => client.del(keys, (err) => resolve(err)));
|
exportObject.delAsync = (...keys) => new Promise((resolve) => client.del(keys, (err) => resolve(err)));
|
||||||
exportObject.close = (flush) => client.end(flush);
|
exportObject.close = (flush) => client.end(flush);
|
||||||
|
exportObject.increment = (key) => new Promise((resolve) =>
|
||||||
|
client.multi()
|
||||||
|
.incr(key)
|
||||||
|
.expire(key, 60)
|
||||||
|
.exec((err, replies) => resolve({ err, replies }))
|
||||||
|
);
|
||||||
client.on("error", function(error) {
|
client.on("error", function(error) {
|
||||||
Logger.error(error);
|
Logger.error(error);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
|
import { config } from "../../src/config";
|
||||||
let dbVersion: number;
|
let dbVersion: number;
|
||||||
|
|
||||||
describe("getStatus", () => {
|
describe("getStatus", () => {
|
||||||
|
@ -86,4 +87,27 @@ describe("getStatus", () => {
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to get statusRequests only", function (done) {
|
||||||
|
if (!config.redis) this.skip();
|
||||||
|
client.get(`${endpoint}/statusRequests`)
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
assert.ok(Number(res.data) > 1);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should be able to get status with statusRequests", function (done) {
|
||||||
|
if (!config.redis) this.skip();
|
||||||
|
client.get(endpoint)
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = res.data;
|
||||||
|
assert.ok(data.statusRequests > 2);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue