fix: normalize daily report layout for Telegram and UI

Ensure server line, compact spacing, and one user per row when ingesting
agent reports or sending SAC-generated daily reports.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
PTah
2026-05-30 09:52:09 +10:00
parent eba2f0d15c
commit 2501ecc4fe
6 changed files with 223 additions and 22 deletions
+44 -4
View File
@@ -3,7 +3,12 @@
from datetime import datetime, timezone
from app.models import Host
from app.services.daily_report_format import build_report_body, enrich_stats_for_storage
from app.services.daily_report_format import (
build_report_body,
enrich_stats_for_storage,
normalize_active_users_list,
normalize_report_body,
)
def test_build_report_body_ssh_matches_agent_layout():
@@ -30,10 +35,12 @@ def test_build_report_body_ssh_matches_agent_layout():
)
body = build_report_body("ssh", host, stats, when, sac_generated=True)
assert "ЕЖЕДНЕВНЫЙ ОТЧЕТ SSH МОНИТОРИНГА" in body
assert "HaProxy Kalina (192.168.160.117)" in body
assert "🖥️ Сервер: HaProxy Kalina (192.168.160.117)" in body
assert " 📈 СТАТИСТИКА" in body
assert "Команд через sudo: 5" in body
assert "АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (1)" in body
assert " 👥 АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (1)" in body
assert "papatramp" in body
assert "\n\n\n" not in body
def test_build_report_body_windows_layout():
@@ -59,5 +66,38 @@ def test_build_report_body_windows_layout():
assert "ЕЖЕДНЕВНЫЙ ОТЧЕТ МОНИТОРИНГА WINDOWS" in body
assert "K6A-DC3 (10.0.0.10)" in body
assert "Успешных RDP подключений: 1" in body
assert "АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (2)" in body
assert " 👥 АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (2)" in body
assert "user2" in body
lines = [ln for ln in body.split("\n") if ln.strip().startswith("👤")]
assert len(lines) == 2
def test_normalize_active_users_list_splits_combined_line():
users = normalize_active_users_list(["👤 k.khodasevich 👤 papatramp"])
assert len(users) == 2
assert users[0].strip().startswith("👤 k.khodasevich")
assert users[1].strip().startswith("👤 papatramp")
def test_normalize_report_body_adds_server_and_collapses_blanks():
host = Host(hostname="srv", display_name="Unimus Kalina", ipv4="192.168.160.17")
raw = "\n".join(
[
"📊 ЕЖЕДНЕВНЫЙ ОТЧЕТ SSH МОНИТОРИНГА",
"",
"",
"🕐 Время отчета: 30.05.2026 09:00:00",
"",
"",
" 📈 СТАТИСТИКА ЗА ПОСЛЕДНИЕ 24 ЧАСА:",
" ✅ Успешных SSH подключений: 0",
"",
" 👥 АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (2):",
" 👤 u1 👤 u2",
]
)
body = normalize_report_body(raw, host, "ssh")
assert "🖥️ Сервер: Unimus Kalina (192.168.160.17)" in body
assert "\n\n\n" not in body
user_lines = [ln for ln in body.split("\n") if ln.strip().startswith("👤")]
assert len(user_lines) == 2