|
1 | | -import pandas as pd |
2 | 1 | import polars as pl |
3 | | -import plotly.express as px |
4 | 2 | import streamlit as st |
5 | 3 |
|
6 | 4 | if "parsed_df" not in st.session_state: |
|
35 | 33 | # ---- FILTRE DATE ---- |
36 | 34 | with col1: |
37 | 35 | st.markdown("### 📅 Date") |
38 | | - start_date = st.date_input("Date début", min_date) |
39 | | - end_date = st.date_input("Date fin", max_date) |
| 36 | + start_date = st.date_input("Start Date", min_date) |
| 37 | + end_date = st.date_input("End Date", max_date) |
40 | 38 |
|
41 | 39 | # ---- FILTRE action---- |
42 | 40 | with col2: |
43 | 41 | st.markdown("### 🔄 Action") |
44 | 42 | if "action" in data.columns: |
45 | | - unique_action = sorted(data["action"].unique().cast(pl.Utf8).to_list()) # S'assurer du bon format |
46 | | - selected_action= st.selectbox("Sélectionnez un action", ["Tous"] + unique_action) |
| 43 | + unique_action = sorted( |
| 44 | + data["action"].unique().cast(pl.Utf8).to_list() |
| 45 | + ) # S'assurer du bon format |
| 46 | + selected_action = st.selectbox( |
| 47 | + "Select an action", ["All"] + unique_action |
| 48 | + ) |
47 | 49 | else: |
48 | | - selected_action= "Tous" |
49 | | - st.warning("Colonne 'action' non trouvée.") |
| 50 | + selected_action = "All" |
| 51 | + st.warning("Column 'action' not found.") |
50 | 52 |
|
51 | 53 | # ---- FILTRE portdst ---- |
52 | 54 | with col3: |
53 | 55 | st.markdown("### 🔢 Port") |
54 | 56 | if "portdst" in data.columns: |
55 | | - min_port, max_port = int(data["portdst"].min()), int(data["portdst"].max()) |
56 | | - selected_port = st.slider("Sélectionnez un port destination", min_port, max_port, (min_port, max_port)) |
| 57 | + min_port, max_port = ( |
| 58 | + int(data["portdst"].min()), |
| 59 | + int(data["portdst"].max()), |
| 60 | + ) |
| 61 | + |
| 62 | + # Initialize port range in session state if not present |
| 63 | + if "port_range" not in st.session_state: |
| 64 | + st.session_state.port_range = (min_port, max_port) |
| 65 | + |
| 66 | + # Quick port range selection buttons |
| 67 | + col_ports1, col_ports2, col_ports3 = st.columns(3) |
| 68 | + |
| 69 | + # Define button handlers to update session state |
| 70 | + def set_well_known(): |
| 71 | + st.session_state.port_range = (0, 1023) |
| 72 | + |
| 73 | + def set_registered(): |
| 74 | + st.session_state.port_range = (1024, 49151) |
| 75 | + |
| 76 | + def set_dynamic(): |
| 77 | + st.session_state.port_range = (49152, 65535) |
| 78 | + |
| 79 | + with col_ports1: |
| 80 | + st.button("Well-known (0-1023)", on_click=set_well_known) |
| 81 | + |
| 82 | + with col_ports2: |
| 83 | + st.button("Registered (1024-49151)", on_click=set_registered) |
| 84 | + |
| 85 | + with col_ports3: |
| 86 | + st.button("Dynamic (49152-65535)", on_click=set_dynamic) |
| 87 | + |
| 88 | + # Custom range slider that uses and updates the session state |
| 89 | + selected_port = st.slider( |
| 90 | + "Custom port range", |
| 91 | + min_port, |
| 92 | + max_port, |
| 93 | + value=st.session_state.port_range, |
| 94 | + key="port_slider", |
| 95 | + ) |
| 96 | + |
| 97 | + # Update port_range when slider changes |
| 98 | + st.session_state.port_range = selected_port |
57 | 99 | else: |
58 | | - min_port, max_port = 0, 600000 # Valeurs par défaut si la colonne est absente |
| 100 | + min_port, max_port = 0, 65535 # Standard TCP/IP port range |
59 | 101 | selected_port = (min_port, max_port) |
60 | | - st.warning("Colonne 'portdst' non trouvée, valeurs par défaut appliquées.") |
| 102 | + st.warning("Column 'portdst' not found, default values applied.") |
61 | 103 |
|
62 | 104 | # Vérification des dates sélectionnées |
63 | 105 | if start_date > end_date: |
64 | | - st.error("La date de début ne peut pas être postérieure à la date de fin.") |
| 106 | + st.error("The start date cannot be later than the end date.") |
65 | 107 | else: |
66 | 108 | # Conversion des dates en datetime |
67 | | - start_datetime = pl.datetime(start_date.year, start_date.month, start_date.day) |
68 | | - end_datetime = pl.datetime(end_date.year, end_date.month, end_date.day, 23, 59, 59) |
| 109 | + start_datetime = pl.datetime( |
| 110 | + start_date.year, start_date.month, start_date.day |
| 111 | + ) |
| 112 | + end_datetime = pl.datetime( |
| 113 | + end_date.year, end_date.month, end_date.day, 23, 59, 59 |
| 114 | + ) |
69 | 115 |
|
70 | 116 | # ---- APPLICATION DES FILTRES ---- |
71 | 117 | filtered_data = data.filter( |
72 | | - (pl.col("timestamp") >= start_datetime) & (pl.col("timestamp") <= end_datetime) |
| 118 | + (pl.col("timestamp") >= start_datetime) |
| 119 | + & (pl.col("timestamp") <= end_datetime) |
73 | 120 | ) |
74 | 121 |
|
75 | 122 | # Correction du filtrage par action(forcer conversion Utf8) |
76 | | - if "action" in data.columns and selected_action!= "Tous": |
77 | | - filtered_data = filtered_data.filter(pl.col("action").cast(pl.Utf8) == selected_action) |
| 123 | + if "action" in data.columns and selected_action != "All": |
| 124 | + filtered_data = filtered_data.filter( |
| 125 | + pl.col("action").cast(pl.Utf8) == selected_action |
| 126 | + ) |
78 | 127 |
|
79 | 128 | # Filtrer par portdst en prenant en compte min/max |
80 | 129 | if "portdst" in data.columns: |
81 | 130 | filtered_data = filtered_data.filter( |
82 | | - (pl.col("portdst").cast(pl.Int64) >= selected_port[0]) & |
83 | | - (pl.col("portdst").cast(pl.Int64) <= selected_port[1]) |
| 131 | + (pl.col("portdst").cast(pl.Int64) >= selected_port[0]) |
| 132 | + & (pl.col("portdst").cast(pl.Int64) <= selected_port[1]) |
84 | 133 | ) |
85 | 134 |
|
86 | 135 | # Affichage des données filtrées |
87 | | - st.write("### 🔍 Data filtred :") |
| 136 | + st.write(f"### 🔍 Data filtered : {filtered_data.shape[0]} entries") |
88 | 137 | st.dataframe(filtered_data) |
89 | 138 |
|
90 | 139 | else: |
91 | | - st.warning("La colonne 'timestamp' n'existe pas ou n'est pas au format datetime.") |
| 140 | + st.warning( |
| 141 | + "The 'timestamp' column does not exist or is not in datetime format." |
| 142 | + ) |
92 | 143 |
|
93 | 144 | # Onglet Sankey |
94 | 145 | with tab2: |
95 | 146 | st.subheader("Sankey Diagram") |
96 | | - |
|
0 commit comments