""" api/system.py — Endpoints hệ thống GET /api/health — Trạng thái server & model POST /api/database/reload — Reload embeddings từ Supabase vào RAM """ import logging from flask import Blueprint, jsonify import config from face_model.analyzer import face_analyzer from storage.pg_storage import pg_storage logger = logging.getLogger("api.system") bp = Blueprint("system", __name__, url_prefix="/api") @bp.get("/health") def health(): """ Trả về trạng thái hệ thống. """ return jsonify({ "status": "ok", "model": { "name": config.MODEL_NAME, "provider": config.MODEL_PROVIDER, "ready": face_analyzer.is_ready, }, "cache": { "total_embeddings": face_analyzer.total, }, "threshold": config.SIMILARITY_THRESHOLD, }), 200 @bp.get("/status") def status_legacy(): """Legacy alias → /api/health (backward compatibility với frontend).""" return jsonify({ "status": "ok", "total_faces": face_analyzer.total, "threshold": config.SIMILARITY_THRESHOLD, }), 200 @bp.post("/database/reload") def reload_database(): """ Reload toàn bộ embeddings từ Supabase vào RAM cache. """ try: records = pg_storage.load_all_embeddings() count = face_analyzer.reload_cache(records) return jsonify({ "success": True, "message": f"Đã reload {count} embeddings từ Supabase vào RAM.", "total_embeddings": count, }), 200 except Exception as e: logger.error(f"[reload] Error: {e}", exc_info=True) return jsonify({"error": { "code": "RELOAD_FAILED", "message": f"Không thể reload database: {e}", "status": 500, }}), 500 @bp.post("/reload") def reload_legacy(): """Legacy alias → /api/database/reload (backward compatibility với frontend).""" return reload_database()