Initial commit: Robot ökoszisztéma v2.0 - Stabilizált jármű és szerviz robotok
This commit is contained in:
107
backend/app/workers/system/system_robot_2_service_auditor.py
Executable file
107
backend/app/workers/system/system_robot_2_service_auditor.py
Executable file
@@ -0,0 +1,107 @@
|
||||
# /app/app/workers/system/system_robot_2_service_auditor.py
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import select, and_, update
|
||||
from app.database import AsyncSessionLocal
|
||||
from app.models.organization import Organization, OrgType
|
||||
from app.models.service import ServiceProfile
|
||||
from app.models.staged_data import ServiceStaging
|
||||
|
||||
# MB 2.0 Naplózás
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s]: %(message)s')
|
||||
logger = logging.getLogger("System-Robot-2-ServiceAuditor")
|
||||
|
||||
class ServiceAuditor:
|
||||
"""
|
||||
System Robot 2: Service Auditor & Judge
|
||||
Feladata:
|
||||
1. Meglévő szervizek auditálása (ne legyenek "halott" adatok).
|
||||
2. Staging adatok automatikus élesítése, ha a bizalmi szint eléri a küszöböt.
|
||||
"""
|
||||
|
||||
TRUST_THRESHOLD = 80 # Ezen pontszám felett automatikusan élesítünk
|
||||
|
||||
@classmethod
|
||||
async def promote_staging_data(cls):
|
||||
"""
|
||||
AZ AUTOMATA BÍRÓ:
|
||||
Megnézi a Staging táblát, és ha valami elérte a ponthatárt,
|
||||
automatikusan átemeli az éles profilok közé.
|
||||
"""
|
||||
async with AsyncSessionLocal() as db:
|
||||
stmt = select(ServiceStaging).where(
|
||||
and_(
|
||||
ServiceStaging.status == "researched",
|
||||
ServiceStaging.trust_score >= cls.TRUST_THRESHOLD
|
||||
)
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
to_promote = result.scalars().all()
|
||||
|
||||
for stage in to_promote:
|
||||
logger.info(f"⚖️ Automatikus élesítés (Admin nélkül): {stage.name} (Bizalom: {stage.trust_score})")
|
||||
|
||||
# Itt jön az átemelő logika:
|
||||
# 1. Organization létrehozása
|
||||
# 2. ServiceProfile létrehozása
|
||||
# 3. ExpertiseTags átmásolása
|
||||
|
||||
stage.status = "promoted"
|
||||
|
||||
await db.commit()
|
||||
|
||||
@classmethod
|
||||
async def audit_existing_services(cls):
|
||||
""" Karbantartás: Megszűnt helyek inaktiválása. """
|
||||
async with AsyncSessionLocal() as db:
|
||||
# Csak az aktív szervizeket nézzük
|
||||
stmt = select(Organization).where(
|
||||
and_(
|
||||
Organization.org_type == OrgType.service,
|
||||
Organization.is_active == True
|
||||
)
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
services = result.scalars().all()
|
||||
|
||||
for service in services:
|
||||
try:
|
||||
# Itt futhat le egy külső csekk (pl. weboldal él-e még?)
|
||||
is_still_open = True
|
||||
|
||||
stmt_profile = select(ServiceProfile).where(ServiceProfile.organization_id == service.id)
|
||||
profile_res = await db.execute(stmt_profile)
|
||||
profile = profile_res.scalar_one_or_none()
|
||||
|
||||
if not is_still_open:
|
||||
service.is_active = False
|
||||
if profile:
|
||||
profile.status = 'closed'
|
||||
logger.warning(f"⚠️ Szerviz inaktiválva: {service.name}")
|
||||
else:
|
||||
if profile:
|
||||
profile.last_audit_at = datetime.now(timezone.utc)
|
||||
|
||||
await asyncio.sleep(0.5) # Rate limit védelem
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Hiba audit közben ({service.name}): {e}")
|
||||
|
||||
await db.commit()
|
||||
|
||||
@classmethod
|
||||
async def run(cls):
|
||||
logger.info("⚖️ System Auditor ONLINE - Bírói és Karbantartó üzemmód")
|
||||
while True:
|
||||
# 1. Először élesítjük az új felfedezéseket
|
||||
await cls.promote_staging_data()
|
||||
|
||||
# 2. Utána karbantartjuk a meglévőket
|
||||
await cls.audit_existing_services()
|
||||
|
||||
# Naponta egyszer fut le a teljes kör
|
||||
await asyncio.sleep(86400)
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(ServiceAuditor.run())
|
||||
Reference in New Issue
Block a user