Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get/Delete ChatMessage based on startDateTime / endDateTime #3867

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/server/src/controllers/chat-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chatflowsService from '../../services/chatflows'
import chatMessagesService from '../../services/chat-messages'
import { aMonthAgo, clearSessionMemory, setDateToStartOrEndOfDay } 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'
Expand Down Expand Up @@ -71,6 +71,8 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
const messageId = req.query?.messageId as string | undefined
const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined
const startDateTime = req.query?.startDateTime as string | undefined
const endDateTime = req.query?.endDateTime as string | undefined
const feedback = req.query?.feedback as boolean | undefined
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
if (feedbackTypeFilters) {
Expand All @@ -91,6 +93,8 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
sessionId,
startDate,
endDate,
startDateTime,
endDateTime,
messageId,
feedback,
feedbackTypeFilters
Expand All @@ -111,6 +115,8 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
const messageId = req.query?.messageId as string | undefined
const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined
const startDateTime = req.query?.startDateTime as string | undefined
const endDateTime = req.query?.endDateTime as string | undefined
const feedback = req.query?.feedback as boolean | undefined
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
if (feedbackTypeFilters) {
Expand All @@ -125,6 +131,8 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
sessionId,
startDate,
endDate,
startDateTime,
endDateTime,
messageId,
feedback,
feedbackTypeFilters
Expand Down Expand Up @@ -158,6 +166,8 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
const _chatType = req.query?.chatType as string | undefined
const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined
const startDateTime = req.query?.startDateTime as string | undefined
const endDateTime = req.query?.endDateTime as string | undefined
const isClearFromViewMessageDialog = req.query?.isClearFromViewMessageDialog as string | undefined
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
if (feedbackTypeFilters) {
Expand All @@ -176,12 +186,19 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
undefined,
startDate,
endDate,
startDateTime,
endDateTime,
undefined,
isFeedback,
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<string, ChatMessage[]>()
messages.forEach((message) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/server/src/services/chat-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const getAllChatMessages = async (
sessionId?: string,
startDate?: string,
endDate?: string,
startDateTime?: string,
endDateTime?: string,
messageId?: string,
feedback?: boolean,
feedbackTypes?: ChatMessageRatingType[]
Expand All @@ -48,6 +50,8 @@ const getAllChatMessages = async (
sessionId,
startDate,
endDate,
startDateTime,
endDateTime,
messageId,
feedback,
feedbackTypes
Expand All @@ -71,6 +75,8 @@ const getAllInternalChatMessages = async (
sessionId?: string,
startDate?: string,
endDate?: string,
startDateTime?: string,
endDateTime?: string,
messageId?: string,
feedback?: boolean,
feedbackTypes?: ChatMessageRatingType[]
Expand All @@ -86,6 +92,8 @@ const getAllInternalChatMessages = async (
startDate,
endDate,
messageId,
startDateTime,
endDateTime,
feedback,
feedbackTypes
)
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/services/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const getChatflowStats = async (
undefined,
startDate,
endDate,
undefined,
undefined,
messageId,
feedback,
feedbackTypes
Expand Down
34 changes: 31 additions & 3 deletions packages/server/src/utils/getChatMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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'
Expand Down Expand Up @@ -27,6 +27,8 @@ export const utilGetChatMessage = async (
sessionId?: string,
startDate?: string,
endDate?: string,
startDateTime?: string,
endDateTime?: string,
messageId?: string,
feedback?: boolean,
feedbackTypes?: ChatMessageRatingType[]
Expand Down Expand Up @@ -66,6 +68,14 @@ export const utilGetChatMessage = async (
fromDate: fromDate ?? aMonthAgo(),
toDate: toDate ?? new Date()
})

if (startDateTime) {
query.andWhere('chat_message.createdDate >= :startDateTime', { startDateTime: new Date(startDateTime) })
}
if (endDateTime) {
query.andWhere('chat_message.createdDate <= :endDateTime', { endDateTime: new Date(endDateTime) })
}

// sort
query.orderBy('chat_message.createdDate', sortOrder === 'DESC' ? 'DESC' : 'ASC')

Expand All @@ -89,15 +99,33 @@ export const utilGetChatMessage = async (
return messages
}

let createdDateQuery
if (fromDate || toDate) {
if (fromDate && toDate) {
createdDateQuery = Between(fromDate, toDate)
} else if (fromDate) {
createdDateQuery = MoreThanOrEqual(fromDate)
} else if (toDate) {
createdDateQuery = LessThanOrEqual(toDate)
}
} else if (startDateTime || endDateTime) {
if (startDateTime && endDateTime) {
createdDateQuery = Between(new Date(startDateTime), new Date(endDateTime))
} else if (startDateTime) {
createdDateQuery = MoreThanOrEqual(new Date(startDateTime))
} else if (endDateTime) {
createdDateQuery = LessThanOrEqual(new Date(endDateTime))
}
}

return await appServer.AppDataSource.getRepository(ChatMessage).find({
where: {
chatflowid,
chatType,
chatId,
memoryType: memoryType ?? undefined,
sessionId: sessionId ?? undefined,
...(fromDate && { createdDate: MoreThanOrEqual(fromDate) }),
...(toDate && { createdDate: LessThanOrEqual(toDate) }),
createdDate: createdDateQuery,
id: messageId ?? undefined
},
order: {
Expand Down
Loading