refac
This commit is contained in:
@@ -133,6 +133,12 @@ class ModelHistoryEntry(BaseModel):
|
||||
lost: int
|
||||
|
||||
|
||||
class ModelHistoryCounts(BaseModel):
|
||||
date: str
|
||||
won: int = 0
|
||||
lost: int = 0
|
||||
|
||||
|
||||
class ModelHistoryResponse(BaseModel):
|
||||
model_id: str
|
||||
history: list[ModelHistoryEntry]
|
||||
@@ -375,6 +381,45 @@ class FeedbackTable:
|
||||
|
||||
return result
|
||||
|
||||
async def get_model_feedback_counts_by_day(
|
||||
self,
|
||||
model_id: str,
|
||||
start_date: Optional[int] = None,
|
||||
db: Optional[AsyncSession] = None,
|
||||
) -> list[ModelHistoryCounts]:
|
||||
"""Get aggregated feedback counts per day for a model, preserving all matching days."""
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
|
||||
async with get_async_db_context(db) as db:
|
||||
stmt = select(Feedback.created_at, Feedback.data).filter(Feedback.data['model_id'].as_string() == model_id)
|
||||
if start_date is not None:
|
||||
stmt = stmt.filter(Feedback.created_at >= start_date)
|
||||
|
||||
result = await db.execute(stmt.order_by(Feedback.created_at.asc()))
|
||||
rows = result.all()
|
||||
|
||||
daily_counts = defaultdict(lambda: {'won': 0, 'lost': 0})
|
||||
|
||||
for created_at, data in rows:
|
||||
if not data:
|
||||
continue
|
||||
|
||||
rating_str = str(data.get('rating', ''))
|
||||
if rating_str not in ('1', '-1'):
|
||||
continue
|
||||
|
||||
date_str = datetime.fromtimestamp(created_at).strftime('%Y-%m-%d')
|
||||
if rating_str == '1':
|
||||
daily_counts[date_str]['won'] += 1
|
||||
else:
|
||||
daily_counts[date_str]['lost'] += 1
|
||||
|
||||
return [
|
||||
ModelHistoryCounts(date=date_str, won=counts['won'], lost=counts['lost'])
|
||||
for date_str, counts in sorted(daily_counts.items())
|
||||
]
|
||||
|
||||
async def get_feedbacks_by_type(self, type: str, db: Optional[AsyncSession] = None) -> list[FeedbackModel]:
|
||||
async with get_async_db_context(db) as db:
|
||||
result = await db.execute(select(Feedback).filter_by(type=type).order_by(Feedback.updated_at.desc()))
|
||||
|
||||
Reference in New Issue
Block a user