Improve backend
This commit is contained in:
@@ -167,4 +167,105 @@ class PerformanceMetricRepository(ForecastingBaseRepository):
|
||||
|
||||
async def cleanup_old_metrics(self, days_old: int = 180) -> int:
|
||||
"""Clean up old performance metrics"""
|
||||
return await self.cleanup_old_records(days_old=days_old)
|
||||
return await self.cleanup_old_records(days_old=days_old)
|
||||
|
||||
async def bulk_create_metrics(self, metrics: List[ModelPerformanceMetric]) -> int:
|
||||
"""
|
||||
Bulk insert performance metrics for validation
|
||||
|
||||
Args:
|
||||
metrics: List of ModelPerformanceMetric objects to insert
|
||||
|
||||
Returns:
|
||||
Number of metrics created
|
||||
"""
|
||||
try:
|
||||
if not metrics:
|
||||
return 0
|
||||
|
||||
self.session.add_all(metrics)
|
||||
await self.session.flush()
|
||||
|
||||
logger.info(
|
||||
"Bulk created performance metrics",
|
||||
count=len(metrics)
|
||||
)
|
||||
|
||||
return len(metrics)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Failed to bulk create performance metrics",
|
||||
count=len(metrics),
|
||||
error=str(e)
|
||||
)
|
||||
raise DatabaseError(f"Failed to bulk create metrics: {str(e)}")
|
||||
|
||||
async def get_metrics_by_date_range(
|
||||
self,
|
||||
tenant_id: str,
|
||||
start_date: datetime,
|
||||
end_date: datetime,
|
||||
inventory_product_id: Optional[str] = None
|
||||
) -> List[ModelPerformanceMetric]:
|
||||
"""
|
||||
Get performance metrics for a date range
|
||||
|
||||
Args:
|
||||
tenant_id: Tenant identifier
|
||||
start_date: Start of date range
|
||||
end_date: End of date range
|
||||
inventory_product_id: Optional product filter
|
||||
|
||||
Returns:
|
||||
List of performance metrics
|
||||
"""
|
||||
try:
|
||||
filters = {
|
||||
"tenant_id": tenant_id
|
||||
}
|
||||
|
||||
if inventory_product_id:
|
||||
filters["inventory_product_id"] = inventory_product_id
|
||||
|
||||
# Build custom query for date range
|
||||
query_text = """
|
||||
SELECT *
|
||||
FROM model_performance_metrics
|
||||
WHERE tenant_id = :tenant_id
|
||||
AND evaluation_date >= :start_date
|
||||
AND evaluation_date <= :end_date
|
||||
"""
|
||||
|
||||
params = {
|
||||
"tenant_id": tenant_id,
|
||||
"start_date": start_date,
|
||||
"end_date": end_date
|
||||
}
|
||||
|
||||
if inventory_product_id:
|
||||
query_text += " AND inventory_product_id = :inventory_product_id"
|
||||
params["inventory_product_id"] = inventory_product_id
|
||||
|
||||
query_text += " ORDER BY evaluation_date DESC"
|
||||
|
||||
result = await self.session.execute(text(query_text), params)
|
||||
rows = result.fetchall()
|
||||
|
||||
# Convert rows to ModelPerformanceMetric objects
|
||||
metrics = []
|
||||
for row in rows:
|
||||
metric = ModelPerformanceMetric()
|
||||
for column in row._mapping.keys():
|
||||
setattr(metric, column, row._mapping[column])
|
||||
metrics.append(metric)
|
||||
|
||||
return metrics
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Failed to get metrics by date range",
|
||||
tenant_id=tenant_id,
|
||||
error=str(e)
|
||||
)
|
||||
raise DatabaseError(f"Failed to get metrics: {str(e)}")
|
||||
Reference in New Issue
Block a user