Adding Monthly data and Week column
Browse files
apps/kpi_analysis/trafic_analysis.py
CHANGED
@@ -1,4 +1,7 @@
|
|
|
|
|
|
1 |
from datetime import datetime
|
|
|
2 |
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
@@ -8,6 +11,29 @@ from utils.convert_to_excel import convert_dfs, save_dataframe
|
|
8 |
from utils.utils_vars import get_physical_db
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
class TraficAnalysis:
|
12 |
last_period_df: pd.DataFrame = None
|
13 |
|
@@ -145,6 +171,39 @@ def merge_and_compare(df_2g, df_3g, df_lte, pre_range, post_range, last_period_r
|
|
145 |
return df, last_period, pivot.round(2)
|
146 |
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
############################## UI #########################
|
149 |
st.title("📊 Global Trafic Analysis - 2G / 3G / LTE")
|
150 |
doc_col, image_col = st.columns(2)
|
@@ -165,17 +224,11 @@ with doc_col:
|
|
165 |
|
166 |
upload_2g_col, upload_3g_col, upload_lte_col = st.columns(3)
|
167 |
with upload_2g_col:
|
168 |
-
two_g_file = st.file_uploader(
|
169 |
-
"Upload 2G Traffic Report", type=["csv", "xls", "xlsx"]
|
170 |
-
)
|
171 |
with upload_3g_col:
|
172 |
-
three_g_file = st.file_uploader(
|
173 |
-
"Upload 3G Traffic Report", type=["csv", "xls", "xlsx"]
|
174 |
-
)
|
175 |
with upload_lte_col:
|
176 |
-
lte_file = st.file_uploader(
|
177 |
-
"Upload LTE Traffic Report", type=["csv", "xls", "xlsx"]
|
178 |
-
)
|
179 |
|
180 |
pre_range_col, post_range_col = st.columns(2)
|
181 |
with pre_range_col:
|
@@ -200,9 +253,9 @@ if not all([two_g_file, three_g_file, lte_file]):
|
|
200 |
|
201 |
if st.button("🔍 Run Analysis"):
|
202 |
|
203 |
-
df_2g =
|
204 |
-
df_3g =
|
205 |
-
df_lte =
|
206 |
|
207 |
df_2g_clean = preprocess_2g(df_2g)
|
208 |
df_3g_clean = preprocess_3g(df_3g)
|
@@ -212,6 +265,11 @@ if st.button("🔍 Run Analysis"):
|
|
212 |
df_2g_clean, df_3g_clean, df_lte_clean, pre_range, post_range, last_period_range
|
213 |
)
|
214 |
|
|
|
|
|
|
|
|
|
|
|
215 |
# 🔍 Display Summary
|
216 |
st.success("✅ Analysis completed")
|
217 |
st.subheader("📈 Summary Analysis Pre / Post")
|
@@ -386,7 +444,13 @@ if TraficAnalysis.last_period_df is not None:
|
|
386 |
st.plotly_chart(fig)
|
387 |
|
388 |
final_dfs = convert_dfs(
|
389 |
-
[full_df, summary_df
|
|
|
|
|
|
|
|
|
|
|
|
|
390 |
)
|
391 |
# 📥 Bouton de téléchargement
|
392 |
st.download_button(
|
|
|
1 |
+
import io
|
2 |
+
import zipfile
|
3 |
from datetime import datetime
|
4 |
+
from pathlib import Path
|
5 |
|
6 |
import pandas as pd
|
7 |
import plotly.express as px
|
|
|
11 |
from utils.utils_vars import get_physical_db
|
12 |
|
13 |
|
14 |
+
def read_uploaded_file(uploaded_file):
|
15 |
+
"""Read uploaded file, handling both ZIP and CSV formats.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
uploaded_file: Uploaded file object from Streamlit
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
pd.DataFrame: DataFrame containing the data from the uploaded file
|
22 |
+
"""
|
23 |
+
if uploaded_file.name.endswith(".zip"):
|
24 |
+
with zipfile.ZipFile(io.BytesIO(uploaded_file.getvalue())) as z:
|
25 |
+
# Get the first CSV file in the zip
|
26 |
+
csv_files = [f for f in z.namelist() if f.lower().endswith(".csv")]
|
27 |
+
if not csv_files:
|
28 |
+
raise ValueError("No CSV file found in the ZIP archive")
|
29 |
+
with z.open(csv_files[0]) as f:
|
30 |
+
return pd.read_csv(f, encoding="latin1", sep=";", low_memory=False)
|
31 |
+
elif uploaded_file.name.endswith(".csv"):
|
32 |
+
return pd.read_csv(uploaded_file, encoding="latin1", sep=";", low_memory=False)
|
33 |
+
else:
|
34 |
+
raise ValueError("Unsupported file format. Please upload a ZIP or CSV file.")
|
35 |
+
|
36 |
+
|
37 |
class TraficAnalysis:
|
38 |
last_period_df: pd.DataFrame = None
|
39 |
|
|
|
171 |
return df, last_period, pivot.round(2)
|
172 |
|
173 |
|
174 |
+
def monthly_data_analysis(df: pd.DataFrame) -> pd.DataFrame:
|
175 |
+
df["date"] = pd.to_datetime(df["date"])
|
176 |
+
|
177 |
+
# Create column 'YYYY-MM' for grouping by month while keeping the year
|
178 |
+
df["month_year"] = df["date"].dt.to_period("M").astype(str)
|
179 |
+
|
180 |
+
# Pivot : lines = code, columns = month_year, values = sum
|
181 |
+
voice_trafic = df.pivot_table(
|
182 |
+
index="code",
|
183 |
+
columns="month_year",
|
184 |
+
values="total_voice_trafic",
|
185 |
+
aggfunc="sum",
|
186 |
+
fill_value=0,
|
187 |
+
)
|
188 |
+
|
189 |
+
# Sort columns chronologically
|
190 |
+
voice_trafic = voice_trafic.reindex(sorted(voice_trafic.columns), axis=1)
|
191 |
+
|
192 |
+
data_trafic = df.pivot_table(
|
193 |
+
index="code",
|
194 |
+
columns="month_year",
|
195 |
+
values="total_data_trafic",
|
196 |
+
aggfunc="sum",
|
197 |
+
fill_value=0,
|
198 |
+
)
|
199 |
+
|
200 |
+
# Sort columns chronologically
|
201 |
+
data_trafic = data_trafic.reindex(sorted(data_trafic.columns), axis=1)
|
202 |
+
|
203 |
+
# Display result
|
204 |
+
return voice_trafic, data_trafic
|
205 |
+
|
206 |
+
|
207 |
############################## UI #########################
|
208 |
st.title("📊 Global Trafic Analysis - 2G / 3G / LTE")
|
209 |
doc_col, image_col = st.columns(2)
|
|
|
224 |
|
225 |
upload_2g_col, upload_3g_col, upload_lte_col = st.columns(3)
|
226 |
with upload_2g_col:
|
227 |
+
two_g_file = st.file_uploader("Upload 2G Traffic Report", type=["csv", "zip"])
|
|
|
|
|
228 |
with upload_3g_col:
|
229 |
+
three_g_file = st.file_uploader("Upload 3G Traffic Report", type=["csv", "zip"])
|
|
|
|
|
230 |
with upload_lte_col:
|
231 |
+
lte_file = st.file_uploader("Upload LTE Traffic Report", type=["csv", "zip"])
|
|
|
|
|
232 |
|
233 |
pre_range_col, post_range_col = st.columns(2)
|
234 |
with pre_range_col:
|
|
|
253 |
|
254 |
if st.button("🔍 Run Analysis"):
|
255 |
|
256 |
+
df_2g = read_uploaded_file(two_g_file)
|
257 |
+
df_3g = read_uploaded_file(three_g_file)
|
258 |
+
df_lte = read_uploaded_file(lte_file)
|
259 |
|
260 |
df_2g_clean = preprocess_2g(df_2g)
|
261 |
df_3g_clean = preprocess_3g(df_3g)
|
|
|
265 |
df_2g_clean, df_3g_clean, df_lte_clean, pre_range, post_range, last_period_range
|
266 |
)
|
267 |
|
268 |
+
monthly_voice_df, monthly_data_df = monthly_data_analysis(full_df)
|
269 |
+
|
270 |
+
full_df["week"] = full_df["date"].dt.isocalendar().week
|
271 |
+
full_df["year"] = full_df["date"].dt.isocalendar().year
|
272 |
+
|
273 |
# 🔍 Display Summary
|
274 |
st.success("✅ Analysis completed")
|
275 |
st.subheader("📈 Summary Analysis Pre / Post")
|
|
|
444 |
st.plotly_chart(fig)
|
445 |
|
446 |
final_dfs = convert_dfs(
|
447 |
+
[full_df, summary_df, monthly_voice_df, monthly_data_df],
|
448 |
+
[
|
449 |
+
"Global_Trafic_Analysis",
|
450 |
+
"Pre_Post_analysis",
|
451 |
+
"Monthly_voice_analysis",
|
452 |
+
"Monthly_data_analysis",
|
453 |
+
],
|
454 |
)
|
455 |
# 📥 Bouton de téléchargement
|
456 |
st.download_button(
|