Spaces:
Sleeping
Sleeping
Create analytics_agent.py
Browse filesanalytics_agent.py implementing live metrics on demand, end-of-day reports (scheduled), batch export (7/30/90 days), API/UI hooks, error logging/reporting, and config-driven specs. See functions: get_live_metrics, run_end_of_day_report, batch_export, expose_endpoint, get_errors. Needs analytics_utils.py. Update README for usage/testing.
- analytics_agent.py +72 -0
analytics_agent.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import json
|
| 3 |
+
import threading
|
| 4 |
+
from typing import Dict, Any, List, Optional
|
| 5 |
+
|
| 6 |
+
from .analytics_utils import fetch_metrics, export_metrics, log_error, save_report
|
| 7 |
+
|
| 8 |
+
class AnalyticsAgent:
|
| 9 |
+
def __init__(self, config_path: str = "configs/analytics_config.json"):
|
| 10 |
+
with open(config_path, "r") as f:
|
| 11 |
+
self.config = json.load(f)
|
| 12 |
+
self.metrics_cache = {}
|
| 13 |
+
self.error_log = []
|
| 14 |
+
self.lock = threading.Lock()
|
| 15 |
+
|
| 16 |
+
def get_live_metrics(self, spec: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
| 17 |
+
"""Aggregate real-time metrics on demand (API/button/query)."""
|
| 18 |
+
try:
|
| 19 |
+
metrics = fetch_metrics(spec or self.config["live_metrics_spec"])
|
| 20 |
+
with self.lock:
|
| 21 |
+
self.metrics_cache["live"] = metrics
|
| 22 |
+
return metrics
|
| 23 |
+
except Exception as e:
|
| 24 |
+
log_error(self.error_log, e)
|
| 25 |
+
return {"error": str(e)}
|
| 26 |
+
|
| 27 |
+
def run_end_of_day_report(self) -> Dict[str, Any]:
|
| 28 |
+
"""Aggregate metrics, save, notify/report at end of day (scheduled)."""
|
| 29 |
+
try:
|
| 30 |
+
report = fetch_metrics(self.config["end_of_day_spec"])
|
| 31 |
+
save_report(report, period="day")
|
| 32 |
+
with self.lock:
|
| 33 |
+
self.metrics_cache["end_of_day"] = report
|
| 34 |
+
return report
|
| 35 |
+
except Exception as e:
|
| 36 |
+
log_error(self.error_log, e)
|
| 37 |
+
return {"error": str(e)}
|
| 38 |
+
|
| 39 |
+
def batch_export(self, period_days: int) -> Dict[str, Any]:
|
| 40 |
+
"""Export summary for past period (7/30/90 days)."""
|
| 41 |
+
try:
|
| 42 |
+
exported = export_metrics(period_days)
|
| 43 |
+
with self.lock:
|
| 44 |
+
self.metrics_cache[f"export_{period_days}"] = exported
|
| 45 |
+
return exported
|
| 46 |
+
except Exception as e:
|
| 47 |
+
log_error(self.error_log, e)
|
| 48 |
+
return {"error": str(e)}
|
| 49 |
+
|
| 50 |
+
def get_errors(self) -> List[str]:
|
| 51 |
+
"""Return error logs or missing data events."""
|
| 52 |
+
return self.error_log
|
| 53 |
+
|
| 54 |
+
def expose_endpoint(self, endpoint_type: str = "live") -> Dict[str, Any]:
|
| 55 |
+
"""Hook for UI/API to query/display current metrics."""
|
| 56 |
+
return self.metrics_cache.get(endpoint_type, {})
|
| 57 |
+
|
| 58 |
+
def scheduled_tasks(self):
|
| 59 |
+
"""Set up periodic triggers for end-of-day rollup/reports."""
|
| 60 |
+
# Implementation for actual scheduler integration needed.
|
| 61 |
+
pass
|
| 62 |
+
|
| 63 |
+
def clear_error_log(self):
|
| 64 |
+
self.error_log.clear()
|
| 65 |
+
|
| 66 |
+
# Usage Example:
|
| 67 |
+
# agent = AnalyticsAgent()
|
| 68 |
+
# live = agent.get_live_metrics()
|
| 69 |
+
# eod = agent.run_end_of_day_report()
|
| 70 |
+
# sevenday_export = agent.batch_export(7)
|
| 71 |
+
# agent.expose_endpoint()
|
| 72 |
+
# errors = agent.get_errors()
|