GuglielmoTor commited on
Commit
4ffa7e5
·
verified ·
1 Parent(s): db9e84a

Update services/analytics_handlers.py

Browse files
Files changed (1) hide show
  1. services/analytics_handlers.py +50 -30
services/analytics_handlers.py CHANGED
@@ -94,57 +94,75 @@ class AnalyticsHandlers:
94
  return outputs
95
 
96
  async def refresh_analytics_graphs_ui(self, current_token_state_val, date_filter_val,
97
- custom_start_val, custom_end_val,
98
- # chat_histories_st is a state, its value will be accessed via self.chat_histories_st.value
99
- ):
100
  logging.info(f"Refreshing analytics graph UI. Filter: {date_filter_val}. Token set: {'yes' if current_token_state_val.get('token') else 'no'}")
101
  start_time = time.time()
102
 
103
  # Call the function that generates plot figures and summaries
104
- # Ensure update_analytics_plots_figures is adapted to return:
105
- # status_msg (str), figures_dict (dict: {plot_id: fig}), summaries_dict (dict: {plot_id: summary_text})
106
  plot_gen_results = update_analytics_plots_figures(
107
  current_token_state_val,
108
  date_filter_val,
109
  custom_start_val,
110
  custom_end_val,
111
- self.plot_configs # Pass plot_configs to it
112
  )
113
- # Expected: status_msg, list_of_figures, dict_of_plot_summaries
114
- # Original: status_msg, gen_figs (list), new_summaries (dict)
115
-
116
- status_msg = plot_gen_results[0]
117
- gen_figs_result = plot_gen_results[1] # This might be a single figure or a list
118
- new_summaries_dict = plot_gen_results[2] # This should be a dict {plot_id: summary}
119
-
120
- # Handle the case where gen_figs_result might be a single figure or a list
 
 
 
 
 
 
 
 
121
  if isinstance(gen_figs_result, list):
122
  gen_figs_list = gen_figs_result
123
  else:
124
- # If it's a single figure, wrap it in a list
125
- gen_figs_list = [gen_figs_result]
126
- logging.warning(f"Expected list of figures but got single figure of type {type(gen_figs_result)}")
127
-
128
- all_updates = [gr.update(value=status_msg)] # For analytics_status_md
 
 
 
 
 
 
129
 
130
  # Update plot components with new figures
131
  if len(gen_figs_list) == len(self.plot_configs):
 
132
  for fig in gen_figs_list:
133
- all_updates.append(fig) # fig itself is the update for gr.Plot
 
 
 
 
 
 
 
134
  else:
 
135
  logging.error(f"Figure list length mismatch: got {len(gen_figs_list)}, expected {len(self.plot_configs)}")
136
- # If we have fewer figures than expected, pad with placeholders
137
  for i in range(len(self.plot_configs)):
138
  if i < len(gen_figs_list):
139
  all_updates.append(gen_figs_list[i])
140
  else:
141
  all_updates.append(create_placeholder_plot("Error", "Figura mancante"))
142
 
143
- # Reset action panel UI elements
144
  all_updates.extend([
145
  gr.update(visible=False), # global_actions_column_ui
146
- gr.update(value=[], visible=False), # insights_chatbot_ui (value and visibility)
147
- gr.update(value="", visible=False), # insights_chat_input_ui (value and visibility)
148
  gr.update(visible=False), # insights_suggestions_row_ui
149
  gr.update(value="S1"), # insights_suggestion_1_btn
150
  gr.update(value="S2"), # insights_suggestion_2_btn
@@ -157,8 +175,8 @@ class AnalyticsHandlers:
157
  all_updates.extend([
158
  None, # active_panel_action_state
159
  None, # current_chat_plot_id_st
160
- {}, # chat_histories_st (reset to empty dict)
161
- new_summaries_dict # plot_data_for_chatbot_st (update with new summaries)
162
  ])
163
 
164
  # Reset buttons and panel visibility for each plot
@@ -167,10 +185,10 @@ class AnalyticsHandlers:
167
  gr.update(value=BOMB_ICON), # bomb_button
168
  gr.update(value=FORMULA_ICON), # formula_button
169
  gr.update(value=EXPLORE_ICON), # explore_button
170
- gr.update(visible=True) # panel_component (plot visibility)
171
  ])
172
 
173
- all_updates.append(None) # explored_plot_id_state (reset)
174
 
175
  # Reset section title visibility
176
  for _ in self.unique_ordered_sections:
@@ -181,10 +199,12 @@ class AnalyticsHandlers:
181
 
182
  expected_len = 1 + len(self.plot_configs) + 9 + 4 + (4 * len(self.plot_configs)) + 1 + self.num_unique_sections
183
  logging.info(f"Prepared {len(all_updates)} updates for graph refresh. Expected {expected_len}.")
 
184
  if len(all_updates) != expected_len:
185
  logging.error(f"Output length mismatch in refresh_analytics_graphs_ui. Got {len(all_updates)}, expected {expected_len}")
186
- # Pad with gr.update() if lengths don't match, to avoid Gradio errors, though this indicates a logic flaw.
187
- all_updates.extend([gr.update()] * (expected_len - len(all_updates)))
 
188
 
189
  return tuple(all_updates)
190
 
 
94
  return outputs
95
 
