rafaldembski commited on
Commit
bdb743d
verified
1 Parent(s): 816bdcd

Update pages/Statistics.py

Browse files
Files changed (1) hide show
  1. pages/Statistics.py +29 -23
pages/Statistics.py CHANGED
@@ -6,8 +6,11 @@ import plotly.express as px
6
  import plotly.graph_objects as go
7
  import re
8
  from datetime import datetime
9
- from utils.functions import get_stats, get_history, get_phone_info
10
- import logging
 
 
 
11
 
12
  # Definiowanie t艂umacze艅 dla zak艂adki "Statystyki"
13
  page_translations = {
@@ -70,8 +73,8 @@ def main(language):
70
  history = get_history()
71
 
72
  # Kluczowe metryki
73
- total_analyses = stats.get("total_analyses", 0)
74
- total_frauds_detected = stats.get("total_frauds_detected", 0)
75
 
76
  # Wy艣wietlenie metryk
77
  st.title(translations['header'])
@@ -92,19 +95,21 @@ def main(language):
92
  # Wizualizacja procentowego podzia艂u oszustw
93
  fraud_data = [total_frauds_detected, total_analyses - total_frauds_detected]
94
  fraud_labels = ['Fraud', 'Non-Fraud']
95
- colors = ['#FF6347', '#4682B4'] # Kolory: czerwie艅 i niebieski
96
- fig_fraud_pie = go.Figure(data=[go.Pie(labels=fraud_labels, values=fraud_data, hole=.3, marker_colors=colors)])
97
  fig_fraud_pie.update_layout(title_text=translations['fraud_vs_nonfraud'])
98
  st.plotly_chart(fig_fraud_pie, use_container_width=True)
99
 
100
  # Wy艣wietlenie historii analiz w tabeli
101
  if history:
102
  st.markdown(f"### {translations['history_title']}")
 
103
  df_history = pd.DataFrame(history)
104
 
105
- # Konwersja timestamp na dat臋 i dodanie kolumny 'date'
106
  df_history['timestamp'] = pd.to_datetime(df_history['timestamp'])
107
- df_history['date'] = df_history['timestamp'].dt.date # Dodanie kolumny date
 
 
108
 
109
  # Wy艣wietlenie tabeli historii
110
  st.dataframe(df_history[['timestamp', 'phone_number', 'risk_assessment']], height=300)
@@ -112,7 +117,7 @@ def main(language):
112
  # Wykres ko艂owy dla ocen ryzyka
113
  st.markdown(f"### {translations['risk_distribution']}")
114
 
115
- # U偶ycie wyra偶enia regularnego do wyodr臋bnienia liczby z tekstu oceny ryzyka
116
  def extract_risk_score(risk_assessment):
117
  match = re.search(r'(\d+)/10', risk_assessment)
118
  return int(match.group(1)) if match else 0
@@ -138,28 +143,30 @@ def main(language):
138
  )
139
  st.plotly_chart(fig_heatmap, use_container_width=True)
140
 
141
- # Dodanie mapy Europy (wymaga danych kraj贸w dla numer贸w telefon贸w)
142
  st.markdown(f"### {translations['fraud_country_distribution']}")
143
- # Dodanie kolumny 'country' poprzez analiz臋 numer贸w telefon贸w
144
- def get_country(phone_number):
145
- country, _ = get_phone_info(phone_number)
146
- return country if country else 'Unknown'
147
 
148
- df_history['country'] = df_history['phone_number'].apply(get_country)
 
 
 
149
 
150
- country_data = df_history['country'].value_counts().reset_index()
151
- country_data.columns = ['country', 'counts']
 
152
 
153
- # Sprawdzenie, czy s膮 dane o krajach
154
- if country_data['country'].nunique() > 0 and 'Unknown' not in country_data['country'].values:
 
 
 
155
  fig_map = px.choropleth(
156
  country_data,
157
  locations='country',
158
  locationmode='country names',
159
  color='counts',
160
  title=translations['fraud_country_distribution'],
161
- color_continuous_scale=px.colors.sequential.Plasma,
162
- labels={'counts': 'Number of Frauds'}
163
  )
