Muhammad Abdur Rahman Saad
fix pylint errors
72f4cb5
raw
history blame
3.02 kB
"""This module defines the /summary route for the Flask application."""
import importlib
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from controllers.summary import get_summary_data # pylint: disable=import-error
router = APIRouter(prefix="/summary", tags=["summary"])
@router.get('')
async def get_summary() -> JSONResponse:
"""
Generate a summary dashboard with content flow and entity analysis data.
This endpoint provides a complete summary overview, including:
- Content Flow Tracker: Article counts by source and category
- Entity Analysis: Top entities by type with mentions
All data is returned together, divided into three time periods: today, week, and month.
Returns:
JSONResponse: A JSON response containing the complete summary dashboard data:
{
"content": {"today": {...}, "week": {...}, "month": {...}},
"entity": {"today": {...}, "week": {...}, "month": {...}}
}
"""
try:
summary_data = get_summary_data()
return JSONResponse(content=summary_data)
except Exception as e: #pylint: disable=broad-except
return JSONResponse(content={"error": str(e)}, status_code=500)
@router.get("/{module}/{chart_id}")
def get_summary_chart(module: str, chart_id: str) -> JSONResponse:
"""
Handles GET requests to the summary route with a specific module and chart ID.
Args:
module (str): The module identifier (content or entity).
chart_id (str): The chart identifier (today, weekly, monthly).
Returns:
tuple: The result of the chart's process function and HTTP status code 200.
Raises:
ImportError: If the specified chart module cannot be imported.
AttributeError: If the imported module does not have a 'process' function.
Endpoint:
GET /<module>/<chart_id>
"""
try:
result = importlib.import_module(f"controllers.summary.{module}.{chart_id}").process()
return JSONResponse(content=result)
except ImportError as e:
return JSONResponse(content={"error": str(e)}, status_code=404)
except AttributeError as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@router.get("/{module}")
async def get_summary_module(module: str) -> JSONResponse:
"""
Handles GET requests to the summary route for a specific module.
Triggers the process for each chart under this module concurrently.
Args:
module (str): The module identifier (content or entity).
Returns:
dict: {"module": module, "charts": [chart1, chart2, ...]}
"""
try:
result = importlib.import_module("controllers.summary").process(module)
return JSONResponse(content=result)
except ImportError as e:
return JSONResponse(content={"error": str(e)}, status_code=404)
except Exception as e: #pylint: disable=broad-except
return JSONResponse(content={"error": str(e)}, status_code=500)