File size: 1,617 Bytes
c66a1ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sqlite3
import logging
from fastapi import APIRouter, HTTPException
from typing import List, Dict, Any

_logger = logging.getLogger("ads_manager")
router = APIRouter(prefix="/api/ads", tags=["ads"])

DB_PATH = os.getenv("ADS_DB_PATH", "/app/data/ads/ads-client.db")

def get_db_connection():
    if not os.path.exists(DB_PATH):
        _logger.warning(f"Database Ads non trovato in {DB_PATH}")
        return None
    try:
        conn = sqlite3.connect(DB_PATH)
        conn.row_factory = sqlite3.Row
        return conn
    except Exception as e:
        _logger.error(f"Errore connessione DB Ads: {e}")
        return None

@router.get("/health")
async def ads_health():
    conn = get_db_connection()
    if not conn:
        return {"status": "error", "message": "Database non disponibile"}
    try:
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM http_cache")
        count = cursor.fetchone()[0]
        conn.close()
        return {"status": "ok", "record_count": count}
    except Exception as e:
        return {"status": "error", "message": str(e)}

@router.get("/cache")
async def get_ads_cache(limit: int = 10):
    conn = get_db_connection()
    if not conn:
        raise HTTPException(status_code=503, detail="Database Ads non disponibile")
    try:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM http_cache LIMIT ?", (limit,))
        rows = [dict(row) for row in cursor.fetchall()]
        conn.close()
        return {"ok": True, "data": rows}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))