anything-llm/server/endpoints/utils.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-08-10 22:23:23 +02:00
const { SystemSettings } = require("../models/systemSettings");
function getGitVersion() {
2023-08-15 20:36:07 +02:00
try {
return require("child_process")
.execSync("git rev-parse HEAD")
.toString()
.trim();
} catch (e) {
console.error("getGitVersion", e.message);
return "--";
}
2023-08-10 22:23:23 +02:00
}
function byteToGigaByte(n) {
return n / Math.pow(10, 9);
}
async function getDiskStorage() {
try {
const checkDiskSpace = require("check-disk-space").default;
2023-08-10 22:50:17 +02:00
const { free, size } = await checkDiskSpace("/");
2023-08-10 22:23:23 +02:00
return {
current: Math.floor(byteToGigaByte(free)),
capacity: Math.floor(byteToGigaByte(size)),
};
} catch {
return {
current: null,
capacity: null,
};
}
}
function utilEndpoints(app) {
if (!app) return;
app.get("/utils/metrics", async (_, response) => {
try {
const metrics = {
online: true,
version: getGitVersion(),
mode: (await SystemSettings.isMultiUserMode())
? "multi-user"
: "single-user",
vectorDB: process.env.VECTOR_DB || "lancedb",
storage: await getDiskStorage(),
};
response.status(200).json(metrics);
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
});
}
module.exports = { utilEndpoints, getGitVersion };