Skip to content

Commit d4a4d84

Browse files
committed
fix: 修复统计出现错误的情况
1 parent 65ced34 commit d4a4d84

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

core/dashboard.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
from collections import deque
44
from dataclasses import dataclass
55
import io
6-
import json
76
import os
87
from pathlib import Path
98
import socket
10-
from typing import Any, Optional
9+
from typing import Optional
1110

1211
import aiohttp
1312
import psutil
1413

15-
from core import cache, cluster, config, logger
14+
from core import cluster, config, logger
1615

1716
from . import utils
1817

@@ -23,6 +22,8 @@
2322
)
2423
from aiohttp import web
2524

25+
from . import database as db
26+
2627
@dataclass
2728
class CollectionUsage:
2829
total: int
@@ -125,6 +126,24 @@ async def _(request: web.Request):
125126
async def _(request: web.Request):
126127
return web.json_response(counter.get_json())
127128

129+
@route.get("/api/count")
130+
async def _(request: web.Request):
131+
# statistics of the cluster hits and bytes
132+
session = db.SESSION.get_session()
133+
current_hour = db.get_hour()
134+
hour_of_day = (current_hour // 24) * 24
135+
next_hour = hour_of_day + 24
136+
q = session.query(db.ClusterStatisticsTable).filter(
137+
db.ClusterStatisticsTable.hour >= hour_of_day,
138+
db.ClusterStatisticsTable.hour < next_hour
139+
).all()
140+
print(hour_of_day, next_hour)
141+
return web.json_response({
142+
"hits": sum([int(item.hits) for item in q]),
143+
"bytes": sum([int(item.bytes) for item in q])
144+
})
145+
146+
128147
@route.get("/api/openbmclapi/rank")
129148
async def _(request: web.Request):
130149
async with aiohttp.ClientSession(

core/database.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ def commit():
223223
total_bytes = 0
224224
total_storages = 0
225225
cache = FILE_CACHE.copy()
226+
response_cache = RESPONSE_CACHE.copy()
226227
session = SESSION.get_session()
227228
clusters: defaultdict[tuple[int, str], FileStatistics] = defaultdict(lambda: FileStatistics(0, 0))
228-
for key, value in FILE_CACHE.items():
229+
for key, value in cache.items():
229230
hour = key.hour
230231
cluster = key.cluster_id
231232
storage = key.storage_id
@@ -240,7 +241,7 @@ def commit():
240241
for cluster, value in clusters.items():
241242
_commit_cluster(cluster[0], cluster[1], value.hits, value.bytes)
242243

243-
for hour, value in RESPONSE_CACHE.items():
244+
for hour, value in response_cache.items():
244245
_commit_response(hour, value.ip_tables, value.success, value.forbidden, value.redirect, value.not_found, value.error)
245246

246247
session.commit()
@@ -255,7 +256,7 @@ def commit():
255256

256257
old_keys.clear()
257258

258-
for hour, value in RESPONSE_CACHE.items():
259+
for hour, value in response_cache.items():
259260
RESPONSE_CACHE[hour].success -= value.success
260261
RESPONSE_CACHE[hour].forbidden -= value.forbidden
261262
RESPONSE_CACHE[hour].redirect -= value.redirect

0 commit comments

Comments
 (0)