File size: 16,403 Bytes
6277fe0
b560569
575b933
b0464a9
87a87e7
791c130
8add36b
 
 
 
 
 
 
f7fc39b
575b933
826a2a1
2e2e19a
8add36b
575b933
 
2e2e19a
8add36b
2e2e19a
2bd9dad
 
8add36b
 
 
 
 
 
 
2601f1c
8add36b
 
 
5a483f8
8add36b
 
94b5f2a
a9e9029
 
 
8add36b
3ff351d
8add36b
 
a9e9029
 
1fa587a
2e2e19a
8add36b
 
7aa6c73
2a3b22e
3b4dccb
2a3b22e
2e2e19a
8add36b
1644cc1
77179e2
 
1644cc1
77179e2
2e2e19a
77179e2
8add36b
adb3bbe
2e2e19a
67742c4
a342a6b
6a8e128
8add36b
2e2e19a
6a8e128
 
 
2601f1c
67742c4
6277fe0
8add36b
 
 
 
adb3bbe
8add36b
 
 
 
7aa6c73
a342a6b
d33040c
 
 
6277fe0
a342a6b
575b933
8add36b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791c130
 
8add36b
 
 
 
6277fe0
8add36b
 
1644cc1
8add36b
 
 
 
 
 
 
 
1644cc1
8add36b
 
 
 
 
 
 
 
 
 
 
 
 
1644cc1
8add36b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2e19a
8add36b
 
 
2e2e19a
8add36b
2e2e19a
8add36b
2e2e19a
8add36b
 
 
 
 
1644cc1
8add36b
 
1644cc1
2e2e19a
1644cc1
8add36b
2e2e19a
8add36b
 
 
 
 
2e2e19a
8add36b
 
 
 
c67cf25
8add36b
 
1644cc1
2e2e19a
8add36b
 
1644cc1
8add36b
 
1644cc1
 
8add36b
 
 
 
1644cc1
8add36b
 
3e26bda
8add36b
 
1644cc1
8add36b
 
 
 
 
 
 
 
2e2e19a
8add36b
 
 
 
 
2e2e19a
8add36b
 
5a483f8
266ae82
adb3bbe
8add36b
2e2e19a
a6bc02b
8add36b
 
 
 
 
1fa587a
8add36b
 
 
 
 
 
1fa587a
8add36b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# app.py
import gradio as gr
import pandas as pd
import os
import logging
import matplotlib
matplotlib.use('Agg') # Set backend for Matplotlib
import matplotlib.pyplot as plt
import time
from datetime import datetime, timedelta
import numpy as np
from collections import OrderedDict, defaultdict # Added defaultdict
import asyncio

# --- Module Imports ---
from utils.gradio_utils import get_url_user_token

# Functions from newly created/refactored modules
from config import (
    LINKEDIN_CLIENT_ID_ENV_VAR, BUBBLE_APP_NAME_ENV_VAR,
    BUBBLE_API_KEY_PRIVATE_ENV_VAR, BUBBLE_API_ENDPOINT_ENV_VAR,
    PLOT_ID_TO_FORMULA_KEY_MAP # Keep this if used by AnalyticsTab
)
from services.state_manager import process_and_store_bubble_token
from services.sync_logic import sync_all_linkedin_data_orchestrator
from ui.ui_generators import (
    display_main_dashboard,
    build_analytics_tab_plot_area, # This will be passed to AnalyticsTab
    BOMB_ICON, EXPLORE_ICON, FORMULA_ICON, ACTIVE_ICON # These will be passed
)
from ui.analytics_plot_generator import update_analytics_plots_figures, create_placeholder_plot # Pass these
from formulas import PLOT_FORMULAS # Keep this if used by AnalyticsTab

# --- EXISTING CHATBOT MODULE IMPORTS ---
from features.chatbot.chatbot_prompts import get_initial_insight_prompt_and_suggestions # Pass this
from features.chatbot.chatbot_handler import generate_llm_response # Pass this

# --- NEW AGENTIC PIPELINE IMPORTS ---
try:
    from run_agentic_pipeline import run_agentic_pipeline_autonomously
    from ui.insights_ui_generator import (
        format_single_okr_for_display
    )
    AGENTIC_MODULES_LOADED = True
except:
    logging.error(f"Could not import agentic pipeline modules: {e}. Tabs 3 and 4 will be disabled.")
    AGENTIC_MODULES_LOADED = False
    def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded. OKR display unavailable." # Placeholder



# --- IMPORT THE NEW ANALYTICS TAB MODULE ---
from services.analytics_tab_module import AnalyticsTab # Assuming analytics_tab_module.py is in the services directory

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')

