GuglielmoTor commited on
Commit
8fa7748
Β·
verified Β·
1 Parent(s): de146fb

Update services/state_manager.py

Browse files
Files changed (1) hide show
  1. services/state_manager.py +28 -0
services/state_manager.py CHANGED
@@ -28,6 +28,8 @@ from config import (
28
  LOG_SUBJECT_POSTS, LOG_SUBJECT_MENTIONS, LOG_SUBJECT_FOLLOWER_STATS
29
  )
30
 
 
 
31
  def check_token_status(token_state):
32
  """Checks the status of the LinkedIn token."""
33
  return "βœ… Token available" if token_state and token_state.get("token") else "❌ Token not available"
@@ -147,6 +149,32 @@ def process_and_store_bubble_token(url_user_token, org_urn, token_state):
147
  except Exception as e:
148
  logging.error(f"❌ Error fetching {table_name} from Bubble: {e}.", exc_info=True)
149
  new_state[state_key] = pd.DataFrame()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  else:
151
  logging.warning("Org URN not available in state. Cannot fetch data from Bubble.")
152
  for key in ["bubble_posts_df", "bubble_mentions_df", "bubble_follower_stats_df", "bubble_operations_log_df"]:
 
28
  LOG_SUBJECT_POSTS, LOG_SUBJECT_MENTIONS, LOG_SUBJECT_FOLLOWER_STATS
29
  )
30
 
31
+ from .report_data_handler import fetch_latest_agentic_analysis
32
+
33
  def check_token_status(token_state):
34
  """Checks the status of the LinkedIn token."""
35
  return "βœ… Token available" if token_state and token_state.get("token") else "❌ Token not available"
 
149
  except Exception as e:
150
  logging.error(f"❌ Error fetching {table_name} from Bubble: {e}.", exc_info=True)
151
  new_state[state_key] = pd.DataFrame()
152
+
153
+ new_state["agentic_pipeline_should_run_now"] = False
154
+ new_state["bubble_agentic_analysis_data"] = None
155
+
156
+ # Fetch all report data
157
+ all_analysis_data, error = fetch_latest_agentic_analysis(current_org_urn)
158
+
159
+ if all_analysis_data is not None and not all_analysis_data.empty:
160
+ new_state["bubble_agentic_analysis_data"] = all_analysis_data
161
+
162
+ quarter_data = all_analysis_data[all_analysis_data['type'] == 'Quarter'].copy()
163
+
164
+ if not quarter_data.empty:
165
+ latest_quarter_analysis = quarter_data.sort_values(by='Created Date', ascending=False).iloc[0]
166
+ should_run_due_to_no_data = pd.isna(latest_quarter_analysis)
167
+ else:
168
+ should_run_due_to_no_data = True # No quarter data found
169
+ else:
170
+ should_run_due_to_no_data = True # No data at all
171
+
172
+ if should_run_due_to_no_data:
173
+ logging.info(f"No existing agentic analysis data found for org {current_org_urn}. Triggering run.")
174
+ new_state["agentic_pipeline_should_run_now"] = True
175
+ else:
176
+ logging.info(f"Agentic analysis data for org {current_org_urn} is up-to-date.")
177
+
178
  else:
179
  logging.warning("Org URN not available in state. Cannot fetch data from Bubble.")
180
  for key in ["bubble_posts_df", "bubble_mentions_df", "bubble_follower_stats_df", "bubble_operations_log_df"]: