GuglielmoTor commited on
Commit
c473dc0
·
verified ·
1 Parent(s): fed5558

Update services/report_data_handler.py

Browse files
Files changed (1) hide show
  1. services/report_data_handler.py +15 -20
services/report_data_handler.py CHANGED
@@ -31,8 +31,6 @@ def fetch_latest_agentic_analysis(org_urn: str) -> Tuple[Optional[pd.DataFrame],
31
  return None, "org_urn is missing."
32
 
33
  try:
34
- # We fetch all reports and will sort them later if needed, but typically the
35
- # external process should manage providing the "latest" or "active" report.
36
  report_data_df, error = fetch_linkedin_posts_data_from_bubble(
37
  data_type=BUBBLE_REPORT_TABLE_NAME,
38
  constraint_value=org_urn,
@@ -56,33 +54,30 @@ def fetch_latest_agentic_analysis(org_urn: str) -> Tuple[Optional[pd.DataFrame],
56
  return None, str(e)
57
 
58
 
59
- def fetch_and_reconstruct_data_from_bubble(report_df: pd.DataFrame) -> Optional[Dict[str, Any]]:
60
  """
61
- Takes a DataFrame of report data, fetches all related child items (OKRs, KRs, Tasks)
62
- from Bubble, and reconstructs the full nested dictionary expected by the UI.
63
 
64
  Args:
65
- report_df: The DataFrame containing one or more reports, fetched previously.
66
 
67
  Returns:
68
  A dictionary containing the reconstructed data ('report_str', 'actionable_okrs'),
69
- or None if the report is not found or a critical error occurs.
70
  """
71
- logger.info("Starting data reconstruction from fetched Bubble data.")
72
- if report_df is None or report_df.empty:
73
- logger.warning("Cannot reconstruct data, the provided report DataFrame is empty.")
74
  return None
75
 
76
  try:
77
- # Assuming the most recent report is desired if multiple are returned.
78
- # You might need more sophisticated logic here to select the "active" report.
79
- latest_report = report_df.sort_values(by='Created Date', ascending=False).iloc[0]
80
- report_id = latest_report.get('_id')
81
  if not report_id:
82
- logger.error("Fetched report is missing a Bubble '_id', cannot reconstruct children.")
83
  return None
84
 
85
- logger.info(f"Reconstructing data for the latest report, ID: {report_id}")
86
 
87
  # 1. Fetch all related OKRs using the report_id
88
  okrs_df, error = fetch_linkedin_posts_data_from_bubble(
@@ -137,13 +132,13 @@ def fetch_and_reconstruct_data_from_bubble(report_df: pd.DataFrame) -> Optional[
137
  # 5. Assemble the final payload for the UI
138
  actionable_okrs = {"okrs": reconstructed_okrs}
139
  final_reconstructed_data = {
140
- "report_str": latest_report.get("report_text", "Report text not found."),
141
- "quarter": latest_report.get("quarter"),
142
- "year": latest_report.get("year"),
143
  "actionable_okrs": actionable_okrs,
144
  "report_id": report_id
145
  }
146
- logger.info("Successfully reconstructed nested data structure for the UI.")
147
  return final_reconstructed_data
148
 
149
  except Exception as e:
 
31
  return None, "org_urn is missing."
32
 
33
  try:
 
 
34
  report_data_df, error = fetch_linkedin_posts_data_from_bubble(
35
  data_type=BUBBLE_REPORT_TABLE_NAME,
36
  constraint_value=org_urn,
 
54
  return None, str(e)
55
 
56
 
57
+ def fetch_and_reconstruct_data_from_bubble(report_series: pd.Series) -> Optional[Dict[str, Any]]:
58
  """
59
+ MODIFIED: Takes a pandas Series of a single report, fetches all related child items
60
+ (OKRs, KRs, Tasks) from Bubble, and reconstructs the full nested dictionary.
61
 
62
  Args:
63
+ report_series: A pandas Series representing a single report to be processed.
64
 
65
  Returns:
66
  A dictionary containing the reconstructed data ('report_str', 'actionable_okrs'),
67
+ or None if the report series is invalid or a critical error occurs.
68
  """
69
+ logger.info("Starting data reconstruction from a single fetched Bubble report.")
70
+ if report_series is None or report_series.empty:
71
+ logger.warning("Cannot reconstruct data, the provided report Series is empty.")
72
  return None
73
 
74
  try:
75
+ report_id = report_series.get('_id')
 
 
 
76
  if not report_id:
77
+ logger.error("Fetched report series is missing a Bubble '_id', cannot reconstruct children.")
78
  return None
79
 
80
+ logger.info(f"Reconstructing all related data for report, ID: {report_id}")
81
 
82
  # 1. Fetch all related OKRs using the report_id
83
  okrs_df, error = fetch_linkedin_posts_data_from_bubble(
 
132
  # 5. Assemble the final payload for the UI
133
  actionable_okrs = {"okrs": reconstructed_okrs}
134
  final_reconstructed_data = {
135
+ "report_str": report_series.get("report_text", "Report text not found."),
136
+ "quarter": report_series.get("quarter"),
137
+ "year": report_series.get("year"),
138
  "actionable_okrs": actionable_okrs,
139
  "report_id": report_id
140
  }
141
+ logger.info(f"Successfully reconstructed nested data structure for report {report_id}.")
142
  return final_reconstructed_data
143
 
144
  except Exception as e: