This commit is contained in:
Madeline
2022-09-26 22:41:21 +10:00
parent fcc0884e36
commit 99ee7e9400
280 changed files with 6800 additions and 3908 deletions
+34 -17
View File
@@ -5,14 +5,22 @@ import mysql from "mysql2";
import fetch from "node-fetch";
const dbConn = mysql.createConnection(process.env.DATABASE as string);
const executePromise = (sql: string, args: any[]) => new Promise((resolve, reject) => dbConn.execute(sql, args, (err, res) => { if (err) reject(err); else resolve(res); }));
const executePromise = (sql: string, args: any[]) =>
new Promise((resolve, reject) =>
dbConn.execute(sql, args, (err, res) => {
if (err) reject(err);
else resolve(res);
}),
);
const savePerf = async (time: number, name: string, error?: string | Error) => {
if (error && typeof error != "string") error = error.message;
try {
await executePromise("INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)", [time ?? 0, name, new Date(), error ?? null]);
await executePromise(
"INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)",
[time ?? 0, name, new Date(), error ?? null],
);
// await executePromise("DELETE FROM performance WHERE DATE(timestamp) < now() - interval ? DAY", [process.env.RETENTION_DAYS]);
}
catch (e) {
} catch (e) {
console.error(e);
}
};
@@ -23,7 +31,11 @@ const doMeasurements = async (channel: Discord.TextChannel) => {
timestamp = Date.now();
await channel.send("hello this is a special message kthxbye");
setTimeout(doMeasurements, parseInt(process.env.MEASURE_INTERVAL as string), channel);
setTimeout(
doMeasurements,
parseInt(process.env.MEASURE_INTERVAL as string),
channel,
);
};
const instance = {
@@ -37,8 +49,8 @@ const client = new Fosscord.Client({
intents: [],
http: {
api: instance.api,
cdn: instance.cdn
}
cdn: instance.cdn,
},
});
client.on("ready", async () => {
@@ -52,19 +64,24 @@ client.on("ready", async () => {
client.on("messageCreate", async (msg: Discord.Message) => {
if (!timestamp) return;
if (msg.author.id != "992745947417141682"
|| msg.channel.id != "1019955729054267764"
|| msg.content != "hello this is a special message kthxbye")
if (
msg.author.id != "992745947417141682" ||
msg.channel.id != "1019955729054267764" ||
msg.content != "hello this is a special message kthxbye"
)
return;
await savePerf(Date.now() - timestamp, "messageCreate", undefined);
timestamp = undefined;
await fetch(`${instance.api}/channels/1019955729054267764/messages/${msg.id}`, {
method: "DELETE",
headers: {
authorization: instance.token
}
})
await fetch(
`${instance.api}/channels/1019955729054267764/messages/${msg.id}`,
{
method: "DELETE",
headers: {
authorization: instance.token,
},
},
);
});
client.on("error", (error: any) => {
@@ -79,4 +96,4 @@ client.on("warn", (msg: any) => {
await new Promise((resolve) => dbConn.connect(resolve));
console.log("Connected to db");
await client.login(instance.token);
})();
})();
+82 -52
View File
@@ -4,7 +4,13 @@ import mysql from "mysql2";
import fetch from "node-fetch";
const dbConn = mysql.createConnection(process.env.DATABASE as string);
const executePromise = (sql: string, args: any[]) => new Promise((resolve, reject) => dbConn.execute(sql, args, (err, res) => { if (err) reject(err); else resolve(res); }));
const executePromise = (sql: string, args: any[]) =>
new Promise((resolve, reject) =>
dbConn.execute(sql, args, (err, res) => {
if (err) reject(err);
else resolve(res);
}),
);
const instance = {
app: process.env.INSTANCE_WEB_APP as string,
@@ -16,73 +22,86 @@ const instance = {
const savePerf = async (time: number, name: string, error?: string | Error) => {
if (error && typeof error != "string") error = error.message;
try {
await executePromise("INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)", [time ?? 0, name, new Date(), error ?? null]);
await executePromise(
"INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)",
[time ?? 0, name, new Date(), error ?? null],
);
// await executePromise("DELETE FROM performance WHERE DATE(timestamp) < now() - interval ? DAY", [process.env.RETENTION_DAYS]);
}
catch (e) {
} catch (e) {
console.error(e);
}
};
const saveSystemUsage = async (load: number, procUptime: number, sysUptime: number, ram: number, sessions: number) => {
const saveSystemUsage = async (
load: number,
procUptime: number,
sysUptime: number,
ram: number,
sessions: number,
) => {
try {
await executePromise("INSERT INTO monitor (time, cpu, procUp, sysUp, ram, sessions) VALUES (?, ?, ?, ?, ?, ?)", [new Date(), load, procUptime, sysUptime, ram, sessions]);
}
catch (e) {
await executePromise(
"INSERT INTO monitor (time, cpu, procUp, sysUp, ram, sessions) VALUES (?, ?, ?, ?, ?, ?)",
[new Date(), load, procUptime, sysUptime, ram, sessions],
);
} catch (e) {
console.error(e);
}
};
const makeTimedRequest = (path: string, body?: object): Promise<number> => new Promise((resolve, reject) => {
const opts = {
hostname: new URL(path).hostname,
port: 443,
path: new URL(path).pathname,
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": instance.token,
},
timeout: 1000,
};
const makeTimedRequest = (path: string, body?: object): Promise<number> =>
new Promise((resolve, reject) => {
const opts = {
hostname: new URL(path).hostname,
port: 443,
path: new URL(path).pathname,
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: instance.token,
},
timeout: 1000,
};
let start: number, end: number;
const req = https.request(opts, res => {
if (res.statusCode! < 200 || res.statusCode! > 300) {
return reject(`${res.statusCode} ${res.statusMessage}`);
}
let start: number, end: number;
const req = https.request(opts, (res) => {
if (res.statusCode! < 200 || res.statusCode! > 300) {
return reject(`${res.statusCode} ${res.statusMessage}`);
}
res.on("data", (data) => {
res.on("data", (data) => {});
res.on("end", () => {
end = Date.now();
resolve(end - start);
});
});
res.on("end", () => {
end = Date.now();
resolve(end - start);
req.on("finish", () => {
if (body) req.write(JSON.stringify(body));
start = Date.now();
});
});
req.on("finish", () => {
if (body) req.write(JSON.stringify(body));
start = Date.now();
});
req.on("error", (error) => {
reject(error);
});
req.on("error", (error) => {
reject(error);
req.end();
});
req.end();
});
const measureApi = async (name: string, path: string, body?: object) => {
let error, time = -1;
let error,
time = -1;
try {
time = await makeTimedRequest(path, body);
}
catch (e) {
} catch (e) {
error = e as Error | string;
}
console.log(`${name} took ${time}ms ${(error ? "with error" : "")}`, error ?? "");
console.log(
`${name} took ${time}ms ${error ? "with error" : ""}`,
error ?? "",
);
await savePerf(time, name, error);
};
@@ -100,7 +119,11 @@ const app = async () => {
console.log("Connected to db");
// await client.login(instance.token);
console.log(`Monitoring performance for instance at ${new URL(instance.api).hostname}`);
console.log(
`Monitoring performance for instance at ${
new URL(instance.api).hostname
}`,
);
const doMeasurements = async () => {
await measureApi("ping", `${instance.api}/ping`);
@@ -112,18 +135,25 @@ const app = async () => {
const res = await fetch(`${instance.api}/-/monitorz`, {
headers: {
Authorization: process.env.INSTANCE_TOKEN as string,
}
},
});
const json = await res.json() as monitorzSchema;
await saveSystemUsage(json.load[1], json.procUptime, json.sysUptime, json.memPercent, json.sessions);
}
catch (e) {
}
const json = (await res.json()) as monitorzSchema;
await saveSystemUsage(
json.load[1],
json.procUptime,
json.sysUptime,
json.memPercent,
json.sessions,
);
} catch (e) {}
setTimeout(doMeasurements, parseInt(process.env.MEASURE_INTERVAL as string));
setTimeout(
doMeasurements,
parseInt(process.env.MEASURE_INTERVAL as string),
);
};
doMeasurements();
};
app();
app();