refactor: improve startup and sync performance

This commit is contained in:
3252a8
2026-05-20 23:04:29 +03:00
parent 8192eaf55b
commit a7f298743d
12 changed files with 973 additions and 97 deletions
+59
View File
@@ -0,0 +1,59 @@
import unittest
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import bot.app.web.subscription_webapp # noqa: F401
from bot.app.web.admin_api_impl import stats as stats_module
class AdminPanelStatsCacheTests(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
stats_module._ADMIN_PANEL_STATS_CACHES.clear()
async def asyncTearDown(self):
stats_module._ADMIN_PANEL_STATS_CACHES.clear()
def _settings(self):
return SimpleNamespace(
ADMIN_PANEL_STATS_CACHE_TTL_SECONDS=15,
REDIS_URL="redis://redis:6379/0",
REDIS_KEY_PREFIX="shop",
)
def _panel_service(self):
return SimpleNamespace(
get_system_stats=AsyncMock(return_value={"users": {"totalUsers": 10}}),
get_bandwidth_stats=AsyncMock(return_value={"current": 123}),
get_nodes_statistics=AsyncMock(return_value={"nodes": []}),
get_nodes_bandwidth_usage=AsyncMock(return_value={"topNodes": []}),
get_nodes_online_lookups=AsyncMock(return_value={"byUuid": {}, "byName": {}}),
)
async def test_admin_panel_stats_are_cached_between_requests(self):
settings = self._settings()
panel_service = self._panel_service()
cache_store = {}
async def fake_get(_settings, key):
return cache_store.get(key)
async def fake_set(_settings, key, value, ttl):
cache_store[key] = value
with (
patch("bot.infra.redis.cache_get_json", fake_get),
patch("bot.infra.redis.cache_set_json", fake_set),
):
first = await stats_module._load_admin_panel_stats(None, settings, panel_service)
second = await stats_module._load_admin_panel_stats(None, settings, panel_service)
self.assertEqual(first, second)
panel_service.get_system_stats.assert_awaited_once()
panel_service.get_bandwidth_stats.assert_awaited_once()
panel_service.get_nodes_statistics.assert_awaited_once()
panel_service.get_nodes_bandwidth_usage.assert_awaited_once()
panel_service.get_nodes_online_lookups.assert_awaited_once()
if __name__ == "__main__":
unittest.main()