# API Key Setup
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "False"
user_provided_api_key = os.environ.get("GEMINI_API_KEY")
if user_provided_api_key:
    os.environ["GOOGLE_API_KEY"] = user_provided_api_key
    logging.info("GOOGLE_API_KEY environment variable has been set from GEMINI_API_KEY.")
else:
    logging.error("CRITICAL ERROR: The API key environment variable 'GEMINI_API_KEY' was not found.")


with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
                title="LinkedIn Organization Dashboard") as app:
    token_state = gr.State(value={
        "token": None, "client_id": None, "org_urn": None,
        "bubble_posts_df": pd.DataFrame(), "bubble_post_stats_df": pd.DataFrame(),
        "bubble_mentions_df": pd.DataFrame(),
        "bubble_follower_stats_df": pd.DataFrame(),
        "fetch_count_for_api": 0, "url_user_token_temp_storage": None,
        "config_date_col_posts": "published_at", "config_date_col_mentions": "date",
        "config_date_col_followers": "date", "config_media_type_col": "media_type",
        "config_eb_labels_col": "li_eb_label"
    })

    # States for existing analytics tab chatbot - these are passed to AnalyticsTab
    chat_histories_st = gr.State({})
    current_chat_plot_id_st = gr.State(None)
    plot_data_for_chatbot_st = gr.State({}) # This will be populated by the analytics module's refresh

    # --- STATES FOR AGENTIC PIPELINE ---
    orchestration_raw_results_st = gr.State(None) # Stores the full raw output from the agentic pipeline
    key_results_for_selection_st = gr.State([]) # Stores the list of dicts for KR selection (label, id, etc.)
    selected_key_result_ids_st = gr.State([]) # Stores the unique_kr_ids selected in the CheckboxGroup

    gr.Markdown("# 🚀 LinkedIn Organization Dashboard")
    url_user_token_display = gr.Textbox(label="User Token (Nascosto)", interactive=False, visible=False)
    status_box = gr.Textbox(label="Stato Generale Token LinkedIn", interactive=False, value="Inizializzazione...")
    org_urn_display = gr.Textbox(label="URN Organizzazione (Nascosto)", interactive=False, visible=False)

    app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn_display], api_name="get_url_params", show_progress=False)

    def initial_load_sequence(url_token, org_urn_val, current_state):
        status_msg, new_state, btn_update = process_and_store_bubble_token(url_token, org_urn_val, current_state)
        dashboard_content = display_main_dashboard(new_state)
        return status_msg, new_state, btn_update, dashboard_content

    # --- Instantiate the AnalyticsTab module ---
    analytics_icons = {
        'bomb': BOMB_ICON, 'explore': EXPLORE_ICON,
        'formula': FORMULA_ICON, 'active': ACTIVE_ICON
    }
    analytics_tab_instance = AnalyticsTab(
        token_state=token_state,
        chat_histories_st=chat_histories_st,
        current_chat_plot_id_st=current_chat_plot_id_st,
        plot_data_for_chatbot_st=plot_data_for_chatbot_st,
        plot_id_to_formula_map=PLOT_ID_TO_FORMULA_KEY_MAP,
        plot_formulas_data=PLOT_FORMULAS,
        icons=analytics_icons,
        fn_build_plot_area=build_analytics_tab_plot_area,
        fn_update_plot_figures=update_analytics_plots_figures,
        fn_create_placeholder_plot=create_placeholder_plot,
        fn_get_initial_insight=get_initial_insight_prompt_and_suggestions,
        fn_generate_llm_response=generate_llm_response
    )

    with gr.Tabs() as tabs:
        with gr.TabItem("1️⃣ Dashboard & Sync", id="tab_dashboard_sync"):
            gr.Markdown("Il sistema controlla i dati esistenti da Bubble. 'Sincronizza' si attiva se sono necessari nuovi dati.")
            sync_data_btn = gr.Button("🔄 Sincronizza Dati LinkedIn", variant="primary", visible=False, interactive=False)
            sync_status_html_output = gr.HTML("<p style='text-align:center;'>Stato sincronizzazione...</p>")
            dashboard_display_html = gr.HTML("<p style='text-align:center;'>Caricamento dashboard...</p>")

        # --- Use the AnalyticsTab module to create Tab 2 ---
        analytics_tab_instance.create_tab_ui()

        # --- Tab 3: Agentic Analysis Report ---
        with gr.TabItem("3️⃣ Agentic Analysis Report", id="tab_agentic_report", visible=AGENTIC_MODULES_LOADED):
            gr.Markdown("## 🤖 Comprehensive Analysis Report (AI Generated)")
            agentic_pipeline_status_md = gr.Markdown("Stato Pipeline AI (filtro 'Sempre'): In attesa...", visible=True)
            gr.Markdown("Questo report è generato da un agente AI con filtro 'Sempre' sui dati disponibili. Rivedi criticamente.")
            agentic_report_display_md = gr.Markdown("La pipeline AI si avvierà automaticamente dopo il caricamento iniziale dei dati o dopo una sincronizzazione.")
            if not AGENTIC_MODULES_LOADED:
                gr.Markdown("🔴 **Error:** Agentic pipeline modules could not be loaded. This tab is disabled.")

        # --- Tab 4: Agentic OKRs & Tasks ---
        with gr.TabItem("4️⃣ Agentic OKRs & Tasks", id="tab_agentic_okrs", visible=AGENTIC_MODULES_LOADED):
            gr.Markdown("## 🎯 AI Generated OKRs and Actionable Tasks (filtro 'Sempre')")
            gr.Markdown("Basato sull'analisi AI (filtro 'Sempre'), l'agente ha proposto i seguenti OKR e task. Seleziona i Key Results per dettagli.")
            if not AGENTIC_MODULES_LOADED:
                gr.Markdown("🔴 **Error:** Agentic pipeline modules could not be loaded. This tab is disabled.")
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("### Suggested Key Results (da analisi 'Sempre')")
                    key_results_cbg = gr.CheckboxGroup(label="Select Key Results", choices=[], value=[], interactive=True)
                with gr.Column(scale=3):
                    gr.Markdown("### Detailed OKRs and Tasks for Selected Key Results")
                    okr_detail_display_md = gr.Markdown("I dettagli OKR appariranno qui dopo l'esecuzione della pipeline AI.")

            def update_okr_display_on_selection(selected_kr_unique_ids: list, raw_orchestration_results: dict, all_krs_for_selection: list):
                if not raw_orchestration_results or not AGENTIC_MODULES_LOADED:
                    return gr.update(value="Nessun dato dalla pipeline AI o moduli non caricati.")

                actionable_okrs_dict = raw_orchestration_results.get("actionable_okrs_and_tasks")
                if not actionable_okrs_dict or not isinstance(actionable_okrs_dict.get("okrs"), list):
                    return gr.update(value="Nessun OKR trovato nei risultati della pipeline.")

                okrs_list = actionable_okrs_dict["okrs"]
                # Ensure all_krs_for_selection is a list of dicts with expected keys
                if not all_krs_for_selection or not isinstance(all_krs_for_selection, list) or \
                   not all(isinstance(kr, dict) and 'unique_kr_id' in kr and 'okr_index' in kr and 'kr_index' in kr for kr in all_krs_for_selection):
                    logging.error("all_krs_for_selection is not in the expected format.")
                    return gr.update(value="Errore interno: formato dati KR non valido.")


                kr_id_to_indices = {kr_info['unique_kr_id']: (kr_info['okr_index'], kr_info['kr_index']) for kr_info in all_krs_for_selection}

                selected_krs_by_okr_idx = defaultdict(list)
                if selected_kr_unique_ids:
                    for kr_unique_id in selected_kr_unique_ids:
                        if kr_unique_id in kr_id_to_indices:
                            okr_idx, kr_idx = kr_id_to_indices[kr_unique_id]
                            selected_krs_by_okr_idx[okr_idx].append(kr_idx)

                output_md_parts = []
                if not okrs_list:
                    output_md_parts.append("Nessun OKR generato.")
                else:
                    for okr_idx, okr_data in enumerate(okrs_list):
                        accepted_indices_for_this_okr = selected_krs_by_okr_idx.get(okr_idx)
                        # If specific KRs are selected, only show OKRs that have at least one of the selected KRs
                        # OR if no KRs are selected at all, show all OKRs.
                        if selected_kr_unique_ids: # User has made a selection
                            if accepted_indices_for_this_okr is not None: # This OKR has some of the selected KRs
                                output_md_parts.append(format_single_okr_for_display(okr_data, accepted_kr_indices=accepted_indices_for_this_okr, okr_main_index=okr_idx))
                        else: # No KRs selected, show all OKRs with all their KRs
                            output_md_parts.append(format_single_okr_for_display(okr_data, accepted_kr_indices=None, okr_main_index=okr_idx))
                
                if not output_md_parts and selected_kr_unique_ids:
                    final_md = "Nessun OKR corrisponde alla selezione corrente o i KR selezionati non hanno task dettagliati."
                elif not output_md_parts and not selected_kr_unique_ids: # Should be covered by "Nessun OKR generato."
                    final_md = "Nessun OKR generato."
                else:
                    final_md = "\n\n---\n\n".join(output_md_parts)
                return gr.update(value=final_md)

            if AGENTIC_MODULES_LOADED:
                key_results_cbg.change(
                    fn=update_okr_display_on_selection,
                    inputs=[key_results_cbg, orchestration_raw_results_st, key_results_for_selection_st],
                    outputs=[okr_detail_display_md],
                    api_name="update_okr_display_on_selection_module"
                )


    # Define the output list for the agentic pipeline callbacks
    # Order: Report MD, KR CBG, OKR Detail MD, RawResults State, SelectedKRIDs State, KRList State, Status MD
    agentic_pipeline_outputs_list = [
        agentic_report_display_md,
        key_results_cbg,
        okr_detail_display_md,
        orchestration_raw_results_st,
        selected_key_result_ids_st,
        key_results_for_selection_st,
        agentic_pipeline_status_md
    ]
    agentic_pipeline_inputs = [token_state] # Input for the autonomous run

    # --- Event Handling ---
    initial_load_event = org_urn_display.change(
        fn=initial_load_sequence,
        inputs=[url_user_token_display, org_urn_display, token_state],
        outputs=[status_box, token_state, sync_data_btn, dashboard_display_html],
        show_progress="full"
    )

    initial_load_event.then(
        fn=analytics_tab_instance._refresh_analytics_graphs_ui,
        inputs=[
            token_state,
            analytics_tab_instance.date_filter_selector,
            analytics_tab_instance.custom_start_date_picker,
            analytics_tab_instance.custom_end_date_picker,
            chat_histories_st
        ],
        outputs=analytics_tab_instance.graph_refresh_outputs_list,
        show_progress="full"
    ).then(
        fn=run_agentic_pipeline_autonomously, # Generator function
        inputs=[token_state, orchestration_raw_results_st, selected_key_result_ids_st, key_results_for_selection_st],
        outputs=agentic_pipeline_outputs_list,
        show_progress="minimal" # Use minimal for generators that yield status
    )

    sync_event_part1 = sync_data_btn.click(
        fn=sync_all_linkedin_data_orchestrator,
        inputs=[token_state],
        outputs=[sync_status_html_output, token_state],
        show_progress="full"
    )
    sync_event_part2 = sync_event_part1.then(
        fn=process_and_store_bubble_token,
        inputs=[url_user_token_display, org_urn_display, token_state],
        outputs=[status_box, token_state, sync_data_btn],
        show_progress=False
    )
    sync_event_part2.then(
        fn=run_agentic_pipeline_autonomously, # Generator function
        inputs=[token_state, orchestration_raw_results_st, selected_key_result_ids_st, key_results_for_selection_st],
        outputs=agentic_pipeline_outputs_list,
        show_progress="minimal"
    )
    sync_event_part3 = sync_event_part2.then(
        fn=display_main_dashboard,
        inputs=[token_state],
        outputs=[dashboard_display_html],
        show_progress=False
    )
    sync_event_graphs_after_sync = sync_event_part3.then(
        fn=analytics_tab_instance._refresh_analytics_graphs_ui,
        inputs=[
            token_state,
            analytics_tab_instance.date_filter_selector,
            analytics_tab_instance.custom_start_date_picker,
            analytics_tab_instance.custom_end_date_picker,
            chat_histories_st
        ],
        outputs=analytics_tab_instance.graph_refresh_outputs_list,
        show_progress="full"
    )

