From f4edd74e644e2c75f28c6eba4735d0247c816cef Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Wed, 18 Sep 2024 18:35:26 -0700 Subject: [PATCH] wip export all embed and workspace chats --- server/endpoints/system.js | 13 ++------ server/models/systemChats.js | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 server/models/systemChats.js diff --git a/server/endpoints/system.js b/server/endpoints/system.js index 9e57b3c35..6bc2775bd 100644 --- a/server/endpoints/system.js +++ b/server/endpoints/system.js @@ -53,6 +53,7 @@ const { const { SlashCommandPresets } = require("../models/slashCommandsPresets"); const { EncryptionManager } = require("../utils/EncryptionManager"); const { BrowserExtensionApiKey } = require("../models/browserExtensionApiKey"); +const { SystemChats } = require("../models/systemChats"); function systemEndpoints(app) { if (!app) return; @@ -970,16 +971,8 @@ function systemEndpoints(app) { async (request, response) => { try { const { offset = 0, limit = 20 } = reqBody(request); - const chats = await WorkspaceChats.whereWithData( - {}, - limit, - offset * limit, - { id: "desc" } - ); - const totalChats = await WorkspaceChats.count(); - const hasPages = totalChats > (offset + 1) * limit; - - response.status(200).json({ chats: chats, hasPages, totalChats }); + const { chats, hasPages, totalChats } = await SystemChats.getChats(offset, limit); + response.status(200).json({ chats, hasPages, totalChats }); } catch (e) { console.error(e); response.sendStatus(500).end(); diff --git a/server/models/systemChats.js b/server/models/systemChats.js new file mode 100644 index 000000000..2d59cdab2 --- /dev/null +++ b/server/models/systemChats.js @@ -0,0 +1,62 @@ +const prisma = require("../utils/prisma"); + +const SystemChats = { + getChats: async function (offset = 0, limit = 20) { + try { + const result = await prisma.$queryRaw` + SELECT * FROM ( + SELECT + id, + workspaceId, + prompt, + response, + createdAt, + 'workspace' as type, + (SELECT name FROM workspaces WHERE id = workspaceId) as workspace_name, + (SELECT username FROM users WHERE id = user_id) as sent_by + FROM workspace_chats + UNION ALL + SELECT + id, + embed_id as workspaceId, + prompt, + response, + createdAt, + 'embed' as type, + (SELECT name FROM workspaces WHERE id = (SELECT workspace_id FROM embed_configs WHERE id = embed_id)) as workspace_name, + NULL as sent_by + FROM embed_chats + ) AS combined_chats + ORDER BY createdAt DESC + LIMIT ${limit} + OFFSET ${offset * limit} + `; + + const totalChats = await prisma.$queryRaw` + SELECT + (SELECT COUNT(*) FROM workspace_chats) + + (SELECT COUNT(*) FROM embed_chats) as total + `; + + const hasPages = Number(totalChats[0].total) > (offset + 1) * limit; + + const serializedResult = result.map(chat => ({ + ...chat, + id: Number(chat.id), + workspaceId: Number(chat.workspaceId), + createdAt: chat.createdAt.toISOString(), + })); + + return { + chats: serializedResult, + hasPages, + totalChats: Number(totalChats[0].total), + }; + } catch (error) { + console.error(error.message); + return { chats: [], hasPages: false, totalChats: 0 }; + } + }, +}; + +module.exports = { SystemChats }; \ No newline at end of file