164
  st.plotly_chart(fig_map, use_container_width=True)
165
  else:
@@ -167,5 +174,4 @@ def main(language):
167
 
168
  else:
169
  st.info(translations['no_data'])
170
-
171
- # Nie dodawaj "if __name__ == '__main__':" w podstronach
 
6
  import plotly.graph_objects as go
7
  import re
8
  from datetime import datetime
9
+
10
+ from utils.functions import (
11
+ get_stats,
12
+ get_history
13
+ )
14
 
15
  # Definiowanie t艂umacze艅 dla zak艂adki "Statystyki"
16
  page_translations = {
 
73
  history = get_history()
74
 
75
  # Kluczowe metryki
76
+ total_analyses = stats["total_analyses"]
77
+ total_frauds_detected = stats["total_frauds_detected"]
78
 
79
  # Wy艣wietlenie metryk
80
  st.title(translations['header'])
 
95
  # Wizualizacja procentowego podzia艂u oszustw
96
  fraud_data = [total_frauds_detected, total_analyses - total_frauds_detected]
97
  fraud_labels = ['Fraud', 'Non-Fraud']
98
+ fig_fraud_pie = go.Figure(data=[go.Pie(labels=fraud_labels, values=fraud_data, hole=.3, marker_colors=['#FF6347', '#4682B4'])])
 
99
  fig_fraud_pie.update_layout(title_text=translations['fraud_vs_nonfraud'])
100
  st.plotly_chart(fig_fraud_pie, use_container_width=True)
101
 
102
  # Wy艣wietlenie historii analiz w tabeli
103
  if history:
104
  st.markdown(f"### {translations['history_title']}")
105
+ # Tworzenie DataFrame z listy s艂ownik贸w
106
  df_history = pd.DataFrame(history)
107
 
108
+ # Upewnij si臋, 偶e 'timestamp' jest w formacie datetime
109
  df_history['timestamp'] = pd.to_datetime(df_history['timestamp'])
110
+
111
+ # Dodanie kolumny 'date' dla wizualizacji
112
+ df_history['date'] = df_history['timestamp'].dt.date
113
 
114
  # Wy艣wietlenie tabeli historii
115
  st.dataframe(df_history[['timestamp', 'phone_number', 'risk_assessment']], height=300)
 
117
  # Wykres ko艂owy dla ocen ryzyka
118
  st.markdown(f"### {translations['risk_distribution']}")
119
 
120
+ # Wyodr臋bnienie ocen ryzyka
121
  def extract_risk_score(risk_assessment):
122
  match = re.search(r'(\d+)/10', risk_assessment)
123
  return int(match.group(1)) if match else 0
 
143
  )
144
  st.plotly_chart(fig_heatmap, use_container_width=True)
145
 
146
+ # Wizualizacja rozk艂adu oszustw wed艂ug kraj贸w
147
  st.markdown(f"### {translations['fraud_country_distribution']}")
 
 
 
 
148
 
149
+ # Dodanie informacji o kraju do historii (je艣li dost臋pne)
150
+ # Je艣li nie masz danych o kraju, mo偶esz je doda膰 poprzez pobranie informacji o numerze telefonu
151
+ # Dodamy teraz prost膮 funkcj臋, kt贸ra doda kraj na podstawie numeru telefonu
152
+ from utils.functions import get_phone_info
153
 
154
+ def add_country_info(row):
155
+ country, _ = get_phone_info(row['phone_number'])
156
+ return country
157
 
158
+ df_history['country'] = df_history.apply(add_country_info, axis=1)
159
+
160
+ if 'country' in df_history.columns and df_history['country'].notnull().any():
161
+ country_data = df_history['country'].value_counts().reset_index()
162
+ country_data.columns = ['country', 'counts']
163
  fig_map = px.choropleth(
164
  country_data,
165
  locations='country',
166
  locationmode='country names',
167
  color='counts',
168
  title=translations['fraud_country_distribution'],
169
+ color_continuous_scale=px.colors.sequential.Plasma
 
170
  )
171
  st.plotly_chart(fig_map, use_container_width=True)
172
  else:
 
174
 
175
  else:
176
  st.info(translations['no_data'])
177
+