if __name__ == "__main__":
    if not os.environ.get(LINKEDIN_CLIENT_ID_ENV_VAR):
        logging.warning(f"ATTENZIONE: '{LINKEDIN_CLIENT_ID_ENV_VAR}' non impostata.")
    if not all(os.environ.get(var) for var in [BUBBLE_APP_NAME_ENV_VAR, BUBBLE_API_KEY_PRIVATE_ENV_VAR, BUBBLE_API_ENDPOINT_ENV_VAR]):
        logging.warning("ATTENZIONE: Una o più variabili d'ambiente Bubble (BUBBLE_APP_NAME, BUBBLE_API_KEY_PRIVATE, BUBBLE_API_ENDPOINT) non sono impostate.")
    if not AGENTIC_MODULES_LOADED:
        logging.warning("CRITICAL: Agentic pipeline modules failed to load. Tabs 3 and 4 (Agentic Report & OKRs) will be non-functional.")
    if not os.environ.get("GEMINI_API_KEY"): # Check GEMINI_API_KEY directly as GOOGLE_API_KEY is derived
        logging.warning("ATTENZIONE: 'GEMINI_API_KEY' non impostata. Questo è necessario per le funzionalità AI, incluse le tab agentiche e il chatbot dei grafici.")
    
    try:
        logging.info(f"Gradio version: {gr.__version__}")
        logging.info(f"Pandas version: {pd.__version__}")
        logging.info(f"Matplotlib version: {matplotlib.__version__}, Backend: {matplotlib.get_backend()}")
    except Exception as e:
        logging.warning(f"Could not log library versions: {e}")

    app.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)), debug=True)