Files
gpu-services/nanoclaude/db/database.py
Hyungi Ahn e970ebdbea feat: NanoClaude 프로덕션 통합 — Docker, Caddy, aiosqlite 로깅
- docker-compose에 nanoclaude 서비스 추가 (포트 8100)
- Caddy /nano/* → nanoclaude 리버스 프록시 (SSE flush)
- aiosqlite 요청/응답 로깅 (request_logs 테이블)
- .env.example, CLAUDE.md 업데이트

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 11:19:15 +09:00

48 lines
1.5 KiB
Python

"""aiosqlite DB — 요청/응답 로깅 및 메트릭."""
import aiosqlite
from config import settings
SCHEMA = """
CREATE TABLE IF NOT EXISTS request_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job_id TEXT NOT NULL,
message TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'queued',
model TEXT NOT NULL,
response_chars INTEGER DEFAULT 0,
latency_ms REAL DEFAULT 0,
created_at REAL NOT NULL,
completed_at REAL
);
CREATE INDEX IF NOT EXISTS idx_logs_job ON request_logs(job_id);
CREATE INDEX IF NOT EXISTS idx_logs_created ON request_logs(created_at);
"""
async def init_db():
async with aiosqlite.connect(settings.db_path) as db:
await db.execute("PRAGMA journal_mode=WAL")
await db.executescript(SCHEMA)
await db.commit()
async def log_request(job_id: str, message: str, model: str, created_at: float):
async with aiosqlite.connect(settings.db_path) as db:
await db.execute(
"INSERT INTO request_logs (job_id, message, model, created_at) VALUES (?, ?, ?, ?)",
(job_id, message, model, created_at),
)
await db.commit()
async def log_completion(job_id: str, status: str, response_chars: int, latency_ms: float, completed_at: float):
async with aiosqlite.connect(settings.db_path) as db:
await db.execute(
"UPDATE request_logs SET status=?, response_chars=?, latency_ms=?, completed_at=? WHERE job_id=?",
(status, response_chars, latency_ms, completed_at, job_id),
)
await db.commit()