Spaces:
Sleeping
Sleeping
Create usage.py
Browse files- services/usage.py +71 -0
services/usage.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# services/usage.py
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from collections import defaultdict
|
| 7 |
+
|
| 8 |
+
class UsageTracker:
|
| 9 |
+
def __init__(self, daily_limit: int, data_dir: Path = None):
|
| 10 |
+
self.daily_limit = daily_limit
|
| 11 |
+
self.data_dir = data_dir or Path("./data")
|
| 12 |
+
self.data_dir.mkdir(exist_ok=True)
|
| 13 |
+
self.usage_file = self.data_dir / "usage.json"
|
| 14 |
+
self.usage = self._load()
|
| 15 |
+
|
| 16 |
+
def _load(self) -> dict:
|
| 17 |
+
try:
|
| 18 |
+
if self.usage_file.exists():
|
| 19 |
+
with open(self.usage_file, 'r', encoding='utf-8') as f:
|
| 20 |
+
return json.load(f)
|
| 21 |
+
except:
|
| 22 |
+
pass
|
| 23 |
+
return {}
|
| 24 |
+
|
| 25 |
+
def _save(self):
|
| 26 |
+
try:
|
| 27 |
+
with open(self.usage_file, 'w', encoding='utf-8') as f:
|
| 28 |
+
json.dump(self.usage, f, ensure_ascii=False, indent=2)
|
| 29 |
+
except:
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
def _today(self) -> str:
|
| 33 |
+
return datetime.now(timezone.utc).strftime('%Y-%m-%d')
|
| 34 |
+
|
| 35 |
+
def get_count(self, user_id: int) -> int:
|
| 36 |
+
uid = str(user_id)
|
| 37 |
+
today = self._today()
|
| 38 |
+
if uid not in self.usage or self.usage[uid].get('date') != today:
|
| 39 |
+
self.usage[uid] = {'date': today, 'count': 0}
|
| 40 |
+
self._save()
|
| 41 |
+
return 0
|
| 42 |
+
return self.usage[uid].get('count', 0)
|
| 43 |
+
|
| 44 |
+
def check_limit(self, user_id: int) -> bool:
|
| 45 |
+
used = self.get_count(user_id)
|
| 46 |
+
return used < self.daily_limit
|
| 47 |
+
|
| 48 |
+
def increment(self, user_id: int) -> bool:
|
| 49 |
+
if not self.check_limit(user_id):
|
| 50 |
+
return False
|
| 51 |
+
uid = str(user_id)
|
| 52 |
+
today = self._today()
|
| 53 |
+
if uid not in self.usage or self.usage[uid].get('date') != today:
|
| 54 |
+
self.usage[uid] = {'date': today, 'count': 1}
|
| 55 |
+
else:
|
| 56 |
+
self.usage[uid]['count'] = self.usage[uid].get('count', 0) + 1
|
| 57 |
+
self._save()
|
| 58 |
+
return True
|
| 59 |
+
|
| 60 |
+
def get_stats_text(self, user_id: int) -> str:
|
| 61 |
+
used = self.get_count(user_id)
|
| 62 |
+
remaining = self.daily_limit - used
|
| 63 |
+
today = self._today()
|
| 64 |
+
percent = (used / self.daily_limit) * 100 if self.daily_limit > 0 else 0
|
| 65 |
+
bar = "█" * int(20 * percent / 100) + "░" * (20 - int(20 * percent / 100))
|
| 66 |
+
return (
|
| 67 |
+
f"📊 **Статистика**\n"
|
| 68 |
+
f"📅 `{today}`\n"
|
| 69 |
+
f"📈 [{bar}] {percent:.0f}%\n"
|
| 70 |
+
f"✅ `{used}` / ❌ `{remaining}` из `{self.daily_limit}`"
|
| 71 |
+
)
|