Spaces:
Sleeping
Sleeping
Integrate lead_scoring_agent: add scoring metrics to analytics and UI visibility
Browse files- analytics_agent.py +39 -16
analytics_agent.py
CHANGED
|
@@ -2,32 +2,38 @@ 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
|
|
@@ -35,38 +41,55 @@ class AnalyticsAgent:
|
|
| 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()
|
|
|
|
| 2 |
import json
|
| 3 |
import threading
|
| 4 |
from typing import Dict, Any, List, Optional
|
|
|
|
| 5 |
from .analytics_utils import fetch_metrics, export_metrics, log_error, save_report
|
| 6 |
+
from lead_scoring_agent import score_lead
|
| 7 |
|
| 8 |
class AnalyticsAgent:
|
| 9 |
+
"""Analytics Agent with integrated lead scoring metrics for visibility in analytics and UI."""
|
| 10 |
+
|
| 11 |
def __init__(self, config_path: str = "configs/analytics_config.json"):
|
| 12 |
with open(config_path, "r") as f:
|
| 13 |
self.config = json.load(f)
|
| 14 |
self.metrics_cache = {}
|
| 15 |
self.error_log = []
|
| 16 |
self.lock = threading.Lock()
|
| 17 |
+
|
| 18 |
def get_live_metrics(self, spec: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
| 19 |
+
"""Aggregate real-time metrics on demand (API/button/query), including lead scoring data."""
|
| 20 |
try:
|
| 21 |
metrics = fetch_metrics(spec or self.config["live_metrics_spec"])
|
| 22 |
+
# Include lead scoring metrics in analytics
|
| 23 |
+
metrics["lead_scoring"] = self._get_lead_scoring_metrics()
|
| 24 |
with self.lock:
|
| 25 |
self.metrics_cache["live"] = metrics
|
| 26 |
return metrics
|
| 27 |
except Exception as e:
|
| 28 |
log_error(self.error_log, e)
|
| 29 |
return {"error": str(e)}
|
| 30 |
+
|
| 31 |
def run_end_of_day_report(self) -> Dict[str, Any]:
|
| 32 |
+
"""Aggregate metrics, save, notify/report at end of day (scheduled) with scoring breakdowns."""
|
| 33 |
try:
|
| 34 |
report = fetch_metrics(self.config["end_of_day_spec"])
|
| 35 |
+
# Add lead scoring summary to daily report
|
| 36 |
+
report["lead_scoring_summary"] = self._get_lead_scoring_metrics()
|
| 37 |
save_report(report, period="day")
|
| 38 |
with self.lock:
|
| 39 |
self.metrics_cache["end_of_day"] = report
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
log_error(self.error_log, e)
|
| 43 |
return {"error": str(e)}
|
| 44 |
+
|
| 45 |
def batch_export(self, period_days: int) -> Dict[str, Any]:
|
| 46 |
+
"""Export summary for past period (7/30/90 days) including score distribution."""
|
| 47 |
try:
|
| 48 |
exported = export_metrics(period_days)
|
| 49 |
+
# Include lead scoring trends in export
|
| 50 |
+
exported["lead_scoring_trends"] = self._get_lead_scoring_metrics(period_days)
|
| 51 |
with self.lock:
|
| 52 |
self.metrics_cache[f"export_{period_days}"] = exported
|
| 53 |
return exported
|
| 54 |
except Exception as e:
|
| 55 |
log_error(self.error_log, e)
|
| 56 |
return {"error": str(e)}
|
| 57 |
+
|
| 58 |
+
def _get_lead_scoring_metrics(self, period_days: Optional[int] = None) -> Dict[str, Any]:
|
| 59 |
+
"""Retrieve lead scoring metrics for analytics visibility."""
|
| 60 |
+
# Query database/cache for lead scoring data
|
| 61 |
+
# This would integrate with your data layer to fetch scored leads
|
| 62 |
+
return {
|
| 63 |
+
"total_scored_leads": 0, # Populate from database
|
| 64 |
+
"score_distribution": {
|
| 65 |
+
"high": 0,
|
| 66 |
+
"medium": 0,
|
| 67 |
+
"low": 0
|
| 68 |
+
},
|
| 69 |
+
"average_score": 0.0,
|
| 70 |
+
"scoring_enabled": True
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
def get_errors(self) -> List[str]:
|
| 74 |
"""Return error logs or missing data events."""
|
| 75 |
return self.error_log
|
| 76 |
+
|
| 77 |
def expose_endpoint(self, endpoint_type: str = "live") -> Dict[str, Any]:
|
| 78 |
+
"""Hook for UI/API to query/display current metrics with lead scoring data."""
|
| 79 |
return self.metrics_cache.get(endpoint_type, {})
|
| 80 |
+
|
| 81 |
def scheduled_tasks(self):
|
| 82 |
"""Set up periodic triggers for end-of-day rollup/reports."""
|
| 83 |
# Implementation for actual scheduler integration needed.
|
| 84 |
pass
|
| 85 |
+
|
| 86 |
def clear_error_log(self):
|
| 87 |
self.error_log.clear()
|
| 88 |
|
| 89 |
# Usage Example:
|
| 90 |
# agent = AnalyticsAgent()
|
| 91 |
+
# live = agent.get_live_metrics() # Now includes lead_scoring metrics
|
| 92 |
+
# eod = agent.run_end_of_day_report() # Now includes lead_scoring_summary
|
| 93 |
+
# sevenday_export = agent.batch_export(7) # Now includes lead_scoring_trends
|
| 94 |
# agent.expose_endpoint()
|
| 95 |
+
# errors = agent.get_errors()
|