feat: add question insight analytics
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from sqlalchemy import create_engine
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.api.admin_agent_records import attention_list, retrieval_logs
|
||||
from app.api.admin_records import ai_logs, chat_detail, chat_messages
|
||||
from app.api.admin_records import ai_logs, chat_detail, chat_messages, question_insights
|
||||
from app.api.admin_users import list_users
|
||||
from app.models import Base
|
||||
from app.models.chat import ChatMessage, ChatSession
|
||||
@@ -106,3 +107,43 @@ def test_retrieval_and_attention_lists_are_paginated():
|
||||
attention = attention_list(priority="", statusValue="", page=3, pageSize=10, db=db, current_admin=object())["data"]
|
||||
assert retrieval["total"] == attention["total"] == 21
|
||||
assert len(retrieval["items"]) == len(attention["items"]) == 1
|
||||
|
||||
|
||||
def test_question_insights_clean_and_cluster_similar_user_questions():
|
||||
with _database() as db:
|
||||
user_a = User(id=1, phone="13800000000", name="学员A", daily_chat_limit=10)
|
||||
user_b = User(id=2, phone="13800000001", name="学员B", daily_chat_limit=10)
|
||||
session_a = ChatSession(id=1, user_id=1, title="问题", message_count=3)
|
||||
session_b = ChatSession(id=2, user_id=2, title="问题", message_count=2)
|
||||
now = datetime(2026, 7, 31, 10, 0, 0)
|
||||
db.add_all([user_a, user_b, session_a, session_b])
|
||||
db.add_all(
|
||||
[
|
||||
ChatMessage(id=1, session_id=1, user_id=1, role="user", content="老师你好,心光有哪些作业?", created_at=now),
|
||||
ChatMessage(id=2, session_id=1, user_id=1, role="assistant", content="回答", created_at=now + timedelta(seconds=1)),
|
||||
ChatMessage(id=3, session_id=2, user_id=2, role="user", content="请问心光都有什么功课", created_at=now + timedelta(minutes=1)),
|
||||
ChatMessage(id=4, session_id=2, user_id=2, role="user", content="谢谢老师", created_at=now + timedelta(minutes=2)),
|
||||
ChatMessage(id=5, session_id=1, user_id=1, role="user", content="1、回放在哪里看?\n2、上课链接在哪", created_at=now + timedelta(minutes=3)),
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
|
||||
response = question_insights(
|
||||
dateFrom=None,
|
||||
dateTo=None,
|
||||
minCount=2,
|
||||
maxMessages=100,
|
||||
page=1,
|
||||
pageSize=10,
|
||||
db=db,
|
||||
current_admin=object(),
|
||||
)
|
||||
|
||||
data = response["data"]
|
||||
assert data["summary"]["scannedMessages"] == 4
|
||||
assert data["summary"]["filteredMessages"] == 1
|
||||
assert data["summary"]["cleanedQuestions"] == 4
|
||||
assert data["total"] == 1
|
||||
assert data["items"][0]["count"] == 2
|
||||
assert "心光" in data["items"][0]["title"]
|
||||
assert data["items"][0]["userCount"] == 2
|
||||
|
||||
Reference in New Issue
Block a user