rafaldembski commited on
Commit
b3eb2dd
verified
1 Parent(s): 3655f6c

Update pages/Statistics.py

Browse files
Files changed (1) hide show
  1. pages/Statistics.py +106 -59
pages/Statistics.py CHANGED
@@ -4,8 +4,51 @@ import plotly.express as px
4
  import json
5
  from datetime import datetime
6
 
7
- # Ustawienie konfiguracji strony (nale偶y upewni膰 si臋, 偶e nie zostanie to wywo艂ane wi臋cej ni偶 raz)
8
- st.set_page_config(page_title="馃搳 Statystyki", page_icon="馃搱", layout="wide")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Funkcja do pobierania statystyk
11
  def get_stats():
@@ -27,60 +70,64 @@ def get_history():
27
  except (json.JSONDecodeError, FileNotFoundError):
28
  return []
29
 
30
- # Pobieranie danych z plik贸w
31
- stats = get_stats()
32
- history = get_history()
33
-
34
- # Kluczowe metryki
35
- total_analyses = stats["total_analyses"]
36
- total_frauds_detected = stats["total_frauds_detected"]
37
-
38
- # Wy艣wietlenie metryk w poziomych kolumnach
39
- st.title("馃搳 Statystyki Aplikacji")
40
- st.markdown("Poni偶ej znajduj膮 si臋 statystyki analizy wiadomo艣ci w aplikacji.")
41
-
42
- col1, col2, col3 = st.columns(3)
43
- col1.metric(label="Liczba przeanalizowanych wiadomo艣ci", value=total_analyses)
44
- col2.metric(label="Wykryte oszustwa", value=total_frauds_detected)
45
-
46
- # Obs艂uga przypadku, gdy total_analyses jest r贸wne 0
47
- if total_analyses > 0:
48
- fraud_percentage = (total_frauds_detected / total_analyses) * 100
49
- else:
50
- fraud_percentage = 0 # Ustawienie 0% w przypadku braku analiz
51
-
52
- col3.metric(label="Procent oszustw", value=f"{fraud_percentage:.2f}%")
53
-
54
- # Wy艣wietlenie historii analiz w tabeli
55
- if history:
56
- st.markdown("### Historia analizowanych wiadomo艣ci")
57
- df_history = pd.DataFrame(history)
58
-
59
- # Formatowanie daty
60
- df_history['timestamp'] = pd.to_datetime(df_history['timestamp'])
61
-
62
- # Wy艣wietlenie tabeli historii
63
- st.dataframe(df_history[['timestamp', 'phone_number', 'risk_assessment']], height=300)
64
-
65
- # Wizualizacja liczby analiz w czasie
66
- st.markdown("### Liczba analizowanych wiadomo艣ci w czasie")
67
- df_history['date'] = df_history['timestamp'].dt.date
68
- analyses_over_time = df_history.groupby('date').size().reset_index(name='counts')
69
- fig_analyses_over_time = px.line(analyses_over_time, x='date', y='counts', title='Liczba analizowanych wiadomo艣ci w czasie')
70
- st.plotly_chart(fig_analyses_over_time)
71
-
72
- # Wizualizacja oszustw w czasie
73
- st.markdown("### Liczba wykrytych oszustw w czasie")
74
- df_history['fraud_detected'] = df_history['risk_assessment'].apply(lambda x: 'Wykryte oszustwo' if '10/10' in x else 'Brak oszustwa')
75
- frauds_over_time = df_history.groupby(['date', 'fraud_detected']).size().reset_index(name='counts')
76
- fig_frauds_over_time = px.bar(frauds_over_time, x='date', y='counts', color='fraud_detected', title='Wykryte oszustwa w czasie')
77
- st.plotly_chart(fig_frauds_over_time)
78
- else:
79
- st.info("Brak dost臋pnych danych do wy艣wietlenia.")
80
-
81
- # Wizualizacje ryzyka oszustwa (je艣li s膮 dost臋pne dane)
82
- if history:
83
- st.markdown("### Rozk艂ad ocen ryzyka oszustwa")
84
- df_history['risk_score'] = df_history['risk_assessment'].apply(lambda x: int(x.split('/')[0]) if '/' in x else 0)
85
- fig_risk_distribution = px.histogram(df_history, x='risk_score', nbins=10, title='Rozk艂ad ocen ryzyka oszustwa (1-10)')
86
- st.plotly_chart(fig_risk_distribution)
 
 
 
 
 
4
  import json
5
  from datetime import datetime
6
 
