🐛 修复下载历史重复计数
This commit is contained in:
+14
-1
@@ -3,7 +3,7 @@ import json
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from core.response import APIResponse
|
||||
from core.storage import FileStorageInterface, storages
|
||||
@@ -82,6 +82,18 @@ class FileService:
|
||||
def _file_metadata_key(self, file_id: int) -> str:
|
||||
return f"{self.FILE_METADATA_KEY_PREFIX}{file_id}"
|
||||
|
||||
def _dedupe_download_stats(self, stats: List[TransferStats]) -> List[TransferStats]:
|
||||
deduped: List[TransferStats] = []
|
||||
recent_downloads: Dict[Tuple[Optional[int], str, int], datetime] = {}
|
||||
for stat in stats:
|
||||
key = (stat.file_code_id, stat.ip or "", int(stat.size or 0))
|
||||
previous = recent_downloads.get(key)
|
||||
if previous and (stat.created_at - previous).total_seconds() <= 5:
|
||||
continue
|
||||
recent_downloads[key] = stat.created_at
|
||||
deduped.append(stat)
|
||||
return deduped
|
||||
|
||||
async def _delete_file_code(self, file_code: FileCodes):
|
||||
try:
|
||||
name = (
|
||||
@@ -1289,6 +1301,7 @@ class FileService:
|
||||
download_stats = await TransferStats.filter(
|
||||
action="download", file_code_id=file_code.id
|
||||
).order_by("created_at")
|
||||
download_stats = self._dedupe_download_stats(download_stats)
|
||||
timeline.append(
|
||||
{
|
||||
"key": "retrieved",
|
||||
|
||||
Reference in New Issue
Block a user