STABLE: Final schema sync, optimized gitignore
This commit is contained in:
@@ -1,125 +1,129 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/workers/technical_enricher.py
|
||||
import asyncio
|
||||
import httpx
|
||||
import logging
|
||||
import os
|
||||
import datetime
|
||||
import random
|
||||
import sys
|
||||
from sqlalchemy import select, and_, update, text, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.db.session import SessionLocal
|
||||
# JAVÍTVA: case hozzáadva az importhoz
|
||||
from sqlalchemy import select, and_, update, text, func, case
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.models.vehicle_definitions import VehicleModelDefinition
|
||||
from app.models.asset import AssetCatalog
|
||||
from app.services.ai_service import AIService
|
||||
from duckduckgo_search import DDGS
|
||||
|
||||
# --- SZIGORÚ NAPLÓZÁS KONFIGURÁCIÓ ---
|
||||
for handler in logging.root.handlers[:]:
|
||||
logging.root.removeHandler(handler)
|
||||
|
||||
# --- SZIGORÚ NAPLÓZÁS ---
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s.%(msecs)03d [%(levelname)s] Alchemist: %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S',
|
||||
format='%(asctime)s [%(levelname)s] Alchemist-v1.3: %(message)s',
|
||||
stream=sys.stdout
|
||||
)
|
||||
logger = logging.getLogger("Robot-Enricher-v1.3.0")
|
||||
logger = logging.getLogger("Robot-Enricher")
|
||||
|
||||
class TechEnricher:
|
||||
"""
|
||||
Industrial TechEnricher v1.3.0
|
||||
- Fix: Deadlock elkerülése izolált session-kezeléssel.
|
||||
- Logika: Napi 500 AI hívás, Smart Merge, Web Fallback.
|
||||
Industrial TechEnricher (Alchemist Bot).
|
||||
Felelős az MDM (Master Data Management) 'Arany' rekordjainak előállításáért.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.max_attempts = 5
|
||||
self.batch_size = 15
|
||||
self.batch_size = 10
|
||||
self.daily_ai_limit = 500
|
||||
self.ai_calls_today = 0
|
||||
self.last_reset_date = datetime.date.today()
|
||||
|
||||
def check_budget(self) -> bool:
|
||||
""" Ellenőrzi, hogy beleférünk-e még a napi AI keretbe. """
|
||||
if datetime.date.today() > self.last_reset_date:
|
||||
self.ai_calls_today = 0
|
||||
self.last_reset_date = datetime.date.today()
|
||||
return self.ai_calls_today < self.daily_ai_limit
|
||||
|
||||
def is_data_sane(self, data: dict) -> bool:
|
||||
""" Technikai józansági vizsgálat (Hallucináció elleni védelem). """
|
||||
try:
|
||||
if not data: return False
|
||||
ccm = int(data.get("ccm", 0) or 0)
|
||||
kw = int(data.get("kw", 0) or 0)
|
||||
if ccm > 15000 or kw > 2000: return False
|
||||
# Extrém értékek szűrése (pl. nem létezik 20 literes személyautó motor)
|
||||
if ccm > 16000 or (kw > 1500 and data.get("vehicle_type") != "truck"):
|
||||
return False
|
||||
return True
|
||||
except: return False
|
||||
|
||||
async def get_web_wisdom(self, make: str, model: str) -> str:
|
||||
"""Keresés a neten izolált szálon (nem blokkolja az aszinkron loopot)."""
|
||||
query = f"{make} {model} technical specs maintenance oil qty tire size"
|
||||
""" Ha az AI bizonytalan, ez a funkció gyűjt kontextust a netről. """
|
||||
query = f"{make} {model} technical specifications oil capacity engine code"
|
||||
try:
|
||||
def sync_search():
|
||||
with DDGS() as ddgs:
|
||||
return "\n".join([r['body'] for r in ddgs.text(query, max_results=3)])
|
||||
# Az első 3 találat body részét gyűjtjük össze
|
||||
results = ddgs.text(query, max_results=3)
|
||||
return "\n".join([r['body'] for r in results]) if results else ""
|
||||
|
||||
return await asyncio.to_thread(sync_search)
|
||||
except Exception as e:
|
||||
logger.warning(f"🌐 Web hiba ({make}): {e}")
|
||||
logger.warning(f"🌐 Web Search Error ({make}): {e}")
|
||||
return ""
|
||||
|
||||
async def process_single_record(self, record_id: int):
|
||||
"""
|
||||
Egyetlen rekord dúsítása izolált folyamatban.
|
||||
Logika: Read -> AI Process -> Write Merge.
|
||||
"""
|
||||
Dúsítási folyamat 3 szigorúan elválasztott lépésben a fagyás ellen:
|
||||
1. Adat lekérése és DB bezárása.
|
||||
2. AI munka (DB nélkül).
|
||||
3. Mentés új sessionben.
|
||||
"""
|
||||
# --- 1. LÉPÉS: ADAT LEKÉRÉSE ---
|
||||
async with SessionLocal() as db:
|
||||
stmt = select(VehicleModelDefinition).where(VehicleModelDefinition.id == record_id)
|
||||
res = await db.execute(stmt)
|
||||
# 1. ADAT LEKÉRÉSE
|
||||
async with AsyncSessionLocal() as db:
|
||||
res = await db.execute(select(VehicleModelDefinition).where(VehicleModelDefinition.id == record_id))
|
||||
rec = res.scalar_one_or_none()
|
||||
if not rec: return
|
||||
make, m_name, v_type = rec.make, rec.marketing_name, (rec.vehicle_type or "car")
|
||||
logger.info(f"🧪 >>> Dúsítás indítása: {make} {m_name}")
|
||||
|
||||
# --- 2. LÉPÉS: AI MUNKA (DB session itt nincs nyitva!) ---
|
||||
# 2. AI FELDOLGOZÁS (DB kapcsolat nélkül!)
|
||||
try:
|
||||
# AIService hívása a kötelező 4. 'sources' paraméterrel
|
||||
# Elsődleges kísérlet a belső tudásbázis alapján
|
||||
ai_data = await AIService.get_clean_vehicle_data(make, m_name, v_type, {})
|
||||
|
||||
# Ha az AI bizonytalan, indítunk egy webes mélyfúrást
|
||||
if not ai_data or not ai_data.get("kw"):
|
||||
logger.info(f"🔍 AI bizonytalan, webes dúsítás indul: {make} {m_name}")
|
||||
logger.info(f"🔍 AI bizonytalan, Web-Context hívása: {make} {m_name}")
|
||||
web_info = await self.get_web_wisdom(make, m_name)
|
||||
ai_data = await AIService.get_clean_vehicle_data(make, m_name, v_type, {"web_context": web_info})
|
||||
|
||||
if not ai_data: raise ValueError("Az AI nem adott értékelhető választ.")
|
||||
if not ai_data or not self.is_data_sane(ai_data):
|
||||
raise ValueError("Hibás vagy hiányos AI válasz.")
|
||||
|
||||
# --- 3. LÉPÉS: MENTÉS (Új session nyitása) ---
|
||||
async with SessionLocal() as db:
|
||||
# MDM (AssetCatalog) Smart Merge
|
||||
# 3. MENTÉS ÉS MERGE (Új session)
|
||||
async with AsyncSessionLocal() as db:
|
||||
# MDM Összefésülés: létezik-e már ez a variáns a katalógusban?
|
||||
clean_model = str(ai_data.get("marketing_name", m_name))[:50].upper()
|
||||
cat_stmt = select(AssetCatalog).where(and_(
|
||||
AssetCatalog.make == make.upper(),
|
||||
AssetCatalog.model == ai_data.get("marketing_name", m_name)[:50],
|
||||
AssetCatalog.model == clean_model,
|
||||
AssetCatalog.power_kw == ai_data.get("kw")
|
||||
)).limit(1)
|
||||
|
||||
if not (await db.execute(cat_stmt)).scalar_one_or_none():
|
||||
existing_cat = (await db.execute(cat_stmt)).scalar_one_or_none()
|
||||
|
||||
if not existing_cat:
|
||||
db.add(AssetCatalog(
|
||||
make=make.upper(),
|
||||
model=ai_data.get("marketing_name", m_name)[:50],
|
||||
model=clean_model,
|
||||
power_kw=ai_data.get("kw"),
|
||||
engine_capacity=ai_data.get("ccm"),
|
||||
factory_data=ai_data
|
||||
fuel_type=ai_data.get("fuel_type", "petrol"),
|
||||
factory_data=ai_data # Teljes technikai JSONB (olaj, gumi, stb.)
|
||||
))
|
||||
logger.info(f"✅ Mentve az MDM-be: {make} {m_name}")
|
||||
logger.info(f"✨ ÚJ KATALÓGUS ELEM (Gold Data): {make} {clean_model}")
|
||||
|
||||
# Staging frissítése
|
||||
# Staging (Discovery) állapot frissítése
|
||||
await db.execute(
|
||||
update(VehicleModelDefinition)
|
||||
.where(VehicleModelDefinition.id == record_id)
|
||||
.values(
|
||||
status="ai_enriched",
|
||||
technical_code=ai_data.get("technical_code") or f"GEN-{record_id}",
|
||||
technical_code=ai_data.get("technical_code") or f"REF-{record_id}",
|
||||
engine_capacity=ai_data.get("ccm"),
|
||||
power_kw=ai_data.get("kw"),
|
||||
updated_at=func.now()
|
||||
@@ -130,37 +134,50 @@ class TechEnricher:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"🚨 Hiba a(z) {record_id} rekordnál: {e}")
|
||||
async with SessionLocal() as db:
|
||||
await db.execute(update(VehicleModelDefinition).where(VehicleModelDefinition.id == record_id).values(
|
||||
attempts=VehicleModelDefinition.attempts + 1,
|
||||
last_error=str(e)[:200],
|
||||
status=text("CASE WHEN attempts >= 4 THEN 'suspended' ELSE 'unverified' END"),
|
||||
updated_at=func.now()
|
||||
))
|
||||
async with AsyncSessionLocal() as db:
|
||||
# Hibakezelés: ha sokszor bukik el, felfüggesztjük a rekordot
|
||||
await db.execute(
|
||||
update(VehicleModelDefinition)
|
||||
.where(VehicleModelDefinition.id == record_id)
|
||||
.values(
|
||||
attempts=VehicleModelDefinition.attempts + 1,
|
||||
last_error=str(e)[:200],
|
||||
status=case(
|
||||
(VehicleModelDefinition.attempts >= 4, "suspended"),
|
||||
else_="unverified"
|
||||
),
|
||||
updated_at=func.now()
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
async def run(self):
|
||||
logger.info(f"🚀 Robot 2 v1.3.0 ONLINE (Limit: {self.daily_ai_limit})")
|
||||
logger.info(f"🚀 Alchemist Robot v1.3.0 ONLINE (Napi keret: {self.daily_ai_limit})")
|
||||
while True:
|
||||
if not self.check_budget():
|
||||
logger.warning("💰 AI költségkeret kimerült mára. Alvás 1 órát.")
|
||||
await asyncio.sleep(3600); continue
|
||||
|
||||
try:
|
||||
async with SessionLocal() as db:
|
||||
# Csak az ID-kat kérjük le, hogy ne tartsuk nyitva a session-t a dúsítás alatt
|
||||
async with AsyncSessionLocal() as db:
|
||||
# Olyan rekordokat keresünk, amik még nincsenek dúsítva és nincs túl sok hiba rajtuk
|
||||
stmt = select(VehicleModelDefinition.id).where(and_(
|
||||
VehicleModelDefinition.status == "unverified",
|
||||
VehicleModelDefinition.attempts < self.max_attempts
|
||||
)).limit(self.batch_size)
|
||||
ids = [r[0] for r in (await db.execute(stmt)).fetchall()]
|
||||
|
||||
# JAVÍTVA: Fetchall és list comprehension
|
||||
res = await db.execute(stmt)
|
||||
ids = [r[0] for r in res.fetchall()]
|
||||
|
||||
if not ids:
|
||||
await asyncio.sleep(60); continue
|
||||
|
||||
logger.info(f"📦 Batch indul: {len(ids)} rekord.")
|
||||
logger.info(f"📦 Batch feldolgozása indul: {len(ids)} tétel.")
|
||||
for rid in ids:
|
||||
await self.process_single_record(rid)
|
||||
await asyncio.sleep(random.uniform(10.0, 30.0)) # VGA kímélése
|
||||
# VGA kímélése és API rate-limit védelem
|
||||
await asyncio.sleep(random.uniform(5.0, 15.0))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"🚨 Főciklus hiba: {e}")
|
||||
|
||||
Reference in New Issue
Block a user