feat add connections management and health summary improvements

This commit is contained in:
2026-03-15 16:40:43 +03:00
parent eba0fab091
commit 027d17f193
17 changed files with 8521 additions and 10819 deletions

View File

@@ -122,6 +122,43 @@ async def test_get_health_summary_resolves_slug_and_title_from_superset():
mock_client.get_dashboards_summary.assert_called_once_with()
@pytest.mark.anyio
async def test_get_health_summary_reuses_dashboard_metadata_cache_across_service_instances():
HealthService._dashboard_summary_cache.clear()
db = MagicMock()
config_manager = MagicMock()
config_manager.get_environments.return_value = [MagicMock(id="env_1")]
record = ValidationRecord(
id="rec-1",
dashboard_id="42",
environment_id="env_1",
status="PASS",
timestamp=datetime.utcnow(),
summary="Healthy",
issues=[],
)
db.query.return_value.join.return_value.all.return_value = [record]
with patch("src.services.health_service.SupersetClient") as mock_client_cls:
mock_client = MagicMock()
mock_client.get_dashboards_summary.return_value = [
{"id": 42, "slug": "ops-overview", "title": "Ops Overview"}
]
mock_client_cls.return_value = mock_client
first_service = HealthService(db, config_manager=config_manager)
second_service = HealthService(db, config_manager=config_manager)
first_summary = await first_service.get_health_summary(environment_id="env_1")
second_summary = await second_service.get_health_summary(environment_id="env_1")
assert first_summary.items[0].dashboard_slug == "ops-overview"
assert second_summary.items[0].dashboard_slug == "ops-overview"
mock_client.get_dashboards_summary.assert_called_once_with()
HealthService._dashboard_summary_cache.clear()
def test_delete_validation_report_deletes_dashboard_scope_and_linked_tasks():
db = MagicMock()
config_manager = MagicMock()

View File

@@ -8,6 +8,7 @@
# @RELATION: DEPENDS_ON -> backend.src.core.task_manager.cleanup.TaskCleanupService
from typing import List, Dict, Any, Optional, Tuple
import time
from sqlalchemy.orm import Session
from sqlalchemy import func, desc
import os
@@ -24,6 +25,9 @@ from ..core.task_manager import TaskManager
# @RELATION: CALLS -> backend.src.core.superset_client.SupersetClient
# @RELATION: CALLS -> backend.src.core.task_manager.cleanup.TaskCleanupService
class HealthService:
_dashboard_summary_cache: Dict[str, Tuple[float, Dict[str, Dict[str, Optional[str]]]]] = {}
_dashboard_summary_cache_ttl_seconds = 60.0
"""
@PURPOSE: Service for managing and querying dashboard health data.
"""
@@ -77,15 +81,27 @@ class HealthService:
continue
try:
dashboards = SupersetClient(env).get_dashboards_summary()
dashboard_meta_map = {
str(item.get("id")): {
"slug": item.get("slug"),
"title": item.get("title"),
cached_meta = self.__class__._dashboard_summary_cache.get(environment_id)
cache_is_fresh = (
cached_meta is not None
and (time.monotonic() - cached_meta[0]) < self.__class__._dashboard_summary_cache_ttl_seconds
)
if cache_is_fresh:
dashboard_meta_map = cached_meta[1]
else:
dashboards = SupersetClient(env).get_dashboards_summary()
dashboard_meta_map = {
str(item.get("id")): {
"slug": item.get("slug"),
"title": item.get("title"),
}
for item in dashboards
if str(item.get("id") or "").strip()
}
for item in dashboards
if str(item.get("id") or "").strip()
}
self.__class__._dashboard_summary_cache[environment_id] = (
time.monotonic(),
dashboard_meta_map,
)
for dashboard_id in dashboard_ids:
self._dashboard_meta_cache[(environment_id, dashboard_id)] = dashboard_meta_map.get(
dashboard_id,