Spaces:
Running
Running
Update services/report_data_handler.py
Browse files
services/report_data_handler.py
CHANGED
@@ -60,6 +60,37 @@ def fetch_latest_agentic_analysis(org_urn: str) -> Tuple[Optional[pd.DataFrame],
|
|
60 |
logger.exception(f"An unexpected error occurred in fetch_latest_agentic_analysis for org_urn {org_urn}: {e}")
|
61 |
return None, str(e)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
def save_report_results(
|
65 |
org_urn: str,
|
@@ -163,7 +194,8 @@ def save_objectives(
|
|
163 |
|
164 |
def save_key_results(
|
165 |
org_urn: str,
|
166 |
-
objectives_with_ids: List[Tuple[Dict[str, Any], str]]
|
|
|
167 |
) -> Optional[List[Tuple[Dict[str, Any], str]]]:
|
168 |
"""
|
169 |
Saves Key Result records to Bubble, linking them to their parent objectives.
|
@@ -188,6 +220,10 @@ def save_key_results(
|
|
188 |
for kr in objective_data.get("key_results", []):
|
189 |
kr_type = kr.get("key_result_type")
|
190 |
key_results_to_process.append(kr)
|
|
|
|
|
|
|
|
|
191 |
key_result_payloads.append({
|
192 |
"okr": parent_objective_id,
|
193 |
"description": kr.get("key_result_description"),
|
@@ -196,6 +232,7 @@ def save_key_results(
|
|
196 |
# FIX: Convert Enum to its value before sending.
|
197 |
"kr_type": kr_type.value if hasattr(kr_type, 'value') else kr_type,
|
198 |
"data_subject": kr.get("data_subject"),
|
|
|
199 |
})
|
200 |
|
201 |
if not key_result_payloads:
|
@@ -299,7 +336,7 @@ def save_tasks(
|
|
299 |
|
300 |
# --- Orchestrator Function ---
|
301 |
|
302 |
-
def save_actionable_okrs(org_urn: str, actionable_okrs: Dict[str, Any], report_id: str):
|
303 |
"""
|
304 |
Orchestrates the sequential saving of objectives, key results, and tasks.
|
305 |
"""
|
@@ -332,7 +369,7 @@ def save_actionable_okrs(org_urn: str, actionable_okrs: Dict[str, Any], report_i
|
|
332 |
objectives_with_ids = list(zip(objectives_data, objective_ids))
|
333 |
|
334 |
# Step 2: Save the key results, linking them to the objectives
|
335 |
-
key_results_with_ids = save_key_results(org_urn, objectives_with_ids)
|
336 |
if key_results_with_ids is None:
|
337 |
logger.error("OKR save process aborted due to failure in saving key results.")
|
338 |
return
|
|
|
60 |
logger.exception(f"An unexpected error occurred in fetch_latest_agentic_analysis for org_urn {org_urn}: {e}")
|
61 |
return None, str(e)
|
62 |
|
63 |
+
def _get_metrics_for_subject(data_subject: Optional[str], metrics: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
64 |
+
"""
|
65 |
+
Selects the appropriate metrics dictionary from the main metrics object
|
66 |
+
based on the key result's data_subject.
|
67 |
+
|
68 |
+
Args:
|
69 |
+
data_subject: The data subject string from the key result (e.g., 'follower_stats').
|
70 |
+
metrics: The dictionary containing all available metrics for the agents.
|
71 |
+
|
72 |
+
Returns:
|
73 |
+
The relevant metrics dictionary, or None if no match is found.
|
74 |
+
"""
|
75 |
+
if not data_subject or not metrics:
|
76 |
+
return None
|
77 |
+
|
78 |
+
# This mapping connects the data_subject string values to the keys in the `metrics` parameter.
|
79 |
+
METRICS_KEY_MAPPING = {
|
80 |
+
"follower_stats": "follower_agent",
|
81 |
+
"posts": "post_agent",
|
82 |
+
"mentions": "mentions_agent",
|
83 |
+
}
|
84 |
+
|
85 |
+
# Find the corresponding key for the metrics dictionary (e.g., 'follower_agent').
|
86 |
+
metrics_key = METRICS_KEY_MAPPING.get(data_subject)
|
87 |
+
|
88 |
+
if not metrics_key:
|
89 |
+
logger.debug(f"No metrics mapping found for data_subject: '{data_subject}'")
|
90 |
+
return None
|
91 |
+
|
92 |
+
# Retrieve and return the actual metrics data using the resolved key.
|
93 |
+
return metrics.get(metrics_key)
|
94 |
|
95 |
def save_report_results(
|
96 |
org_urn: str,
|
|
|
194 |
|
195 |
def save_key_results(
|
196 |
org_urn: str,
|
197 |
+
objectives_with_ids: List[Tuple[Dict[str, Any], str]],
|
198 |
+
metrics
|
199 |
) -> Optional[List[Tuple[Dict[str, Any], str]]]:
|
200 |
"""
|
201 |
Saves Key Result records to Bubble, linking them to their parent objectives.
|
|
|
220 |
for kr in objective_data.get("key_results", []):
|
221 |
kr_type = kr.get("key_result_type")
|
222 |
key_results_to_process.append(kr)
|
223 |
+
data_subject_value = kr.get("data_subject")
|
224 |
+
|
225 |
+
metrics_for_kr = _get_metrics_for_subject(data_subject_value, metrics)
|
226 |
+
|
227 |
key_result_payloads.append({
|
228 |
"okr": parent_objective_id,
|
229 |
"description": kr.get("key_result_description"),
|
|
|
232 |
# FIX: Convert Enum to its value before sending.
|
233 |
"kr_type": kr_type.value if hasattr(kr_type, 'value') else kr_type,
|
234 |
"data_subject": kr.get("data_subject"),
|
235 |
+
"metriche_data_subject": metrics_for_kr
|
236 |
})
|
237 |
|
238 |
if not key_result_payloads:
|
|
|
336 |
|
337 |
# --- Orchestrator Function ---
|
338 |
|
339 |
+
def save_actionable_okrs(org_urn: str, actionable_okrs: Dict[str, Any], report_id: str, metrics):
|
340 |
"""
|
341 |
Orchestrates the sequential saving of objectives, key results, and tasks.
|
342 |
"""
|
|
|
369 |
objectives_with_ids = list(zip(objectives_data, objective_ids))
|
370 |
|
371 |
# Step 2: Save the key results, linking them to the objectives
|
372 |
+
key_results_with_ids = save_key_results(org_urn, objectives_with_ids, metrics)
|
373 |
if key_results_with_ids is None:
|
374 |
logger.error("OKR save process aborted due to failure in saving key results.")
|
375 |
return
|