refactor(serverlist): refactored to also show uptime and other info about the server

This commit is contained in:
2025-10-31 07:14:11 -05:00
parent d60c08a281
commit e1e659f9b1
8 changed files with 205 additions and 23 deletions

View File

@@ -18,7 +18,7 @@ const registerSchema = z.object({
.string()
.min(3)
.max(32)
.regex(/^[a-zA-Z0-9_]+$/, "Only alphanumeric + underscores"),
.regex(/^[a-zA-Z0-9.]+$/, "Only alphanumeric + dots allowed"),
displayUsername: z.string().min(2).max(100).optional(), // optional in your API, but supported
});

View File

@@ -1,10 +1,12 @@
import type { Express, Request, Response } from "express";
import allServerStats from "./routes/allServerStats.js";
import modules from "./routes/modules/moduleRoutes.js";
import settings from "./routes/settings/settingRoutes.js";
import stats from "./routes/stats.js";
export const setupSystemRoutes = (app: Express, basePath: string) => {
app.use(basePath + "/api/system/stats", stats);
app.use(basePath + "/api/system/allservers/stats", allServerStats);
app.use(
basePath + "/api/system/settings", // will pass bc system admin but this is just telling us we need this

View File

@@ -0,0 +1,90 @@
import axios from "axios";
import { format } from "date-fns-tz";
import { eq } from "drizzle-orm";
import { Router } from "express";
import { db } from "../../../pkg/db/db.js";
import { serverData } from "../../../pkg/db/schema/servers.js";
import {
type ServerStats,
serverStats,
} from "../../../pkg/db/schema/serverstats.js";
import dbTransport from "../../../pkg/logger/dbTransport.js";
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
import { checkBuildUpdate } from "../utlis/checkForBuild.js";
const router = Router();
type ServerInfo = {
server: string;
};
// GET /health
router.get("/", async (req, res) => {
const { data, error } = await tryCatch(
db.select().from(serverStats).where(eq(serverStats.id, "serverStats")),
);
if (error || !data) {
res.status(400).json({ error: error });
}
const statData = data as ServerStats[];
const used = process.memoryUsage();
const serverInfo = [
{
server: "Dev Server",
status: "ok",
plantToken: "test3",
uptime: process.uptime(),
build: statData[0]?.build,
pendingUpdateFile: await checkBuildUpdate(["."]),
lastUpdate: statData[0]?.lastUpdate
? format(statData[0].lastUpdate, "MM/dd/yyyy HH:mm")
: "",
memoryUsage: `Heap: ${(used.heapUsed / 1024 / 1024).toFixed(2)} MB / RSS: ${(
used.rss / 1024 / 1024
).toFixed(2)} MB`,
},
] as any;
// const { data: serverInfo } = await axios.get(
// `https://ushou1prod.alpla.net/lst/api/system/stats`,
// );
// get the server list so we can go to each and get there stats
const { data: sInfo, error: eInfo } = (await tryCatch(
db.select().from(serverData),
)) as any;
if (eInfo) {
console.log("There was an error getting the serverData");
}
for (let i = 0; i < sInfo.length; i++) {
//console.log(`Getting stats for ${sInfo[i].plantToken}`);
try {
let url = "";
if (sInfo[i].plantToken.includes("test")) {
url = `https://${sInfo[i].serverDNS}.alpla.net/lst/api/system/stats`;
} else {
url = `https://${sInfo[i].plantToken}prod.alpla.net/lst/api/system/stats`;
}
const { data: prodServerInfo } = await axios.get(url);
// console.log(
// `${sInfo[i].plantToken}, data ${JSON.stringify(prodServerInfo)}`,
// );
serverInfo.push({
server: sInfo[i].name,
status: prodServerInfo.status,
plantToken: sInfo[i].plantToken,
uptime: prodServerInfo.uptime,
build: prodServerInfo.build,
pendingUpdateFile: prodServerInfo.pendingUpdateFile,
lastUpdate: prodServerInfo.lastUpdate,
memoryUsage: prodServerInfo.memoryUsage,
});
} catch (e) {}
}
res.json(serverInfo);
});
export default router;