added analytics

This commit is contained in:
belviskhoremk
2026-02-23 17:24:41 +00:00
parent 07c4c55072
commit 2ed998058e
5 changed files with 543 additions and 105 deletions

View File

@@ -129,32 +129,14 @@ async def get_chat_history(
]
# ── Analytics endpoint ────────────────────────────────────────────────────────
@router.get("/analytics/{chatbot_id}")
async def get_analytics(chatbot_id: str, user=Depends(get_current_user)):
supabase = get_supabase()
# Verify ownership
company = supabase.table("companies").select("id").eq("owner_id", user.id).execute()
if not company.data:
raise HTTPException(status_code=404, detail="Company not found")
chatbot = supabase.table("chatbots").select("id").eq("id", chatbot_id).eq("company_id",
company.data[0]["id"]).execute()
if not chatbot.data:
raise HTTPException(status_code=404, detail="Chatbot not found")
total_convs = supabase.table("conversations").select("id", count="exact").eq("chatbot_id", chatbot_id).execute()
total_msgs = supabase.table("messages").select("id", count="exact").execute()
return {
"chatbot_id": chatbot_id,
"total_conversations": total_convs.count or 0,
"total_messages": total_msgs.count or 0,
"average_rating": 0.0,
"conversations_last_30_days": total_convs.count or 0,
}
# ── OLD analytics endpoint REMOVED ───────────────────────────────────────────
# The /analytics/{chatbot_id} endpoint that was here has been replaced by
# the dedicated analytics router (app/routers/analytics.py) which provides:
# GET /api/v1/analytics/overview — All chatbots overview
# GET /api/v1/analytics/chatbot/{id} — Single chatbot detail
# The old endpoint conflicted with the new router because FastAPI matched
# "overview" as a chatbot_id UUID, causing a 500 error.
# ─────────────────────────────────────────────────────────────────────────────
# ── Helpers ───────────────────────────────────────────────────────────────────
@@ -188,12 +170,8 @@ def _get_or_create_conversation(
def _get_conversation_history(conversation_id: str, supabase) -> List[dict]:
"""
FIX: Changed from desc=True to desc=False (ascending/chronological order).
The conversation history MUST be in chronological order (oldest first)
Returns conversation history in chronological order (oldest first)
for the LLM to correctly understand the conversation flow.
Previously, messages were returned newest-first, which reversed the
conversation and confused the model.
"""
messages = supabase.table("messages").select("role, content") \
.eq("conversation_id", conversation_id) \