This commit is contained in:
Timothy Jaeryang Baek 2025-12-27 01:06:21 +04:00
parent 3b0d25ad2b
commit ac0ae2ae20

View file

@ -1,7 +1,7 @@
import json import json
import logging import logging
from typing import Optional from typing import Optional
import asyncio
from open_webui.utils.misc import get_message_list from open_webui.utils.misc import get_message_list
from open_webui.socket.main import get_event_emitter from open_webui.socket.main import get_event_emitter
@ -209,43 +209,12 @@ class ChatStatsExportList(BaseModel):
page: int page: int
@router.get("/stats/export", response_model=ChatStatsExportList) def calculate_chat_stats(user_id, skip=0, limit=10, filter=None):
def export_chat_stats( if filter is None:
request: Request, filter = {}
chat_id: Optional[str] = None,
start_time: Optional[int] = None,
end_time: Optional[int] = None,
page: Optional[int] = 1,
user=Depends(get_verified_user),
):
# Check if the user has permission to share/export chats
if (user.role != "admin") and (
not request.app.state.config.ENABLE_COMMUNITY_SHARING
):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
try:
limit = 10 # Fixed limit for export
skip = (page - 1) * limit
# Fetch chats with date filtering
filter = {"order_by": "created_at", "direction": "asc"}
if chat_id:
chat = Chats.get_chat_by_id(chat_id)
if chat:
filter["start_time"] = chat.created_at
if start_time:
filter["start_time"] = start_time
if end_time:
filter["end_time"] = end_time
result = Chats.get_chats_by_user_id( result = Chats.get_chats_by_user_id(
user.id, user_id,
skip=skip, skip=skip,
limit=limit, limit=limit,
filter=filter, filter=filter,
@ -270,7 +239,9 @@ def export_chat_stats(
if isinstance(content, str): if isinstance(content, str):
content_length = len(content) content_length = len(content)
else: else:
content_length = 0 # Handle cases where content might be None or not string content_length = (
0 # Handle cases where content might be None or not string
)
# Extract rating safely # Extract rating safely
rating = message.get("annotation", {}).get("rating") rating = message.get("annotation", {}).get("rating")
@ -370,9 +341,7 @@ def export_chat_stats(
# Construct Chat Body # Construct Chat Body
chat_body = ChatBody( chat_body = ChatBody(
history=ChatHistoryStats( history=ChatHistoryStats(messages=export_messages, currentId=message_id)
messages=export_messages, currentId=message_id
)
) )
chat_stat = ChatStatsExport( chat_stat = ChatStatsExport(
@ -390,10 +359,50 @@ def export_chat_stats(
log.debug(f"Error exporting stats for chat {chat.id}: {e}") log.debug(f"Error exporting stats for chat {chat.id}: {e}")
continue continue
return ChatStatsExportList( return chat_stats_export_list, result.total
items=chat_stats_export_list, total=result.total, page=page
@router.get("/stats/export", response_model=ChatStatsExportList)
async def export_chat_stats(
request: Request,
chat_id: Optional[str] = None,
start_time: Optional[int] = None,
end_time: Optional[int] = None,
page: Optional[int] = 1,
user=Depends(get_verified_user),
):
# Check if the user has permission to share/export chats
if (user.role != "admin") and (
not request.app.state.config.ENABLE_COMMUNITY_SHARING
):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
) )
try:
limit = 10 # Fixed limit for export
skip = (page - 1) * limit
# Fetch chats with date filtering
filter = {"order_by": "created_at", "direction": "asc"}
if chat_id:
chat = Chats.get_chat_by_id(chat_id)
if chat:
filter["start_time"] = chat.created_at
if start_time:
filter["start_time"] = start_time
if end_time:
filter["end_time"] = end_time
chat_stats_export_list, total = await asyncio.to_thread(
calculate_chat_stats, user.id, skip, limit, filter
)
return ChatStatsExportList(items=chat_stats_export_list, total=total, page=page)
except Exception as e: except Exception as e:
log.debug(f"Error exporting chat stats: {e}") log.debug(f"Error exporting chat stats: {e}")
raise HTTPException( raise HTTPException(