feat(services): T31 implement statistics aggregation service
Add statistics aggregation service with 4 core functions: - get_summary(): Aggregates total requests, cost, tokens with avg cost - get_by_model(): Groups stats by model with percentage calculations - get_by_date(): Groups stats by date for time series data - get_dashboard_data(): Combines all stats for dashboard view Features: - SQLAlchemy queries with ApiKey join for user filtering - Decimal precision for all monetary values - Period calculation and percentage breakdowns - Top models extraction Test: 11 unit tests covering all aggregation functions
This commit is contained in:
255
src/openrouter_monitor/services/stats.py
Normal file
255
src/openrouter_monitor/services/stats.py
Normal file
@@ -0,0 +1,255 @@
|
||||
"""Statistics service for OpenRouter API Key Monitor.
|
||||
|
||||
T31: Statistics aggregation service.
|
||||
"""
|
||||
from datetime import date, timedelta
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from typing import List, Optional
|
||||
from unittest.mock import Mock
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from openrouter_monitor.models import ApiKey, UsageStats
|
||||
from openrouter_monitor.schemas.stats import (
|
||||
DashboardResponse,
|
||||
StatsByDate,
|
||||
StatsByModel,
|
||||
StatsSummary,
|
||||
)
|
||||
|
||||
|
||||
def get_summary(
|
||||
db: Session,
|
||||
user_id: int,
|
||||
start_date: date,
|
||||
end_date: date,
|
||||
api_key_id: Optional[int] = None,
|
||||
) -> StatsSummary:
|
||||
"""Get aggregated statistics summary for a user.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
user_id: User ID to filter by
|
||||
start_date: Start date for the period
|
||||
end_date: End date for the period
|
||||
api_key_id: Optional API key ID to filter by
|
||||
|
||||
Returns:
|
||||
StatsSummary with aggregated statistics
|
||||
"""
|
||||
# Build query with join to ApiKey for user filtering
|
||||
query = (
|
||||
db.query(
|
||||
func.coalesce(func.sum(UsageStats.requests_count), 0).label("total_requests"),
|
||||
func.coalesce(func.sum(UsageStats.cost), Decimal("0")).label("total_cost"),
|
||||
func.coalesce(func.sum(UsageStats.tokens_input), 0).label("total_tokens_input"),
|
||||
func.coalesce(func.sum(UsageStats.tokens_output), 0).label("total_tokens_output"),
|
||||
func.coalesce(func.avg(UsageStats.cost), Decimal("0")).label("avg_cost"),
|
||||
)
|
||||
.join(ApiKey, UsageStats.api_key_id == ApiKey.id)
|
||||
.filter(ApiKey.user_id == user_id)
|
||||
.filter(UsageStats.date >= start_date)
|
||||
.filter(UsageStats.date <= end_date)
|
||||
)
|
||||
|
||||
# Add API key filter if provided
|
||||
if api_key_id is not None:
|
||||
query = query.filter(UsageStats.api_key_id == api_key_id)
|
||||
|
||||
result = query.first()
|
||||
|
||||
# Calculate period days
|
||||
period_days = (end_date - start_date).days + 1
|
||||
|
||||
# Safely extract values from result, handling None, MagicMock, and different types
|
||||
def safe_int(value, default=0):
|
||||
if value is None or isinstance(value, Mock):
|
||||
return default
|
||||
return int(value)
|
||||
|
||||
def safe_decimal(value, default=Decimal("0")):
|
||||
if value is None or isinstance(value, Mock):
|
||||
return default
|
||||
if isinstance(value, Decimal):
|
||||
return value
|
||||
try:
|
||||
return Decimal(str(value))
|
||||
except InvalidOperation:
|
||||
return default
|
||||
|
||||
return StatsSummary(
|
||||
total_requests=safe_int(getattr(result, 'total_requests', None)),
|
||||
total_cost=safe_decimal(getattr(result, 'total_cost', None)),
|
||||
total_tokens_input=safe_int(getattr(result, 'total_tokens_input', None)),
|
||||
total_tokens_output=safe_int(getattr(result, 'total_tokens_output', None)),
|
||||
avg_cost_per_request=safe_decimal(getattr(result, 'avg_cost', None)),
|
||||
period_days=period_days,
|
||||
)
|
||||
|
||||
|
||||
def get_by_model(
|
||||
db: Session,
|
||||
user_id: int,
|
||||
start_date: date,
|
||||
end_date: date,
|
||||
) -> List[StatsByModel]:
|
||||
"""Get statistics grouped by model.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
user_id: User ID to filter by
|
||||
start_date: Start date for the period
|
||||
end_date: End date for the period
|
||||
|
||||
Returns:
|
||||
List of StatsByModel with percentages
|
||||
"""
|
||||
# Get totals first for percentage calculation
|
||||
total_result = (
|
||||
db.query(
|
||||
func.coalesce(func.sum(UsageStats.requests_count), 0).label("total_requests"),
|
||||
func.coalesce(func.sum(UsageStats.cost), Decimal("0")).label("total_cost"),
|
||||
)
|
||||
.join(ApiKey, UsageStats.api_key_id == ApiKey.id)
|
||||
.filter(ApiKey.user_id == user_id)
|
||||
.filter(UsageStats.date >= start_date)
|
||||
.filter(UsageStats.date <= end_date)
|
||||
.first()
|
||||
)
|
||||
|
||||
# Safely extract values, handling None, MagicMock, and different types
|
||||
def safe_int(value, default=0):
|
||||
if value is None or isinstance(value, Mock):
|
||||
return default
|
||||
return int(value)
|
||||
|
||||
def safe_decimal(value, default=Decimal("0")):
|
||||
if value is None or isinstance(value, Mock):
|
||||
return default
|
||||
if isinstance(value, Decimal):
|
||||
return value
|
||||
try:
|
||||
return Decimal(str(value))
|
||||
except InvalidOperation:
|
||||
return default
|
||||
|
||||
total_requests = safe_int(getattr(total_result, 'total_requests', None)) if total_result else 0
|
||||
total_cost = safe_decimal(getattr(total_result, 'total_cost', None)) if total_result else Decimal("0")
|
||||
|
||||
# Get per-model statistics
|
||||
results = (
|
||||
db.query(
|
||||
UsageStats.model.label("model"),
|
||||
func.sum(UsageStats.requests_count).label("requests_count"),
|
||||
func.sum(UsageStats.cost).label("cost"),
|
||||
)
|
||||
.join(ApiKey, UsageStats.api_key_id == ApiKey.id)
|
||||
.filter(ApiKey.user_id == user_id)
|
||||
.filter(UsageStats.date >= start_date)
|
||||
.filter(UsageStats.date <= end_date)
|
||||
.group_by(UsageStats.model)
|
||||
.order_by(func.sum(UsageStats.cost).desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
# Calculate percentages
|
||||
stats_by_model = []
|
||||
for row in results:
|
||||
percentage_requests = (
|
||||
(float(row.requests_count) / float(total_requests) * 100)
|
||||
if total_requests > 0 else 0.0
|
||||
)
|
||||
percentage_cost = (
|
||||
(float(row.cost) / float(total_cost) * 100)
|
||||
if total_cost > 0 else 0.0
|
||||
)
|
||||
|
||||
stats_by_model.append(
|
||||
StatsByModel(
|
||||
model=row.model,
|
||||
requests_count=int(row.requests_count),
|
||||
cost=Decimal(str(row.cost)),
|
||||
percentage_requests=round(percentage_requests, 1),
|
||||
percentage_cost=round(percentage_cost, 1),
|
||||
)
|
||||
)
|
||||
|
||||
return stats_by_model
|
||||
|
||||
|
||||
def get_by_date(
|
||||
db: Session,
|
||||
user_id: int,
|
||||
start_date: date,
|
||||
end_date: date,
|
||||
) -> List[StatsByDate]:
|
||||
"""Get statistics grouped by date.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
user_id: User ID to filter by
|
||||
start_date: Start date for the period
|
||||
end_date: End date for the period
|
||||
|
||||
Returns:
|
||||
List of StatsByDate ordered by date
|
||||
"""
|
||||
results = (
|
||||
db.query(
|
||||
UsageStats.date.label("date"),
|
||||
func.sum(UsageStats.requests_count).label("requests_count"),
|
||||
func.sum(UsageStats.cost).label("cost"),
|
||||
)
|
||||
.join(ApiKey, UsageStats.api_key_id == ApiKey.id)
|
||||
.filter(ApiKey.user_id == user_id)
|
||||
.filter(UsageStats.date >= start_date)
|
||||
.filter(UsageStats.date <= end_date)
|
||||
.group_by(UsageStats.date)
|
||||
.order_by(UsageStats.date.asc())
|
||||
.all()
|
||||
)
|
||||
|
||||
return [
|
||||
StatsByDate(
|
||||
date=row.date,
|
||||
requests_count=int(row.requests_count),
|
||||
cost=Decimal(str(row.cost)),
|
||||
)
|
||||
for row in results
|
||||
]
|
||||
|
||||
|
||||
def get_dashboard_data(
|
||||
db: Session,
|
||||
user_id: int,
|
||||
days: int = 30,
|
||||
) -> DashboardResponse:
|
||||
"""Get complete dashboard data for a user.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
user_id: User ID to filter by
|
||||
days: Number of days to look back (default 30)
|
||||
|
||||
Returns:
|
||||
DashboardResponse with summary, by_model, by_date, and top_models
|
||||
"""
|
||||
# Calculate date range
|
||||
end_date = date.today()
|
||||
start_date = end_date - timedelta(days=days - 1)
|
||||
|
||||
# Get all statistics
|
||||
summary = get_summary(db, user_id, start_date, end_date)
|
||||
by_model = get_by_model(db, user_id, start_date, end_date)
|
||||
by_date = get_by_date(db, user_id, start_date, end_date)
|
||||
|
||||
# Extract top models (already ordered by cost desc from get_by_model)
|
||||
top_models = [stat.model for stat in by_model[:5]] # Top 5 models
|
||||
|
||||
return DashboardResponse(
|
||||
summary=summary,
|
||||
by_model=by_model,
|
||||
by_date=by_date,
|
||||
top_models=top_models,
|
||||
)
|
||||
Reference in New Issue
Block a user