update analytics

This commit is contained in:
lelo 2025-03-23 18:36:56 +01:00
parent 0e50f519ff
commit 86daea355f
3 changed files with 20 additions and 10 deletions

View File

@ -185,11 +185,22 @@ def dashboard():
# Initialize GeoIP2 reader once for efficiency
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
location_data = []
location_data = {}
for ip, count in ip_rows:
country, city = lookup_location(ip, reader)
location_data.append(dict(count=count, country=country, city=city))
key = (country, city)
if key in location_data:
location_data[key] += count
else:
location_data[key] = count
reader.close()
# Convert the dictionary to a list of dictionaries
location_data = [
dict(country=key[0], city=key[1], count=value)
for key, value in location_data.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]

9
app.py
View File

@ -324,10 +324,10 @@ def query_recent_connections():
rows = a.return_file_access()
connections = [
{
'timestamp': row[0],
'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'),
'full_path': row[1],
'ip_address': row[2],
'user_agent': row[3],
'referrer': row[4]
'user_agent': row[3]
}
for row in rows
]
@ -365,8 +365,7 @@ def handle_request_initial_data():
'timestamp': row[0],
'full_path': row[1],
'ip_address': row[2],
'user_agent': row[3],
'referrer': row[4]
'user_agent': row[3]
}
for row in rows
]

View File

@ -26,7 +26,7 @@
</head>
<body>
<div class="container-fluid">
<h1 class="mb-4">Downloads in den letzten 10 Minute</h1>
<h1 class="mb-4">Initiale downloads in den letzten 10 Minute</h1>
<div class="mb-3">
<a href="{{ url_for('index') }}" class="btn btn-primary">App</a>
<a href="{{ url_for('mylinks') }}" class="btn btn-primary">Meine Links</a>
@ -40,7 +40,7 @@
<th>Timestamp</th>
<th>IP Address</th>
<th>User Agent</th>
<th>Referrer</th>
<th>File Path</th>
</tr>
</thead>
<tbody id="connectionsTableBody">
@ -70,7 +70,7 @@
<td>${record.timestamp}</td>
<td>${record.ip_address}</td>
<td>${record.user_agent}</td>
<td>${record.referrer}</td>
<td>${record.full_path}</td>
`;
tbody.appendChild(row);
});