File size: 4,178 Bytes
89973ac
3d9b28f
89973ac
3d9b28f
 
89973ac
 
3d9b28f
89973ac
3d9b28f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89973ac
3d9b28f
89973ac
3d9b28f
89973ac
3d9b28f
 
89973ac
3d9b28f
 
89973ac
3d9b28f
 
 
 
89973ac
3d9b28f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89973ac
3d9b28f
 
89973ac
3d9b28f
89973ac
3d9b28f
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
import streamlit as st
from endpoints_config import API_ENDPOINTS, ENV
from services.cache_admin import purge_cache
from services.stats_logs import fetch_stats_data, fetch_logs_data
from log import logger


logger.info(f"ENV : {ENV}")
st.set_page_config(page_title="Admin Recoplante", page_icon="🛠️", layout="wide")
st.title("🛠️ Tableau de bord d'administration Recoplante")


st.markdown("### ⚙️ Actions globales")
# Initialisation
if "confirm_clear_pred" not in st.session_state:
    st.session_state.confirm_clear_pred = False
if "confirm_clear_heatmap" not in st.session_state:
    st.session_state.confirm_clear_heatmap = False

col_global1, col_global2, col_global3 = st.columns(3)

# === Purge Prédictions ===
with col_global1:
    if not st.session_state.confirm_clear_pred:
        if st.button("🧹 Purger toutes les prédictions"):
            st.session_state.confirm_clear_pred = True
            st.rerun()
    else:
        st.warning("⚠️ Cette action supprimera toutes les prédictions en cache.")
        if st.button("✅ Confirmer purge prédictions"):
            for service_name, config in API_ENDPOINTS.items():
                if "clear_pred" in config:
                    purge_cache(config, "clear_pred")
            st.success("✅ Prédictions purgées.")
            st.session_state.confirm_clear_pred = False
            st.rerun()
        if st.button("❌ Annuler", key="cancel_pred"):
            st.session_state.confirm_clear_pred = False
            st.rerun()

# === Purge Heatmaps ===
with col_global2:
    if not st.session_state.confirm_clear_heatmap:
        if st.button("🔥 Purger toutes les heatmaps"):
            st.session_state.confirm_clear_heatmap = True
            st.rerun()
    else:
        st.warning("⚠️ Cette action supprimera toutes les heatmaps en cache.")
        if st.button("✅ Confirmer purge heatmaps"):
            for service_name, config in API_ENDPOINTS.items():
                if "clear_heatmap" in config:
                    purge_cache(config, "clear_heatmap")
            st.success("✅ Heatmaps purgées.")
            st.session_state.confirm_clear_heatmap = False
            st.rerun()
        if st.button("❌ Annuler", key="cancel_heatmap"):
            st.session_state.confirm_clear_heatmap = False
            st.rerun()

# === Rafraîchir toute la page ===
with col_global3:
    if st.button("🔄 Rafraîchir tout"):
        st.rerun()

# Pour chaque service/API
for service_name, config in API_ENDPOINTS.items():
    st.markdown(f"## 🔹 {service_name}")

    # Fetch live stats
    stats = fetch_stats_data(config)

    # Ligne d'état principale
    col1, col2, col3, col4, col5, col6 = st.columns([1.5, 1, 1, 1, 1, 3])

    with col1:
        st.metric("Statut", "✅ OK" if stats else "❌ OFFLINE")
    with col2:
        st.metric("RAM", stats.get("memory_usage_mb", "–") if stats else "–", "MB")
    with col3:
        st.metric("Uptime", stats.get("uptime_human", "–") if stats else "–")
    with col4:
        st.metric("Requêtes", stats.get("request_count", "–") if stats else "–")
    with col5:
        st.metric("Cache", stats.get("cache_items", "–") if stats else "–")

    with col6:
        colb1, colb2, colb3 = st.columns(3)
        with colb1:
            if "logs" in config and st.button("📜 Logs", key=f"logs_{service_name}"):
                logs = fetch_logs_data(config)
                with st.expander(f"📜 Logs détaillés - {service_name}"):
                    st.code("".join(logs), language="bash")

        with colb2:
            if "clear_pred" in config and st.button("🧹 Pred", key=f"pred_{service_name}"):
                purge_cache(config, "clear_pred")

        with colb3:
            if "clear_heatmap" in config and st.button("🔥 Heatmap", key=f"hm_{service_name}"):
                purge_cache(config, "clear_heatmap")

    if "clear_state" in config and st.button("♻️ Reset orchestrateur", key=f"reset_{service_name}"):
        purge_cache(config, "clear_state")

    st.markdown("---")

st.caption("⚙️ Interface Admin Recoplante — Pilotage centralisé de l’infrastructure")