diff --git a/analytics.py b/analytics.py index 82e87fd..63408af 100644 --- a/analytics.py +++ b/analytics.py @@ -179,24 +179,20 @@ def dashboard(): WHERE timestamp >= ? GROUP BY ip_address ORDER BY count DESC - LIMIT 20 + LIMIT 1000 ''', (start.isoformat(),)) ip_rows = cursor.fetchall() # Initialize GeoIP2 reader once for efficiency reader = geoip2.database.Reader('GeoLite2-City.mmdb') - ip_data = [] + location_data = [] for ip, count in ip_rows: country, city = lookup_location(ip, reader) - ip_data.append(dict(ip=ip, count=count, country=country, city=city)) + location_data.append(dict(count=count, country=country, city=city)) reader.close() - - # Aggregate by city (ignoring entries without a city) - city_counts = {} - for entry in ip_data: - if entry['city']: - city_counts[entry['city']] = city_counts.get(entry['city'], 0) + entry['count'] - city_data = [dict(city=city, count=count) for city, count in city_counts.items()] + # Sort by count in descending order and take the top 20 + location_data.sort(key=lambda x: x['count'], reverse=True) + location_data = location_data[:20] # Summary stats using separate SQL queries cursor.execute('SELECT COUNT(*) FROM file_access_log WHERE timestamp >= ?', (start.isoformat(),)) @@ -219,8 +215,7 @@ def dashboard(): top_files_data=top_files_data, user_agent_data=user_agent_data, referrer_data=referrer_data, - ip_data=ip_data, - city_data=city_data, + location_data=location_data, total_accesses=total_accesses, unique_files=unique_files, unique_ips=unique_ips) \ No newline at end of file diff --git a/templates/dashboard.html b/templates/dashboard.html index 769c757..242ef59 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -41,7 +41,7 @@
{{ total_accesses }}
{{ unique_files }}
{{ unique_ips }}
| IP Address | -Access Count | -City | -Country | +Anzahl Downloads | +Stadt | +Land |
|---|---|---|---|---|---|---|
| {{ ip.ip }} | -{{ ip.count }} | -{{ ip.city }} | -{{ ip.country }} | +{{ loc.count }} | +{{ loc.city }} | +{{ loc.country }} |
| No IP access data available for the selected timeframe. | -||||||
| City | -Access Count | -||||
|---|---|---|---|---|---|
| {{ city.city }} | -{{ city.count }} | -||||
| No city access data available for the selected timeframe. | +No access data available for the selected timeframe. | ||||