Spaces:
Running
Running
Download src/api/dashboard.py from reputeradar/ioc-core: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/spaces/reputeradar/ioc-core/resolve/main/src/api/dashboard.py
- Command line
-
hf download hf://spaces/reputeradar/ioc-core/src/api/dashboard.py
-
curl -L -o dashboard.py https://huggingface.co/spaces/reputeradar/ioc-core/resolve/main/src/api/dashboard.py
2.71 kB
| """Dashboard API endpoints for analytics and statistics.""" | |
| from fastapi import APIRouter, Query | |
| from src.services.stats import stats_service | |
| router = APIRouter(prefix="/dashboard", tags=["Dashboard"]) | |
| async def get_dashboard_stats( | |
| days: int = Query(default=7, ge=1, le=365, description="Number of days to include") | |
| ): | |
| """ | |
| Get aggregated dashboard statistics. | |
| Returns total counts, breakdown by type/verdict, threat rate, and today's count. | |
| """ | |
| stats = stats_service.get_stats(days=days) | |
| return { | |
| "status": "ok", | |
| "enabled": stats_service.enabled, | |
| "days": days, | |
| **stats | |
| } | |
| async def get_dashboard_trend( | |
| days: int = Query(default=7, ge=1, le=365, description="Number of days to include") | |
| ): | |
| """ | |
| Get daily trend data for charts. | |
| Returns list of daily totals and threat counts. | |
| """ | |
| trend = stats_service.get_trend(days=days) | |
| return { | |
| "status": "ok", | |
| "enabled": stats_service.enabled, | |
| "days": days, | |
| "data": trend | |
| } | |
| async def get_recent_lookups( | |
| limit: int = Query(default=20, ge=1, le=100, description="Maximum records to return") | |
| ): | |
| """ | |
| Get recent IOC lookups. | |
| Returns latest lookups with IOC value, type, verdict, and timestamp. | |
| """ | |
| recent = stats_service.get_recent(limit=limit) | |
| return { | |
| "status": "ok", | |
| "enabled": stats_service.enabled, | |
| "data": recent | |
| } | |
| async def get_top_iocs( | |
| limit: int = Query(default=10, ge=1, le=50, description="Maximum records to return"), | |
| days: int = Query(default=30, ge=1, le=365, description="Number of days to include") | |
| ): | |
| """ | |
| Get most frequently searched IOCs. | |
| Returns top IOCs with search counts. | |
| """ | |
| top = stats_service.get_top_iocs(limit=limit, days=days) | |
| return { | |
| "status": "ok", | |
| "enabled": stats_service.enabled, | |
| "data": top | |
| } | |
| async def get_geo_stats( | |
| days: int = Query(default=30, ge=1, le=365, description="Number of days to include") | |
| ): | |
| """ | |
| Get geographic distribution of threats. | |
| Returns country codes with counts for world map visualization. | |
| """ | |
| geo = stats_service.get_geo_stats(days=days) | |
| return { | |
| "status": "ok", | |
| "enabled": stats_service.enabled, | |
| "data": geo | |
| } | |
| async def get_dashboard_status(): | |
| """ | |
| Get dashboard service diagnostics. | |
| Returns in-memory record count, pending retries, and Supabase connection state. | |
| """ | |
| return { | |
| "status": "ok", | |
| **stats_service.get_status() | |
| } | |