diff --git a/packages/server/src/controllers/chat-messages/index.ts b/packages/server/src/controllers/chat-messages/index.ts index 0e914a474c7..7f380f47d2c 100644 --- a/packages/server/src/controllers/chat-messages/index.ts +++ b/packages/server/src/controllers/chat-messages/index.ts @@ -2,9 +2,9 @@ import { Request, Response, NextFunction } from 'express' import { ChatMessageRatingType, ChatType, IReactFlowObject } from '../../Interface' import chatflowsService from '../../services/chatflows' import chatMessagesService from '../../services/chat-messages' -import { aMonthAgo, clearSessionMemory, setDateToStartOrEndOfDay } from '../../utils' +import { aMonthAgo, clearSessionMemory } from '../../utils' import { getRunningExpressApp } from '../../utils/getRunningExpressApp' -import { Between, FindOptionsWhere } from 'typeorm' +import { Between, DeleteResult, FindOptionsWhere } from 'typeorm' import { ChatMessage } from '../../database/entities/ChatMessage' import { InternalFlowiseError } from '../../errors/internalFlowiseError' import { StatusCodes } from 'http-status-codes' @@ -167,21 +167,21 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc if (!chatId) { const isFeedback = feedbackTypeFilters?.length ? true : false const hardDelete = req.query?.hardDelete as boolean | undefined - const messages = await utilGetChatMessage( + const messages = await utilGetChatMessage({ chatflowid, - _chatType as ChatType | undefined, - undefined, - undefined, - undefined, - undefined, + chatType: _chatType as ChatType | undefined, startDate, endDate, - undefined, - isFeedback, - feedbackTypeFilters - ) + feedback: isFeedback, + feedbackTypes: feedbackTypeFilters + }) const messageIds = messages.map((message) => message.id) + if (messages.length === 0) { + const result: DeleteResult = { raw: [], affected: 0 } + return res.json(result) + } + // Categorize by chatId_memoryType_sessionId const chatIdMap = new Map() messages.forEach((message) => { @@ -238,8 +238,8 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc if (sessionId) deleteOptions.sessionId = sessionId if (_chatType) deleteOptions.chatType = _chatType if (startDate && endDate) { - const fromDate = setDateToStartOrEndOfDay(startDate, 'start') - const toDate = setDateToStartOrEndOfDay(endDate, 'end') + const fromDate = new Date(startDate) + const toDate = new Date(endDate) deleteOptions.createdDate = Between(fromDate ?? aMonthAgo(), toDate ?? new Date()) } const apiResponse = await chatMessagesService.removeAllChatMessages(chatId, chatflowid, deleteOptions) diff --git a/packages/server/src/services/chat-messages/index.ts b/packages/server/src/services/chat-messages/index.ts index 621b37d1a72..858d0a77ec8 100644 --- a/packages/server/src/services/chat-messages/index.ts +++ b/packages/server/src/services/chat-messages/index.ts @@ -39,9 +39,9 @@ const getAllChatMessages = async ( feedbackTypes?: ChatMessageRatingType[] ): Promise => { try { - const dbResponse = await utilGetChatMessage( - chatflowId, - chatTypeFilter, + const dbResponse = await utilGetChatMessage({ + chatflowid: chatflowId, + chatType: chatTypeFilter, sortOrder, chatId, memoryType, @@ -51,7 +51,7 @@ const getAllChatMessages = async ( messageId, feedback, feedbackTypes - ) + }) return dbResponse } catch (error) { throw new InternalFlowiseError( @@ -76,9 +76,9 @@ const getAllInternalChatMessages = async ( feedbackTypes?: ChatMessageRatingType[] ): Promise => { try { - const dbResponse = await utilGetChatMessage( - chatflowId, - chatTypeFilter, + const dbResponse = await utilGetChatMessage({ + chatflowid: chatflowId, + chatType: chatTypeFilter, sortOrder, chatId, memoryType, @@ -88,7 +88,7 @@ const getAllInternalChatMessages = async ( messageId, feedback, feedbackTypes - ) + }) return dbResponse } catch (error) { throw new InternalFlowiseError( diff --git a/packages/server/src/services/stats/index.ts b/packages/server/src/services/stats/index.ts index c4c0e52887c..7c1f60fc9eb 100644 --- a/packages/server/src/services/stats/index.ts +++ b/packages/server/src/services/stats/index.ts @@ -17,19 +17,15 @@ const getChatflowStats = async ( feedbackTypes?: ChatMessageRatingType[] ): Promise => { try { - const chatmessages = (await utilGetChatMessage( + const chatmessages = (await utilGetChatMessage({ chatflowid, - chatTypeFilter, - undefined, - undefined, - undefined, - undefined, + chatType: chatTypeFilter, startDate, endDate, messageId, feedback, feedbackTypes - )) as Array + })) as Array const totalMessages = chatmessages.length const totalFeedback = chatmessages.filter((message) => message?.feedback).length const positiveFeedback = chatmessages.filter((message) => message?.feedback?.rating === 'THUMBS_UP').length diff --git a/packages/server/src/services/upsert-history/index.ts b/packages/server/src/services/upsert-history/index.ts index 72e484b1cb1..f606a7a6571 100644 --- a/packages/server/src/services/upsert-history/index.ts +++ b/packages/server/src/services/upsert-history/index.ts @@ -1,4 +1,4 @@ -import { MoreThanOrEqual, LessThanOrEqual } from 'typeorm' +import { MoreThanOrEqual, LessThanOrEqual, Between } from 'typeorm' import { StatusCodes } from 'http-status-codes' import { getRunningExpressApp } from '../../utils/getRunningExpressApp' import { UpsertHistory } from '../../database/entities/UpsertHistory' @@ -14,26 +14,20 @@ const getAllUpsertHistory = async ( try { const appServer = getRunningExpressApp() - const setDateToStartOrEndOfDay = (dateTimeStr: string, setHours: 'start' | 'end') => { - const date = new Date(dateTimeStr) - if (isNaN(date.getTime())) { - return undefined + let createdDateQuery + if (startDate || endDate) { + if (startDate && endDate) { + createdDateQuery = Between(new Date(startDate), new Date(endDate)) + } else if (startDate) { + createdDateQuery = MoreThanOrEqual(new Date(startDate)) + } else if (endDate) { + createdDateQuery = LessThanOrEqual(new Date(endDate)) } - setHours === 'start' ? date.setHours(0, 0, 0, 0) : date.setHours(23, 59, 59, 999) - return date } - - let fromDate - if (startDate) fromDate = setDateToStartOrEndOfDay(startDate, 'start') - - let toDate - if (endDate) toDate = setDateToStartOrEndOfDay(endDate, 'end') - let upsertHistory = await appServer.AppDataSource.getRepository(UpsertHistory).find({ where: { chatflowid, - ...(fromDate && { date: MoreThanOrEqual(fromDate) }), - ...(toDate && { date: LessThanOrEqual(toDate) }) + date: createdDateQuery }, order: { date: sortOrder === 'DESC' ? 'DESC' : 'ASC' diff --git a/packages/server/src/utils/getChatMessage.ts b/packages/server/src/utils/getChatMessage.ts index 7ffde0eb8cf..3a35b486e96 100644 --- a/packages/server/src/utils/getChatMessage.ts +++ b/packages/server/src/utils/getChatMessage.ts @@ -1,9 +1,9 @@ -import { MoreThanOrEqual, LessThanOrEqual } from 'typeorm' +import { MoreThanOrEqual, LessThanOrEqual, Between } from 'typeorm' import { ChatMessageRatingType, ChatType } from '../Interface' import { ChatMessage } from '../database/entities/ChatMessage' import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback' import { getRunningExpressApp } from '../utils/getRunningExpressApp' -import { aMonthAgo, setDateToStartOrEndOfDay } from '.' +import { aMonthAgo } from '.' /** * Method that get chat messages. @@ -18,26 +18,34 @@ import { aMonthAgo, setDateToStartOrEndOfDay } from '.' * @param {boolean} feedback * @param {ChatMessageRatingType[]} feedbackTypes */ -export const utilGetChatMessage = async ( - chatflowid: string, - chatType: ChatType | undefined, - sortOrder: string = 'ASC', - chatId?: string, - memoryType?: string, - sessionId?: string, - startDate?: string, - endDate?: string, - messageId?: string, - feedback?: boolean, +interface GetChatMessageParams { + chatflowid: string + chatType?: ChatType + sortOrder?: string + chatId?: string + memoryType?: string + sessionId?: string + startDate?: string + endDate?: string + messageId?: string + feedback?: boolean feedbackTypes?: ChatMessageRatingType[] -): Promise => { - const appServer = getRunningExpressApp() - - let fromDate - if (startDate) fromDate = setDateToStartOrEndOfDay(startDate, 'start') +} - let toDate - if (endDate) toDate = setDateToStartOrEndOfDay(endDate, 'end') +export const utilGetChatMessage = async ({ + chatflowid, + chatType, + sortOrder = 'ASC', + chatId, + memoryType, + sessionId, + startDate, + endDate, + messageId, + feedback, + feedbackTypes +}: GetChatMessageParams): Promise => { + const appServer = getRunningExpressApp() if (feedback) { const query = await appServer.AppDataSource.getRepository(ChatMessage).createQueryBuilder('chat_message') @@ -62,10 +70,13 @@ export const utilGetChatMessage = async ( } // set date range - query.andWhere('chat_message.createdDate BETWEEN :fromDate AND :toDate', { - fromDate: fromDate ?? aMonthAgo(), - toDate: toDate ?? new Date() - }) + if (startDate) { + query.andWhere('chat_message.createdDate >= :startDateTime', { startDateTime: startDate ? new Date(startDate) : aMonthAgo() }) + } + if (endDate) { + query.andWhere('chat_message.createdDate <= :endDateTime', { endDateTime: endDate ? new Date(endDate) : new Date() }) + } + // sort query.orderBy('chat_message.createdDate', sortOrder === 'DESC' ? 'DESC' : 'ASC') @@ -89,6 +100,17 @@ export const utilGetChatMessage = async ( return messages } + let createdDateQuery + if (startDate || endDate) { + if (startDate && endDate) { + createdDateQuery = Between(new Date(startDate), new Date(endDate)) + } else if (startDate) { + createdDateQuery = MoreThanOrEqual(new Date(startDate)) + } else if (endDate) { + createdDateQuery = LessThanOrEqual(new Date(endDate)) + } + } + return await appServer.AppDataSource.getRepository(ChatMessage).find({ where: { chatflowid, @@ -96,8 +118,7 @@ export const utilGetChatMessage = async ( chatId, memoryType: memoryType ?? undefined, sessionId: sessionId ?? undefined, - ...(fromDate && { createdDate: MoreThanOrEqual(fromDate) }), - ...(toDate && { createdDate: LessThanOrEqual(toDate) }), + createdDate: createdDateQuery, id: messageId ?? undefined }, order: { diff --git a/packages/server/src/utils/index.ts b/packages/server/src/utils/index.ts index bc0c4c35829..663214923ae 100644 --- a/packages/server/src/utils/index.ts +++ b/packages/server/src/utils/index.ts @@ -1745,15 +1745,6 @@ export const convertToValidFilename = (word: string) => { .toLowerCase() } -export const setDateToStartOrEndOfDay = (dateTimeStr: string, setHours: 'start' | 'end') => { - const date = new Date(dateTimeStr) - if (isNaN(date.getTime())) { - return undefined - } - setHours === 'start' ? date.setHours(0, 0, 0, 0) : date.setHours(23, 59, 59, 999) - return date -} - export const aMonthAgo = () => { const date = new Date() date.setMonth(new Date().getMonth() - 1) diff --git a/packages/ui/src/ui-component/dialog/ViewMessagesDialog.jsx b/packages/ui/src/ui-component/dialog/ViewMessagesDialog.jsx index 7daab109849..dc0dd42a1f7 100644 --- a/packages/ui/src/ui-component/dialog/ViewMessagesDialog.jsx +++ b/packages/ui/src/ui-component/dialog/ViewMessagesDialog.jsx @@ -167,28 +167,32 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => { let storagePath = '' const onStartDateSelected = (date) => { - setStartDate(date) + const updatedDate = new Date(date) + updatedDate.setHours(0, 0, 0, 0) + setStartDate(updatedDate) getChatmessageApi.request(dialogProps.chatflow.id, { - startDate: date, + startDate: updatedDate, endDate: endDate, chatType: chatTypeFilter.length ? chatTypeFilter : undefined }) getStatsApi.request(dialogProps.chatflow.id, { - startDate: date, + startDate: updatedDate, endDate: endDate, chatType: chatTypeFilter.length ? chatTypeFilter : undefined }) } const onEndDateSelected = (date) => { - setEndDate(date) + const updatedDate = new Date(date) + updatedDate.setHours(23, 59, 59, 999) + setEndDate(updatedDate) getChatmessageApi.request(dialogProps.chatflow.id, { - endDate: date, + endDate: updatedDate, startDate: startDate, chatType: chatTypeFilter.length ? chatTypeFilter : undefined }) getStatsApi.request(dialogProps.chatflow.id, { - endDate: date, + endDate: updatedDate, startDate: startDate, chatType: chatTypeFilter.length ? chatTypeFilter : undefined }) diff --git a/packages/ui/src/views/vectorstore/UpsertHistoryDialog.jsx b/packages/ui/src/views/vectorstore/UpsertHistoryDialog.jsx index d8ddc58ff7e..b2cf03c9d82 100644 --- a/packages/ui/src/views/vectorstore/UpsertHistoryDialog.jsx +++ b/packages/ui/src/views/vectorstore/UpsertHistoryDialog.jsx @@ -211,17 +211,21 @@ const UpsertHistoryDialog = ({ show, dialogProps, onCancel }) => { } const onStartDateSelected = (date) => { - setStartDate(date) + const updatedDate = new Date(date) + updatedDate.setHours(0, 0, 0, 0) + setStartDate(updatedDate) getUpsertHistoryApi.request(dialogProps.chatflow.id, { - startDate: date, + startDate: updatedDate, endDate: endDate }) } const onEndDateSelected = (date) => { - setEndDate(date) + const updatedDate = new Date(date) + updatedDate.setHours(23, 59, 59, 999) + setEndDate(updatedDate) getUpsertHistoryApi.request(dialogProps.chatflow.id, { - endDate: date, + endDate: updatedDate, startDate: startDate }) }