diff --git a/apps/admin/views.py b/apps/admin/views.py index 58390ac..83e3c9d 100644 --- a/apps/admin/views.py +++ b/apps/admin/views.py @@ -275,6 +275,24 @@ async def analytics(start: str = "", end: str = ""): bucket["size"] = bucket["size"] or int(file_item.size or 0) bucket["uploaded_at"] = bucket["uploaded_at"] or file_item.created_at bucket["expired_at"] = bucket["expired_at"] or file_item.expired_at + created_key = ( + file_item.created_at.strftime("%Y-%m-%d") if file_item.created_at else "" + ) + created_row = day_map.get(created_key) + if bucket["uploads"] <= 0: + bucket["uploads"] = 1 + bucket["uploadTraffic"] = int(file_item.size or 0) + if created_row: + created_row["uploads"] += 1 + created_row["uploadTraffic"] += int(file_item.size or 0) + missing_downloads = max(0, int(file_item.used_count or 0) - bucket["downloads"]) + if missing_downloads: + traffic = missing_downloads * int(file_item.size or 0) + bucket["downloads"] += missing_downloads + bucket["downloadTraffic"] += traffic + if created_row: + created_row["downloads"] += missing_downloads + created_row["downloadTraffic"] += traffic def iso(value): return value.isoformat() if value else "" @@ -320,6 +338,7 @@ async def analytics(start: str = "", end: str = ""): key=lambda kv: (kv[1].get("downloads") or 0, kv[1].get("downloadTraffic") or 0), reverse=True, )[:5] + daily_rows = list(day_map.values()) return APIResponse( detail={ @@ -328,21 +347,21 @@ async def analytics(start: str = "", end: str = ""): "end": end_day.strftime("%Y-%m-%d"), }, "totals": { - "totalDownloads": sum(1 for item in stats if item.action == "download"), + "totalDownloads": sum(row["downloads"] for row in daily_rows), "downloadedFiles": len( - {item.code for item in stats if item.action == "download" and item.code} + [item for item in file_stats.values() if (item.get("downloads") or 0) > 0] ), "downloadTraffic": str( - sum(item.size or 0 for item in stats if item.action == "download") + sum(row["downloadTraffic"] for row in daily_rows) ), "historicalFiles": len(file_stats), "currentFiles": len(files), - "totalUploads": sum(1 for item in stats if item.action == "upload"), + "totalUploads": sum(row["uploads"] for row in daily_rows), "uploadTraffic": str( - sum(item.size or 0 for item in stats if item.action == "upload") + sum(row["uploadTraffic"] for row in daily_rows) ), }, - "daily": list(day_map.values()), + "daily": daily_rows, "topFiles": [history_row(code, data) for code, data in top_files], "historyFiles": [history_row(code, data) for code, data in history], }