Spaces:
Running
Running
Create report_data_handler.py
Browse files
services/report_data_handler.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# services/report_data_handler.py
|
2 |
+
import pandas as pd
|
3 |
+
import logging
|
4 |
+
from apis.Bubble_API_Calls import fetch_linkedin_posts_data_from_bubble, bulk_upload_to_bubble
|
5 |
+
from config import (
|
6 |
+
BUBBLE_REPORT_TABLE_NAME,
|
7 |
+
BUBBLE_OKR_TABLE_NAME,
|
8 |
+
BUBBLE_KEY_RESULTS_TABLE_NAME,
|
9 |
+
BUBBLE_TASKS_TABLE_NAME,
|
10 |
+
BUBBLE_KR_UPDATE_TABLE_NAME,
|
11 |
+
)
|
12 |
+
import json # For handling JSON data
|
13 |
+
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
def fetch_latest_agentic_analysis(org_urn: str):
|
17 |
+
"""
|
18 |
+
Fetches the most recent agentic analysis for a given org_urn from Bubble.
|
19 |
+
Returns the analysis data (dict) and its timestamp, or None, None.
|
20 |
+
"""
|
21 |
+
if not org_urn:
|
22 |
+
logger.warning("fetch_latest_agentic_analysis: org_urn is missing.")
|
23 |
+
return None
|
24 |
+
|
25 |
+
# Simpler: fetch all from this table (if it's not too large) or filter by org_urn
|
26 |
+
# This example assumes you might fetch more and then filter.
|
27 |
+
report_data_df, error = fetch_linkedin_posts_data_from_bubble(
|
28 |
+
data_type=BUBBLE_REPORT_TABLE_NAME,
|
29 |
+
org_urn=org_urn # Or use this if your function supports it for filtering
|
30 |
+
)
|
31 |
+
|
32 |
+
if error or report_data_df is None or report_data_df.empty:
|
33 |
+
logger.info(f"No existing agentic analysis found in Bubble for org_urn {org_urn} or error: {error}")
|
34 |
+
return None
|
35 |
+
|
36 |
+
# Filter for the specific org_urn if not done by the fetch call
|
37 |
+
quarter_report_data_df = report_data_df[report_data_df['type'] == 'Quarter'].copy()
|
38 |
+
|
39 |
+
if quarter_report_data_df.empty:
|
40 |
+
logger.info(f"No agentic analysis found for org_urn {org_urn} after filtering.")
|
41 |
+
return None
|
42 |
+
|
43 |
+
latest_analysis = quarter_report_data_df.sort_values(by='Created Date', ascending=False).iloc[0]
|
44 |
+
|
45 |
+
logger.info(f"Latest agentic analysis found for org_urn {org_urn}}")
|
46 |
+
return latest_report
|
47 |
+
|
48 |
+
|
49 |
+
def save_agentic_analysis_results(
|
50 |
+
org_urn: str,
|
51 |
+
report_markdown: str,
|
52 |
+
orchestration_raw_results: dict, # This is the full output from agentic pipeline
|
53 |
+
# key_results_for_selection: list, # Pass this too if you want to save it
|
54 |
+
generation_timestamp: pd.Timestamp
|
55 |
+
):
|
56 |
+
"""Saves the agentic pipeline results to Bubble."""
|
57 |
+
if not org_urn:
|
58 |
+
logger.error("Cannot save agentic results: org_urn missing.")
|
59 |
+
return False
|
60 |
+
|
61 |
+
# Extract what you need from orchestration_raw_results
|
62 |
+
actionable_okrs_dict = orchestration_raw_results.get("actionable_okrs_and_tasks", {})
|
63 |
+
# Assuming key_results_for_selection is also part of orchestration_raw_results or passed separately
|
64 |
+
key_results_selection_list = orchestration_raw_results.get("key_results_for_selection", [])
|
65 |
+
|
66 |
+
|
67 |
+
payload = {
|
68 |
+
"org_urn": org_urn,
|
69 |
+
BUBBLE_AGENTIC_TIMESTAMP_COLUMN: generation_timestamp.isoformat(), # Store as ISO string
|
70 |
+
"report_markdown": report_markdown if report_markdown else "N/A",
|
71 |
+
"okrs_json": json.dumps(actionable_okrs_dict) if actionable_okrs_dict else json.dumps({}),
|
72 |
+
"key_results_selection_json": json.dumps(key_results_selection_list) if key_results_selection_list else json.dumps([]),
|
73 |
+
# Add other fields as needed
|
74 |
+
}
|
75 |
+
logger.info(f"Attempting to save agentic analysis to Bubble for org_urn: {org_urn}")
|
76 |
+
success_ids = bulk_upload_to_bubble([payload], BUBBLE_AGENTIC_RESULTS_TABLE_NAME)
|
77 |
+
|
78 |
+
if success_ids and success_ids[0]: # bulk_upload_to_bubble returns list of IDs or False
|
79 |
+
logger.info(f"Successfully saved agentic analysis to Bubble. Record ID: {success_ids[0]}")
|
80 |
+
return True
|
81 |
+
else:
|
82 |
+
logger.error("Failed to save agentic analysis to Bubble.")
|
83 |
+
return False
|