96
  async def refresh_analytics_graphs_ui(self, current_token_state_val, date_filter_val,
97
+ custom_start_val, custom_end_val):
 
 
98
  logging.info(f"Refreshing analytics graph UI. Filter: {date_filter_val}. Token set: {'yes' if current_token_state_val.get('token') else 'no'}")
99
  start_time = time.time()
100
 
101
  # Call the function that generates plot figures and summaries
 
 
102
  plot_gen_results = update_analytics_plots_figures(
103
  current_token_state_val,
104
  date_filter_val,
105
  custom_start_val,
106
  custom_end_val,
107
+ self.plot_configs
108
  )
109
+
110
+ # Debug: Log what we actually got back
111
+ logging.debug(f"plot_gen_results type: {type(plot_gen_results)}, length: {len(plot_gen_results) if hasattr(plot_gen_results, '__len__') else 'N/A'}")
112
+
113
+ # Handle different possible return formats
114
+ if len(plot_gen_results) == 3:
115
+ # Expected format: (status_msg, figures_list, summaries_dict)
116
+ status_msg, gen_figs_result, new_summaries_dict = plot_gen_results
117
+ else:
118
+ # Fallback - try to parse based on old format
119
+ # Old format seemed to be: status_msg, *figures, summaries_dict
120
+ status_msg = plot_gen_results[0]
121
+ new_summaries_dict = plot_gen_results[-1]
122
+ gen_figs_result = plot_gen_results[1:-1] # Everything between first and last
123
+
124
+ # Ensure we have a list of figures
125
  if isinstance(gen_figs_result, list):
126
  gen_figs_list = gen_figs_result
127
  else:
128
+ # If it's a single figure, we need to check if it should be replicated
129
+ # or if the function should return multiple figures
130
+ logging.warning(f"Got single figure of type {type(gen_figs_result)}, expected list of {len(self.plot_configs)} figures")
131
+
132
+ # Option 1: If the single figure should be used for all plots (unlikely)
133
+ # gen_figs_list = [gen_figs_result] * len(self.plot_configs)
134
+
135
+ # Option 2: If this indicates an error in the function (more likely)
136
+ gen_figs_list = [gen_figs_result] # Keep as single item list for now
137
+
138
+ all_updates = [gr.update(value=status_msg)] # For analytics_status_md
139
 
140
  # Update plot components with new figures
141
  if len(gen_figs_list) == len(self.plot_configs):
142
+ # Perfect match - use figures as-is
143
  for fig in gen_figs_list:
144
+ all_updates.append(fig)
145
+ elif len(gen_figs_list) == 1 and len(self.plot_configs) > 1:
146
+ # Special case: one figure for multiple plots - check if this is intentional
147
+ logging.warning(f"Using single figure for {len(self.plot_configs)} plot components")
148
+ # Use the single figure for the first plot, placeholders for the rest
149
+ all_updates.append(gen_figs_list[0])
150
+ for i in range(1, len(self.plot_configs)):
151
+ all_updates.append(create_placeholder_plot("Info", f"Plot {i+1} - Data processing"))
152
  else:
153
+ # Length mismatch - pad with placeholders
154
  logging.error(f"Figure list length mismatch: got {len(gen_figs_list)}, expected {len(self.plot_configs)}")
 
155
  for i in range(len(self.plot_configs)):
156
  if i < len(gen_figs_list):
157
  all_updates.append(gen_figs_list[i])
158
  else:
159
  all_updates.append(create_placeholder_plot("Error", "Figura mancante"))
160
 
161
+ # Reset action panel UI elements (same as before)
162
  all_updates.extend([
163
  gr.update(visible=False), # global_actions_column_ui
164
+ gr.update(value=[], visible=False), # insights_chatbot_ui
165
+ gr.update(value="", visible=False), # insights_chat_input_ui
166
  gr.update(visible=False), # insights_suggestions_row_ui
167
  gr.update(value="S1"), # insights_suggestion_1_btn
168
  gr.update(value="S2"), # insights_suggestion_2_btn
 
175
  all_updates.extend([
176
  None, # active_panel_action_state
177
  None, # current_chat_plot_id_st
178
+ {}, # chat_histories_st
179
+ new_summaries_dict # plot_data_for_chatbot_st
180
  ])
181
 
182
  # Reset buttons and panel visibility for each plot
 
185
  gr.update(value=BOMB_ICON), # bomb_button
186
  gr.update(value=FORMULA_ICON), # formula_button
187
  gr.update(value=EXPLORE_ICON), # explore_button
188
+ gr.update(visible=True) # panel_component
189
  ])
190
 
191
+ all_updates.append(None) # explored_plot_id_state
192
 
193
  # Reset section title visibility
194
  for _ in self.unique_ordered_sections:
 
199
 
200
  expected_len = 1 + len(self.plot_configs) + 9 + 4 + (4 * len(self.plot_configs)) + 1 + self.num_unique_sections
201
  logging.info(f"Prepared {len(all_updates)} updates for graph refresh. Expected {expected_len}.")
202
+
203
  if len(all_updates) != expected_len:
204
  logging.error(f"Output length mismatch in refresh_analytics_graphs_ui. Got {len(all_updates)}, expected {expected_len}")
205
+ # Pad with gr.update() to prevent Gradio errors
206
+ while len(all_updates) < expected_len:
207
+ all_updates.append(gr.update())
208
 
209
  return tuple(all_updates)
210