2023-08-10 13:23:23 -07:00
|
|
|
const { SystemSettings } = require("../models/systemSettings");
|
|
|
|
|
|
|
|
function getGitVersion() {
|
2023-08-15 11:36:07 -07:00
|
|
|
try {
|
|
|
|
return require("child_process")
|
|
|
|
.execSync("git rev-parse HEAD")
|
|
|
|
.toString()
|
|
|
|
.trim();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("getGitVersion", e.message);
|
|
|
|
return "--";
|
|
|
|
}
|
2023-08-10 13:23:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function byteToGigaByte(n) {
|
|
|
|
return n / Math.pow(10, 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDiskStorage() {
|
|
|
|
try {
|
|
|
|
const checkDiskSpace = require("check-disk-space").default;
|
2023-08-10 13:50:17 -07:00
|
|
|
const { free, size } = await checkDiskSpace("/");
|
2023-08-10 13:23:23 -07:00
|
|
|
return {
|
|
|
|
current: Math.floor(byteToGigaByte(free)),
|
|
|
|
capacity: Math.floor(byteToGigaByte(size)),
|
|
|
|
};
|
|
|
|
} catch {
|
|
|
|
return {
|
|
|
|
current: null,
|
|
|
|
capacity: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:59:51 -08:00
|
|
|
async function convertToCSV(workspaceChatsMap) {
|
|
|
|
const rows = ["role,content"];
|
|
|
|
for (const workspaceChats of Object.values(workspaceChatsMap)) {
|
|
|
|
for (const message of workspaceChats.messages) {
|
|
|
|
// Escape double quotes and wrap content in double quotes
|
|
|
|
const escapedContent = `"${message.content
|
|
|
|
.replace(/"/g, '""')
|
|
|
|
.replace(/\n/g, " ")}"`;
|
|
|
|
rows.push(`${message.role},${escapedContent}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function convertToJSON(workspaceChatsMap) {
|
|
|
|
const allMessages = [].concat.apply(
|
|
|
|
[],
|
|
|
|
Object.values(workspaceChatsMap).map((workspace) => workspace.messages)
|
|
|
|
);
|
|
|
|
return JSON.stringify(allMessages);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function convertToJSONL(workspaceChatsMap) {
|
|
|
|
return Object.values(workspaceChatsMap)
|
|
|
|
.map((workspaceChats) => JSON.stringify(workspaceChats))
|
|
|
|
.join("\n");
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:23:23 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:59:51 -08:00
|
|
|
module.exports = {
|
|
|
|
utilEndpoints,
|
|
|
|
getGitVersion,
|
|
|
|
convertToCSV,
|
|
|
|
convertToJSON,
|
|
|
|
convertToJSONL,
|
|
|
|
};
|