File size: 2,012 Bytes
75557db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
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()