-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding usage apis * change date to datetime * optimise query --------- Co-authored-by: dhirenmathur <dhiru17398@gmail.com>
- Loading branch information
1 parent
d95224f
commit 80b57aa
Showing
7 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from datetime import datetime | ||
from app.modules.usage.usage_service import UsageService | ||
from app.modules.usage.usage_schema import UsageResponse | ||
|
||
class UsageController: | ||
@staticmethod | ||
async def get_user_usage(start_date: datetime, end_date: datetime, user_id: str) -> UsageResponse: | ||
usage_data = await UsageService.get_usage_data(start_date, end_date, user_id) | ||
return usage_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from fastapi import APIRouter, Depends | ||
from datetime import datetime | ||
from app.modules.auth.auth_service import AuthService | ||
from app.modules.usage.usage_controller import UsageController | ||
from app.modules.usage.usage_schema import UsageResponse | ||
|
||
router = APIRouter() | ||
|
||
class UsageAPI: | ||
@staticmethod | ||
@router.get("/usage", response_model=UsageResponse) | ||
async def get_usage( | ||
start_date: datetime, | ||
end_date: datetime, | ||
user=Depends(AuthService.check_auth), | ||
): | ||
user_id = user["user_id"] | ||
return await UsageController.get_user_usage(start_date, end_date, user_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pydantic import BaseModel | ||
from typing import Dict | ||
|
||
class UsageResponse(BaseModel): | ||
total_human_messages: int | ||
agent_message_counts: Dict[str, int] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from datetime import datetime | ||
from fastapi import logger | ||
from sqlalchemy import func | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from app.core.database import SessionLocal | ||
from app.modules.conversations.message.message_model import Message, MessageType | ||
from app.modules.conversations.conversation.conversation_model import Conversation | ||
|
||
|
||
class UsageService: | ||
@staticmethod | ||
async def get_usage_data(start_date: datetime, end_date: datetime, user_id: str): | ||
try: | ||
with SessionLocal() as session: | ||
agent_query = ( | ||
session.query( | ||
func.unnest(Conversation.agent_ids).label('agent_id'), | ||
func.count(Message.id).label('message_count') | ||
) | ||
.join(Message, Message.conversation_id == Conversation.id) | ||
.filter( | ||
Conversation.user_id == user_id, | ||
Message.created_at.between(start_date, end_date), | ||
Message.type == MessageType.HUMAN, | ||
) | ||
.group_by(func.unnest(Conversation.agent_ids)) | ||
.all() | ||
) | ||
|
||
agent_message_counts = { | ||
agent_id: count | ||
for agent_id, count in agent_query | ||
} | ||
|
||
total_human_messages = sum(agent_message_counts.values()) | ||
|
||
return { | ||
"total_human_messages": total_human_messages, | ||
"agent_message_counts": agent_message_counts | ||
} | ||
|
||
except SQLAlchemyError as e: | ||
logger.error(f"Failed to fetch usage data: {e}") | ||
raise Exception("Failed to fetch usage data") from e |