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

Feat/analytics 2 #577

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 31 additions & 27 deletions llm-server/models/repository/chat_history_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,37 +279,41 @@ def create_chat_histories(
return chat_histories


async def get_analytics(email: str):
async def get_analytics(chatbot_id: str):
with Session() as session:
chat_histories = []

# Step 1: Get chatbot_ids associated with the given email
chatbot_ids = session.query(Chatbot.id).filter(Chatbot.email == email).all()

if chatbot_ids:
chatbot_ids = [chatbot_id[0] for chatbot_id in chatbot_ids]

# Step 2: Get analytics data in a single query
analytics_data = (
session.query(
func.sum(func.cast(ChatHistory.api_called, Integer)).label("api_called_count"),
func.sum(func.cast(ChatHistory.knowledgebase_called, Integer)).label("knowledgebase_called_count"),
func.count().label("total"),
func.sum(func.cast(
~ChatHistory.api_called & ~ChatHistory.knowledgebase_called, Integer)).label("other_count")
)
.filter(ChatHistory.chatbot_id.in_(chatbot_ids))
.one()
# Step 2: Get analytics data in a single query
analytics_data = (
session.query(
func.sum(func.cast(ChatHistory.api_called, Integer)).label(
"api_called_count"
),
func.sum(func.cast(ChatHistory.knowledgebase_called, Integer)).label(
"knowledgebase_called_count"
),
func.count().label("total"),
func.sum(
func.cast(
~ChatHistory.api_called & ~ChatHistory.knowledgebase_called,
Integer,
)
).label("other_count"),
)
.filter(ChatHistory.chatbot_id == chatbot_id)
.one()
)

# Append the results to chat_histories
chat_histories.append(
{
"api_called_count": analytics_data.api_called_count or 0,
"knowledgebase_called_count": analytics_data.knowledgebase_called_count or 0,
"total": analytics_data.total or 0,
"other_count": analytics_data.other_count or 0,
}
)
# Append the results to chat_histories
chat_histories.append(
{
"api_called_count": int(analytics_data.api_called_count or 0),
"knowledgebase_called_count": int(
analytics_data.knowledgebase_called_count or 0
),
"total": int(analytics_data.total or 0),
"other_count": int(analytics_data.other_count or 0),
}
)

return chat_histories
14 changes: 3 additions & 11 deletions llm-server/routes/chat/chat_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,7 @@ async def handle_chat_send_common(


# curl -X POST http://localhost:5000/analytics -H "x_consumer_username: your_username"
@chat_workflow.route("/analytics", methods=["GET"])
async def get_analytics_by_email():
x_consumer_username = request.headers.get("x_consumer_username") or "guest"
if not x_consumer_username:
return Response(
response='{"error": "x_consumer_username is required"}',
status=400,
content_type="application/json",
)

result = await get_analytics(x_consumer_username)
@chat_workflow.route("/analytics/<bot_id>", methods=["GET"])
async def get_analytics_by_email(bot_id: str) -> Response:
result = await get_analytics(bot_id)
return jsonify(result)
Loading