Files
gh-christianlouis-dmarq/backend/app/api/api_v1/endpoints/stats.py
T
copilot-swe-agent[bot] 6ae017b142 Fix code formatting and linting issues
- Auto-format all Python files with black and isort
- Remove unused imports with autoflake
- Fix flake8 issues (missing newlines, blank lines, etc.)
- Fix nonlocal/global scope issues in main.py
- Fix security.py import order (E402)
- Remove f-string without placeholders
- Add nosec comment for intentional exception handling
- Fix test imports to match refactored DMARCParser API

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
2026-02-09 12:08:51 +00:00

78 lines
2.5 KiB
Python

from typing import Any, Dict
from app.core.database import get_db
from app.utils.stats_summarizer import StatsSummarizer
from fastapi import APIRouter, Depends, Path, Query
from sqlalchemy.orm import Session
router = APIRouter()
@router.get("/dashboard")
async def get_dashboard_statistics(
db: Session = Depends(get_db),
force_refresh: bool = Query(False, title="Force refresh of statistics"),
period_days: int = Query(30, title="Period in days for time-based statistics"),
) -> Dict[str, Any]:
"""
Get optimized statistics for the dashboard using cached data when possible.
This endpoint provides efficient access to statistics for large datasets.
Args:
force_refresh: If True, invalidate cache and recalculate statistics
period_days: Period in days for time-based statistics (default: 30)
Returns:
Dictionary with dashboard statistics
"""
# Initialize statistics summarizer
stats_summarizer = StatsSummarizer()
# If force refresh, invalidate cache
if force_refresh:
stats_summarizer.invalidate_cache()
# Get statistics (from cache or calculate if needed)
stats = stats_summarizer.calculate_summary_statistics(db)
# Add version and timestamp
stats["api_version"] = "1.0"
stats["period_days"] = period_days
return stats
@router.get("/domain/{domain_id}")
async def get_domain_statistics(
domain_id: str = Path(..., title="The domain ID or name"),
db: Session = Depends(get_db),
force_refresh: bool = Query(False, title="Force refresh of statistics"),
period_days: int = Query(30, title="Period in days for time-based statistics"),
) -> Dict[str, Any]:
"""
Get optimized statistics for a specific domain using cached data when possible.
Args:
domain_id: The domain ID or name
force_refresh: If True, invalidate cache and recalculate statistics
period_days: Period in days for time-based statistics (default: 30)
Returns:
Dictionary with domain statistics
"""
# Initialize statistics summarizer
stats_summarizer = StatsSummarizer()
# If force refresh, invalidate domain cache
if force_refresh:
stats_summarizer.invalidate_cache(domain_id)
# Get domain statistics (from cache or calculate if needed)
stats = stats_summarizer.calculate_summary_statistics(db, domain_id)
# Add version and timestamp
stats["api_version"] = "1.0"
stats["period_days"] = period_days
return stats