version 1.2.2: исправлен парсинг статистики трафика

- app.py: get_traffic_stats() — позиционный парсинг 8 колонок mita get users, без сканирования суффиксов
- bot.py: cmd_users, cmd_dashboard — единый позиционный парсинг вместо поиска единиц
- README: обновлена дорожная карта
This commit is contained in:
2026-06-26 23:48:32 +03:00
parent 667a0957ac
commit 49db39c877
3 changed files with 49 additions and 69 deletions
+26 -27
View File
@@ -149,26 +149,25 @@ async def cmd_users(update: Update, context: ContextTypes.DEFAULT_TYPE):
stats_map = {}
for line in data_lines:
parts = re.split(r"\s{2,}", line.strip())
if len(parts) < 3:
parts = line.split()
if len(parts) < 3:
parts = line.split()
if len(parts) < 8:
continue
try:
name = parts[0]
traffic_parts = [p for p in parts[1:] if any(u in p for u in ["TiB","GiB","MiB","KiB","iB","B"])]
d1 = d30 = 0.0
if len(traffic_parts) >= 6:
d1 = _parse_traffic(traffic_parts[0]) + _parse_traffic(traffic_parts[1])
d30= _parse_traffic(traffic_parts[4]) + _parse_traffic(traffic_parts[5])
elif len(traffic_parts) >= 4:
d1 = _parse_traffic(traffic_parts[0]) + _parse_traffic(traffic_parts[1])
d30= _parse_traffic(traffic_parts[2]) + _parse_traffic(traffic_parts[3])
elif len(traffic_parts) >= 2:
d30= _parse_traffic(traffic_parts[0]) + _parse_traffic(traffic_parts[1])
d1_down = _parse_traffic(parts[2])
d1_up = _parse_traffic(parts[3])
d7_down = _parse_traffic(parts[4])
d7_up = _parse_traffic(parts[5])
d30_down = _parse_traffic(parts[6])
d30_up = _parse_traffic(parts[7])
d1_bytes = d1_down + d1_up
d7_bytes = d7_down + d7_up
d30_bytes = d30_down + d30_up
online = False
la_raw = parts[1].strip() if len(parts) > 1 else ""
la_raw = parts[1]
if la_raw and la_raw.lower() not in ("never","-","n/a","никогда"):
try:
la_date = datetime.strptime(la_raw[:10], "%Y-%m-%d").date()
@@ -177,8 +176,10 @@ async def cmd_users(update: Update, context: ContextTypes.DEFAULT_TYPE):
except Exception:
pass
stats_map[name] = {"online": online, "day_mb": round(d1/1024/1024, 2),
"month_mb": round(d30/1024/1024, 2)}
stats_map[name] = {"online": online,
"day_mb": round(d1_bytes/1024/1024, 2),
"week_mb": round(d7_bytes/1024/1024, 2),
"month_mb": round(d30_bytes/1024/1024, 2)}
except Exception:
continue
@@ -346,18 +347,16 @@ async def cmd_dashboard(update: Update, context: ContextTypes.DEFAULT_TYPE):
traffic = None
try:
raw = mita_cmd("get", "users")
d_lines = [l.strip() for l in raw.splitlines() if l.strip() and not l.upper().startswith("USER")]
week_total = month_total = 0.0
for line in d_lines:
parts = re.split(r"\s{2,}", line.strip())
if len(parts) < 3:
parts = line.split()
if len(parts) < 3:
for line in raw.splitlines():
line = line.strip()
if not line or line.upper().startswith("USER"):
continue
tp = [p for p in parts[1:] if any(u in p for u in ["TiB","GiB","MiB","KiB","iB","B"])]
if len(tp) >= 6:
week_total += _parse_traffic(tp[2]) + _parse_traffic(tp[3])
month_total += _parse_traffic(tp[4]) + _parse_traffic(tp[5])
parts = line.split()
if len(parts) < 8:
continue
week_total += _parse_traffic(parts[4]) + _parse_traffic(parts[5])
month_total += _parse_traffic(parts[6]) + _parse_traffic(parts[7])
traffic = (round(week_total/1024**3, 2), round(month_total/1024**3, 2))
except Exception:
pass