feat:重构UI
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
from datetime import datetime
|
||||
import redis.asyncio as redis
|
||||
@@ -30,21 +31,26 @@ class StatsService:
|
||||
):
|
||||
if not self.redis:
|
||||
return
|
||||
now = datetime.utcnow()
|
||||
date = now.strftime("%Y-%m-%d")
|
||||
hour = now.hour
|
||||
key = self._get_key(provider_id, date, hour)
|
||||
try:
|
||||
now = datetime.utcnow()
|
||||
date = now.strftime("%Y-%m-%d")
|
||||
hour = now.hour
|
||||
key = self._get_key(provider_id, date, hour)
|
||||
|
||||
pipe = self.redis.pipeline()
|
||||
pipe.hincrby(key, "request_count", 1)
|
||||
pipe.hincrby(key, "input_tokens", input_tokens)
|
||||
pipe.hincrby(key, "output_tokens", output_tokens)
|
||||
if cached:
|
||||
pipe.hincrby(key, "cached_count", 1)
|
||||
if error:
|
||||
pipe.hincrby(key, "error_count", 1)
|
||||
pipe.expire(key, 86400 * 30)
|
||||
await pipe.execute()
|
||||
pipe = self.redis.pipeline()
|
||||
pipe.hincrby(key, "request_count", 1)
|
||||
pipe.hincrby(key, "input_tokens", input_tokens)
|
||||
pipe.hincrby(key, "output_tokens", output_tokens)
|
||||
if cached:
|
||||
pipe.hincrby(key, "cached_count", 1)
|
||||
if error:
|
||||
pipe.hincrby(key, "error_count", 1)
|
||||
pipe.expire(key, 86400 * 30)
|
||||
await pipe.execute()
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
except Exception:
|
||||
return
|
||||
|
||||
async def get_stats(self, provider_id: int, date: str) -> dict:
|
||||
if not self.redis:
|
||||
@@ -61,7 +67,12 @@ class StatsService:
|
||||
|
||||
for hour in range(24):
|
||||
key = self._get_key(provider_id, date, hour)
|
||||
data = await self.redis.hgetall(key)
|
||||
try:
|
||||
data = await self.redis.hgetall(key)
|
||||
except asyncio.CancelledError:
|
||||
return result
|
||||
except Exception:
|
||||
data = {}
|
||||
hourly = {
|
||||
"hour": hour,
|
||||
"request_count": int(data.get(b"request_count", 0)),
|
||||
@@ -79,27 +90,37 @@ class StatsService:
|
||||
async def get_rpm_tpm(self, provider_id: int) -> dict:
|
||||
if not self.redis:
|
||||
return {"rpm": 0, "tpm": 0}
|
||||
now = datetime.utcnow()
|
||||
minute_key = f"rpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
rpm = int(await self.redis.get(minute_key) or 0)
|
||||
try:
|
||||
now = datetime.utcnow()
|
||||
minute_key = f"rpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
rpm = int(await self.redis.get(minute_key) or 0)
|
||||
|
||||
tpm_key = f"tpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
tpm = int(await self.redis.get(tpm_key) or 0)
|
||||
return {"rpm": rpm, "tpm": tpm}
|
||||
tpm_key = f"tpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
tpm = int(await self.redis.get(tpm_key) or 0)
|
||||
return {"rpm": rpm, "tpm": tpm}
|
||||
except asyncio.CancelledError:
|
||||
return {"rpm": 0, "tpm": 0}
|
||||
except Exception:
|
||||
return {"rpm": 0, "tpm": 0}
|
||||
|
||||
async def incr_rpm_tpm(self, provider_id: int, tokens: int):
|
||||
if not self.redis:
|
||||
return
|
||||
now = datetime.utcnow()
|
||||
minute_key = f"rpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
tpm_key = f"tpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
try:
|
||||
now = datetime.utcnow()
|
||||
minute_key = f"rpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
tpm_key = f"tpm:{provider_id}:{now.strftime('%Y-%m-%d-%H-%M')}"
|
||||
|
||||
pipe = self.redis.pipeline()
|
||||
pipe.incr(minute_key)
|
||||
pipe.expire(minute_key, 120)
|
||||
pipe.incrby(tpm_key, tokens)
|
||||
pipe.expire(tpm_key, 120)
|
||||
await pipe.execute()
|
||||
pipe = self.redis.pipeline()
|
||||
pipe.incr(minute_key)
|
||||
pipe.expire(minute_key, 120)
|
||||
pipe.incrby(tpm_key, tokens)
|
||||
pipe.expire(tpm_key, 120)
|
||||
await pipe.execute()
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
stats_service = StatsService()
|
||||
|
||||
Reference in New Issue
Block a user