You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/bookscraper/scraper/ui_log.py

47 lines
1.1 KiB

# ============================================
# File: scraper/ui_log.py
# Purpose: Central UI log buffer for WebGUI
# Single global buffer. No book_id.
# ============================================
import redis
import os
from datetime import datetime
REDIS_URL = os.getenv("REDIS_BROKER", "redis://redis:6379/0")
LOG_BUFFER_SIZE = int(os.getenv("LOG_BUFFER_SIZE", "1000"))
r = redis.Redis.from_url(REDIS_URL, decode_responses=True)
UI_LOG_KEY = "logs:ui"
def push_ui(message: str):
"""Push a message into the global UI log (no book_id)."""
if not message or not message.strip():
return
ts = datetime.now().strftime("%H:%M:%S")
entry = f"[{ts}] {message}"
r.rpush(UI_LOG_KEY, entry)
r.ltrim(UI_LOG_KEY, -LOG_BUFFER_SIZE, -1)
def get_ui_logs(limit: int = None):
"""Return last N global UI log lines."""
if limit is None:
limit = LOG_BUFFER_SIZE
return r.lrange(UI_LOG_KEY, -limit, -1)
def reset_ui_logs():
"""
Clear the entire UI log buffer.
Used by:
- Clear button in GUI
- Auto-clear when new book scraping starts
"""
r.delete(UI_LOG_KEY)