129 lines
5.5 KiB
Python
129 lines
5.5 KiB
Python
# /opt/docker/dev/service_finder/backend/app/diagnose_system.py
|
||
import asyncio
|
||
import sys
|
||
import logging
|
||
from sqlalchemy import text, select, func
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
||
# MB2.0 Importok
|
||
try:
|
||
from app.core.config import settings
|
||
from app.database import AsyncSessionLocal, engine
|
||
from app.services.translation_service import translation_service
|
||
from app.models.system import SystemParameter
|
||
from app.models.identity import User
|
||
from app.models.organization import Organization
|
||
from app.models.asset import AssetCatalog
|
||
from app.models.vehicle_definitions import VehicleModelDefinition
|
||
except ImportError as e:
|
||
print(f"❌ Kritikus import hiba: {e}")
|
||
print("Győződj meg róla, hogy a PYTHONPATH tartalmazza a /backend mappát!")
|
||
sys.exit(1)
|
||
|
||
# Naplózás kikapcsolása a tiszta diagnosztikai kimenetért
|
||
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)
|
||
|
||
async def diagnose():
|
||
print("\n" + "═"*50)
|
||
print("🛰️ SENTINEL SYSTEM DIAGNOSTICS - MB2.0 (2026)")
|
||
print("═"*50 + "\n")
|
||
|
||
async with AsyncSessionLocal() as session:
|
||
# --- 1. CSATLAKOZÁS ÉS ADATBÁZIS PING ---
|
||
print("1️⃣ Kapcsolódási teszt...")
|
||
try:
|
||
await session.execute(text("SELECT 1"))
|
||
print(" [✅ OK] PostgreSQL aszinkron kapcsolat aktív.")
|
||
except Exception as e:
|
||
print(f" [❌ HIBA] Nem sikerült kapcsolódni az adatbázishoz: {e}")
|
||
return
|
||
|
||
# --- 2. SÉMA INTEGRITÁS (MB2.0 Specifikus) ---
|
||
print("\n2️⃣ Séma integritás ellenőrzése (Master Data)...")
|
||
tables_to_check = [
|
||
("identity.users", ["preferred_language", "scope_id", "is_active"]),
|
||
("data.organizations", ["org_type", "folder_slug", "is_active"]),
|
||
("data.assets", ["owner_org_id", "catalog_id", "vin"]),
|
||
("data.asset_catalog", ["make", "model", "factory_data"]),
|
||
("data.vehicle_model_definitions", ["status", "raw_search_context"])
|
||
]
|
||
|
||
for table, columns in tables_to_check:
|
||
try:
|
||
schema, table_name = table.split('.')
|
||
query = text(f"""
|
||
SELECT column_name FROM information_schema.columns
|
||
WHERE table_schema = '{schema}' AND table_name = '{table_name}';
|
||
""")
|
||
res = await session.execute(query)
|
||
existing_cols = [row[0] for row in res.fetchall()]
|
||
|
||
if not existing_cols:
|
||
print(f" [❌ HIBA] A tábla nem létezik: {table}")
|
||
continue
|
||
|
||
missing = [c for c in columns if c not in existing_cols]
|
||
if not missing:
|
||
print(f" [✅ OK] {table} (Minden mező a helyén)")
|
||
else:
|
||
print(f" [⚠️ HIÁNY] {table} - Hiányzó mezők: {', '.join(missing)}")
|
||
except Exception as e:
|
||
print(f" [❌ HIBA] Hiba a(z) {table} ellenőrzésekor: {e}")
|
||
|
||
# --- 3. RENDSZER PARAMÉTEREK ---
|
||
print("\n3️⃣ System Parameters (Sentinel Config) ellenőrzése...")
|
||
try:
|
||
res = await session.execute(select(SystemParameter))
|
||
params = res.scalars().all()
|
||
if params:
|
||
print(f" [✅ OK] Talált paraméterek: {len(params)} db")
|
||
critical_keys = ["SECURITY_MAX_RECORDS_PER_HOUR", "VEHICLE_LIMIT"]
|
||
existing_keys = [p.key for p in params]
|
||
for ck in critical_keys:
|
||
status = "✔️" if ck in existing_keys else "❌"
|
||
print(f" {status} {ck}")
|
||
else:
|
||
print(" [⚠️ FIGYELEM] A system_parameters tábla üres! Futtasd a seedert.")
|
||
except Exception as e:
|
||
print(f" [❌ HIBA] SystemParameter lekérdezési hiba: {e}")
|
||
|
||
# --- 4. i18n ÉS CACHE MOTOR ---
|
||
print("\n4️⃣ Nyelvi motor és i18n Cache ellenőrzése...")
|
||
try:
|
||
# Cache betöltése manuálisan a diagnosztikához
|
||
await translation_service.load_cache(session)
|
||
|
||
test_key = "COMMON.SAVE"
|
||
test_val = translation_service.get_text(test_key, "hu")
|
||
|
||
if test_val != f"[{test_key}]":
|
||
print(f" [✅ OK] Fordítás sikeres (HU): {test_key} -> '{test_val}'")
|
||
else:
|
||
print(f" [❌ HIBA] A fordítás nem működik. Nincs betöltött adat az adatbázisban.")
|
||
except Exception as e:
|
||
print(f" [❌ HIBA] Nyelvi motor hiba: {e}")
|
||
|
||
# --- 5. ROBOT ELŐKÉSZÜLETEK (MDM) ---
|
||
print("\n5️⃣ Robot Pipeline (MDM Staging) állapot...")
|
||
try:
|
||
res_hunter = await session.execute(
|
||
select(func.count(VehicleModelDefinition.id)).where(VehicleModelDefinition.status == 'unverified')
|
||
)
|
||
unverified_count = res_hunter.scalar()
|
||
|
||
res_gold = await session.execute(
|
||
select(func.count(AssetCatalog.id))
|
||
)
|
||
gold_count = res_gold.scalar()
|
||
|
||
print(f" [📊 ADAT] Staging rekordok (Hunter): {unverified_count} db")
|
||
print(f" [📊 ADAT] Arany rekordok (Catalog): {gold_count} db")
|
||
except Exception as e:
|
||
print(f" [❌ HIBA] Robot-statisztika hiba: {e}")
|
||
|
||
print("\n" + "═"*50)
|
||
print("🏁 DIAGNOSZTIKA BEFEJEZŐDÖTT")
|
||
print("═"*50 + "\n")
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(diagnose()) |