7
+ # Definiowanie t艂umacze艅 dla zak艂adki "Statystyki"
8
+ page_translations = {
9
+ 'Polish': {
10
+ 'page_title': "馃搳 Statystyki",
11
+ 'page_icon': "馃搱",
12
+ 'header': "馃搳 Statystyki Aplikacji",
13
+ 'description': "Poni偶ej znajduj膮 si臋 statystyki analizy wiadomo艣ci w aplikacji.",
14
+ 'total_analyses': "Liczba przeanalizowanych wiadomo艣ci",
15
+ 'total_frauds_detected': "Wykryte oszustwa",
16
+ 'fraud_percentage': "Procent oszustw",
17
+ 'history_title': "Historia analizowanych wiadomo艣ci",
18
+ 'analyses_over_time': "Liczba analizowanych wiadomo艣ci w czasie",
19
+ 'frauds_over_time': "Liczba wykrytych oszustw w czasie",
20
+ 'risk_distribution': "Rozk艂ad ocen ryzyka oszustwa",
21
+ 'no_data': "Brak dost臋pnych danych do wy艣wietlenia."
22
+ },
23
+ 'German': {
24
+ 'page_title': "馃搳 Statistiken",
25
+ 'page_icon': "馃搱",
26
+ 'header': "馃搳 Anwendungsstatistiken",
27
+ 'description': "Nachfolgend finden Sie die Statistiken zur Nachrichtenanalyse in der Anwendung.",
28
+ 'total_analyses': "Anzahl der analysierten Nachrichten",
29
+ 'total_frauds_detected': "Erkannte Betr眉gereien",
30
+ 'fraud_percentage': "Betrugsprozentsatz",
31
+ 'history_title': "Analyseverlauf der Nachrichten",
32
+ 'analyses_over_time': "Anzahl der analysierten Nachrichten im Laufe der Zeit",
33
+ 'frauds_over_time': "Anzahl der erkannten Betr眉gereien im Laufe der Zeit",
34
+ 'risk_distribution': "Verteilung der Betrugsrisikobewertungen",
35
+ 'no_data': "Keine Daten zur Anzeige verf眉gbar."
36
+ },
37
+ 'English': {
38
+ 'page_title': "馃搳 Statistics",
39
+ 'page_icon': "馃搱",
40
+ 'header': "馃搳 Application Statistics",
41
+ 'description': "Below are the statistics of message analysis in the app.",
42
+ 'total_analyses': "Total Messages Analyzed",
43
+ 'total_frauds_detected': "Frauds Detected",
44
+ 'fraud_percentage': "Fraud Percentage",
45
+ 'history_title': "History of Analyzed Messages",
46
+ 'analyses_over_time': "Number of Analyzed Messages Over Time",
47
+ 'frauds_over_time': "Number of Detected Frauds Over Time",
48
+ 'risk_distribution': "Distribution of Fraud Risk Scores",
49
+ 'no_data': "No data available to display."
50
+ }
51
+ }
52
 
53
  # Funkcja do pobierania statystyk
54
  def get_stats():
 
70
  except (json.JSONDecodeError, FileNotFoundError):
71
  return []
72
 
73
+ # G艂贸wna funkcja zak艂adki "Statystyki"
74
+ def main(language):
75
+ translations = page_translations[language]
76
+
77
+ # Pobieranie danych z plik贸w
78
+ stats = get_stats()
79
+ history = get_history()
80
+
81
+ # Kluczowe metryki
82
+ total_analyses = stats["total_analyses"]
83
+ total_frauds_detected = stats["total_frauds_detected"]
84
+
85
+ # Wy艣wietlenie metryk
86
+ st.title(translations['header'])
87
+ st.markdown(translations['description'])
88
+
89
+ col1, col2, col3 = st.columns(3)
90
+ col1.metric(label=translations['total_analyses'], value=total_analyses)
91
+ col2.metric(label=translations['total_frauds_detected'], value=total_frauds_detected)
92
+
93
+ # Obs艂uga dzielenia przez zero
94
+ if total_analyses > 0:
95
+ fraud_percentage = (total_frauds_detected / total_analyses) * 100
96
+ else:
97
+ fraud_percentage = 0 # Ustawienie na 0% w przypadku braku analiz
98
+
99
+ col3.metric(label=translations['fraud_percentage'], value=f"{fraud_percentage:.2f}%")
100
+
101
+ # Wy艣wietlenie historii analiz w tabeli
102
+ if history:
103
+ st.markdown(f"### {translations['history_title']}")
104
+ df_history = pd.DataFrame(history)
105
+
106
+ # Formatowanie daty
107
+ df_history['timestamp'] = pd.to_datetime(df_history['timestamp'])
108
+
109
+ # Wy艣wietlenie tabeli historii
110
+ st.dataframe(df_history[['timestamp', 'phone_number', 'risk_assessment']], height=300)
111
+
112
+ # Wizualizacja liczby analiz w czasie
113
+ st.markdown(f"### {translations['analyses_over_time']}")
114
+ df_history['date'] = df_history['timestamp'].dt.date
115
+ analyses_over_time = df_history.groupby('date').size().reset_index(name='counts')
116
+ fig_analyses_over_time = px.line(analyses_over_time, x='date', y='counts', title=translations['analyses_over_time'])
117
+ st.plotly_chart(fig_analyses_over_time)
118
+
119
+ # Wizualizacja oszustw w czasie
120
+ st.markdown(f"### {translations['frauds_over_time']}")
121
+ df_history['fraud_detected'] = df_history['risk_assessment'].apply(lambda x: 'Detected Fraud' if '10/10' in x else 'No Fraud')
122
+ frauds_over_time = df_history.groupby(['date', 'fraud_detected']).size().reset_index(name='counts')
123
+ fig_frauds_over_time = px.bar(frauds_over_time, x='date', y='counts', color='fraud_detected', title=translations['frauds_over_time'])
124
+ st.plotly_chart(fig_frauds_over_time)
125
+ else:
126
+ st.info(translations['no_data'])
127
+
128
+ # Wizualizacje ryzyka oszustwa (je艣li s膮 dost臋pne dane)
129
+ if history:
130
+ st.markdown(f"### {translations['risk_distribution']}")
131
+ df_history['risk_score'] = df_history['risk_assessment'].apply(lambda x: int(x.split('/')[0]) if '/' in x else 0)
132
+ fig_risk_distribution = px.histogram(df_history, x='risk_score', nbins=10, title=translations['risk_distribution'])
133
+ st.plotly_chart(fig_risk_distribution)