mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
Update stats API - date and source filters apply to stats
This commit is contained in:
@@ -723,19 +723,41 @@ export class App {
|
|||||||
// get stats for showing in chatflow
|
// get stats for showing in chatflow
|
||||||
this.app.get('/api/v1/stats/:id', async (req: Request, res: Response) => {
|
this.app.get('/api/v1/stats/:id', async (req: Request, res: Response) => {
|
||||||
const chatflowid = req.params.id
|
const chatflowid = req.params.id
|
||||||
const chatTypeFilter = chatType.EXTERNAL
|
let chatTypeFilter = req.query?.chatType as chatType | undefined
|
||||||
|
const startDate = req.query?.startDate as string | undefined
|
||||||
|
const endDate = req.query?.endDate as string | undefined
|
||||||
|
|
||||||
const totalMessages = await this.AppDataSource.getRepository(ChatMessage).count({
|
if (chatTypeFilter) {
|
||||||
where: {
|
try {
|
||||||
chatflowid,
|
const chatTypeFilterArray = JSON.parse(chatTypeFilter)
|
||||||
chatType: chatTypeFilter
|
if (chatTypeFilterArray.includes(chatType.EXTERNAL) && chatTypeFilterArray.includes(chatType.INTERNAL)) {
|
||||||
|
chatTypeFilter = undefined
|
||||||
|
} else if (chatTypeFilterArray.includes(chatType.EXTERNAL)) {
|
||||||
|
chatTypeFilter = chatType.EXTERNAL
|
||||||
|
} else if (chatTypeFilterArray.includes(chatType.INTERNAL)) {
|
||||||
|
chatTypeFilter = chatType.INTERNAL
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return res.status(500).send(e)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
const chatMessageFeedbackRepo = this.AppDataSource.getRepository(ChatMessageFeedback)
|
const chatmessages = (await this.getChatMessage(
|
||||||
|
chatflowid,
|
||||||
|
chatTypeFilter,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
'',
|
||||||
|
true
|
||||||
|
)) as Array<ChatMessage & { feedback?: ChatMessageFeedback }>
|
||||||
|
const totalMessages = chatmessages.length
|
||||||
|
|
||||||
const totalFeedback = await chatMessageFeedbackRepo.count({ where: { chatflowid } })
|
const totalFeedback = chatmessages.filter((message) => !message?.feedback).length
|
||||||
const positiveFeedback = await chatMessageFeedbackRepo.countBy({ chatflowid, rating: ChatMessageRatingType.THUMBS_UP })
|
const positiveFeedback = chatmessages.filter((message) => message?.feedback?.rating === 'THUMBS_UP').length
|
||||||
|
|
||||||
const results = {
|
const results = {
|
||||||
totalMessages,
|
totalMessages,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import client from './client'
|
import client from './client'
|
||||||
|
|
||||||
const getStatsFromChatflow = (id) => client.get(`/stats/${id}`)
|
const getStatsFromChatflow = (id, params) => client.get(`/stats/${id}`, { params: { ...params } })
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getStatsFromChatflow
|
getStatsFromChatflow
|
||||||
|
|||||||
@@ -115,6 +115,11 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
|
|||||||
endDate: endDate,
|
endDate: endDate,
|
||||||
chatType: chatTypeFilter.length ? chatTypeFilter : undefined
|
chatType: chatTypeFilter.length ? chatTypeFilter : undefined
|
||||||
})
|
})
|
||||||
|
getStatsApi.request(dialogProps.chatflow.id, {
|
||||||
|
startDate: date,
|
||||||
|
endDate: endDate,
|
||||||
|
chatType: chatTypeFilter.length ? chatTypeFilter : undefined
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const onEndDateSelected = (date) => {
|
const onEndDateSelected = (date) => {
|
||||||
@@ -124,6 +129,11 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
|
|||||||
startDate: startDate,
|
startDate: startDate,
|
||||||
chatType: chatTypeFilter.length ? chatTypeFilter : undefined
|
chatType: chatTypeFilter.length ? chatTypeFilter : undefined
|
||||||
})
|
})
|
||||||
|
getStatsApi.request(dialogProps.chatflow.id, {
|
||||||
|
endDate: date,
|
||||||
|
startDate: startDate,
|
||||||
|
chatType: chatTypeFilter.length ? chatTypeFilter : undefined
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const onChatTypeSelected = (chatTypes) => {
|
const onChatTypeSelected = (chatTypes) => {
|
||||||
@@ -133,6 +143,11 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
|
|||||||
startDate: startDate,
|
startDate: startDate,
|
||||||
endDate: endDate
|
endDate: endDate
|
||||||
})
|
})
|
||||||
|
getStatsApi.request(dialogProps.chatflow.id, {
|
||||||
|
chatType: chatTypes.length ? chatTypes : undefined,
|
||||||
|
startDate: startDate,
|
||||||
|
endDate: endDate
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const exportMessages = async () => {
|
const exportMessages = async () => {
|
||||||
@@ -432,6 +447,7 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
|
|||||||
setSelectedMessageIndex(0)
|
setSelectedMessageIndex(0)
|
||||||
setStartDate(new Date().setMonth(new Date().getMonth() - 1))
|
setStartDate(new Date().setMonth(new Date().getMonth() - 1))
|
||||||
setEndDate(new Date())
|
setEndDate(new Date())
|
||||||
|
setStats([])
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
@@ -463,23 +479,6 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
|
|||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<>
|
<>
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
display: 'grid',
|
|
||||||
gridTemplateColumns: 'repeat(3, minmax(0, 1fr))',
|
|
||||||
gap: 10,
|
|
||||||
marginBottom: 16,
|
|
||||||
marginLeft: 8,
|
|
||||||
marginRight: 8
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<StatsCard title='Total Messages (API/Embed)' stat={`${stats.totalMessages}`} />
|
|
||||||
<StatsCard title='Total Feedback Received' stat={`${stats.totalFeedback}`} />
|
|
||||||
<StatsCard
|
|
||||||
title='Positive Feedback'
|
|
||||||
stat={`${((stats.positiveFeedback / stats.totalFeedback) * 100 || 0).toFixed(2)}%`}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
@@ -536,6 +535,23 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
|
|||||||
</div>
|
</div>
|
||||||
<div style={{ flex: 1 }}></div>
|
<div style={{ flex: 1 }}></div>
|
||||||
</div>
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'grid',
|
||||||
|
gridTemplateColumns: 'repeat(3, minmax(0, 1fr))',
|
||||||
|
gap: 10,
|
||||||
|
marginBottom: 16,
|
||||||
|
marginLeft: 8,
|
||||||
|
marginRight: 8
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<StatsCard title='Total Messages' stat={`${stats.totalMessages}`} />
|
||||||
|
<StatsCard title='Total Feedback Received' stat={`${stats.totalFeedback}`} />
|
||||||
|
<StatsCard
|
||||||
|
title='Positive Feedback'
|
||||||
|
stat={`${((stats.positiveFeedback / stats.totalFeedback) * 100 || 0).toFixed(2)}%`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div style={{ display: 'flex', flexDirection: 'row' }}>
|
<div style={{ display: 'flex', flexDirection: 'row' }}>
|
||||||
{chatlogs && chatlogs.length == 0 && (
|
{chatlogs && chatlogs.length == 0 && (
|
||||||
<Stack sx={{ alignItems: 'center', justifyContent: 'center', width: '100%' }} flexDirection='column'>
|
<Stack sx={{ alignItems: 'center', justifyContent: 'center', width: '100%' }} flexDirection='column'>
|
||||||
|
|||||||
Reference in New Issue
Block a user