add service metrics

This commit is contained in:
timothycarambat 2023-08-10 13:23:23 -07:00
parent 970d38ccb5
commit 83ad64b824
4 changed files with 60 additions and 0 deletions

52
server/endpoints/utils.js Normal file
View File

@ -0,0 +1,52 @@
const { SystemSettings } = require("../models/systemSettings");
function getGitVersion() {
return require("child_process")
.execSync("git rev-parse HEAD")
.toString()
.trim();
}
function byteToGigaByte(n) {
return n / Math.pow(10, 9);
}
async function getDiskStorage() {
try {
const checkDiskSpace = require("check-disk-space").default;
const { free, size } = await checkDiskSpace("/dev/sda1");
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 };

View File

@ -15,6 +15,7 @@ const { getVectorDbClass } = require("./utils/helpers");
const { validateTablePragmas } = require("./utils/database");
const { adminEndpoints } = require("./endpoints/admin");
const { inviteEndpoints } = require("./endpoints/invite");
const { utilEndpoints } = require("./endpoints/utils");
const app = express();
const apiRouter = express.Router();
@ -33,6 +34,7 @@ workspaceEndpoints(apiRouter);
chatEndpoints(apiRouter);
adminEndpoints(apiRouter);
inviteEndpoints(apiRouter);
utilEndpoints(apiRouter);
apiRouter.post("/v/:command", async (request, response) => {
try {

View File

@ -21,6 +21,7 @@
"archiver": "^5.3.1",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.2",
"check-disk-space": "^3.4.0",
"chromadb": "^1.5.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3",

View File

@ -566,6 +566,11 @@ chalk@^2.4.2:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"
check-disk-space@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/check-disk-space/-/check-disk-space-3.4.0.tgz#eb8e69eee7a378fd12e35281b8123a8b4c4a8ff7"
integrity sha512-drVkSqfwA+TvuEhFipiR1OC9boEGZL5RrWvVsOthdcvQNXyCCuKkEiTOTXZ7qxSf/GLwq4GvzfrQD/Wz325hgw==
chokidar@^3.5.2:
version "3.5.3"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd"