✨ Statut par service dans la Configuration: configure / teste / echec + endpoints de test pour tous les services
This commit is contained in:
1 parent
23132c6219
commit
3a75527135
9 files changed
+409
-59
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
@@ -9,10 +10,20 @@ from pydantic import BaseModel, Field
|
||||
from app.config import settings
|
||||
|
||||
|
||||
class TestState(BaseModel):
|
||||
"""Resultat du dernier test de connexion (None = jamais teste)."""
|
||||
|
||||
at: str | None = None
|
||||
ok: bool | None = None
|
||||
version: str | None = None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
class ApiServiceConfig(BaseModel):
|
||||
url: str | None = None
|
||||
port: int | None = None
|
||||
api_key: str | None = None
|
||||
test: TestState = Field(default_factory=TestState)
|
||||
|
||||
|
||||
class QbittorrentConfig(BaseModel):
|
||||
@@ -20,6 +31,7 @@ class QbittorrentConfig(BaseModel):
|
||||
port: int | None = None
|
||||
username: str | None = None
|
||||
password: str | None = None
|
||||
test: TestState = Field(default_factory=TestState)
|
||||
|
||||
|
||||
def build_base_url(url: str | None, port: int | None = None) -> str | None:
|
||||
@@ -54,6 +66,48 @@ def config_path() -> Path:
|
||||
return settings.config_dir / "conf.json"
|
||||
|
||||
|
||||
SERVICE_NAMES = ("radarr", "sonarr", "prowlarr", "jellyfin", "seerr", "qbittorrent")
|
||||
|
||||
|
||||
def service_configured(service: ApiServiceConfig | QbittorrentConfig) -> bool:
|
||||
"""Un service est completement configure quand son URL (et sa cle API,
|
||||
quand il en faut une) est renseignee."""
|
||||
if isinstance(service, QbittorrentConfig):
|
||||
return bool(service.url)
|
||||
return bool(service.url and service.api_key)
|
||||
|
||||
|
||||
def service_status(service: ApiServiceConfig | QbittorrentConfig) -> str:
|
||||
"""Statut expose a l'UI: unconfigured | configured | tested | error."""
|
||||
if not service_configured(service):
|
||||
return "unconfigured"
|
||||
if service.test.ok is None:
|
||||
return "configured"
|
||||
return "tested" if service.test.ok else "error"
|
||||
|
||||
|
||||
def record_test(
|
||||
service_name: str, ok: bool, version: str | None = None, error: str | None = None
|
||||
) -> None:
|
||||
"""Persiste dans conf.json le resultat du dernier test de connexion."""
|
||||
if service_name not in SERVICE_NAMES:
|
||||
raise ValueError(f"Service inconnu: {service_name}")
|
||||
cfg = read_config()
|
||||
service = getattr(cfg, service_name)
|
||||
service.test = TestState(
|
||||
at=datetime.now(UTC).isoformat(),
|
||||
ok=ok,
|
||||
version=version if ok else None,
|
||||
error=None if ok else error,
|
||||
)
|
||||
write_config(cfg)
|
||||
|
||||
|
||||
def reset_test(service: ApiServiceConfig | QbittorrentConfig) -> None:
|
||||
"""Reinitialise le statut de test quand la configuration change."""
|
||||
service.test = TestState()
|
||||
|
||||
|
||||
def read_config() -> AppConfig:
|
||||
path = config_path()
|
||||
if not path.exists():
|
||||
|
||||
Reference in new issue
Block a user