Backfill analytics from legacy file records
This commit is contained in:
+25
-6
@@ -275,6 +275,24 @@ async def analytics(start: str = "", end: str = ""):
|
|||||||
bucket["size"] = bucket["size"] or int(file_item.size or 0)
|
bucket["size"] = bucket["size"] or int(file_item.size or 0)
|
||||||
bucket["uploaded_at"] = bucket["uploaded_at"] or file_item.created_at
|
bucket["uploaded_at"] = bucket["uploaded_at"] or file_item.created_at
|
||||||
bucket["expired_at"] = bucket["expired_at"] or file_item.expired_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):
|
def iso(value):
|
||||||
return value.isoformat() if value else ""
|
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),
|
key=lambda kv: (kv[1].get("downloads") or 0, kv[1].get("downloadTraffic") or 0),
|
||||||
reverse=True,
|
reverse=True,
|
||||||
)[:5]
|
)[:5]
|
||||||
|
daily_rows = list(day_map.values())
|
||||||
|
|
||||||
return APIResponse(
|
return APIResponse(
|
||||||
detail={
|
detail={
|
||||||
@@ -328,21 +347,21 @@ async def analytics(start: str = "", end: str = ""):
|
|||||||
"end": end_day.strftime("%Y-%m-%d"),
|
"end": end_day.strftime("%Y-%m-%d"),
|
||||||
},
|
},
|
||||||
"totals": {
|
"totals": {
|
||||||
"totalDownloads": sum(1 for item in stats if item.action == "download"),
|
"totalDownloads": sum(row["downloads"] for row in daily_rows),
|
||||||
"downloadedFiles": len(
|
"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(
|
"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),
|
"historicalFiles": len(file_stats),
|
||||||
"currentFiles": len(files),
|
"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(
|
"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],
|
"topFiles": [history_row(code, data) for code, data in top_files],
|
||||||
"historyFiles": [history_row(code, data) for code, data in history],
|
"historyFiles": [history_row(code, data) for code, data in history],
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user