This commit is contained in:
qing committed 2026-07-25 21:08:17 +08:00
1 parent 013e130a08
commit 9349b8248f
1 file changed
+12 -17
+12 -17
View File
@@ -16,7 +16,7 @@ import threading
import time import time
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
from flask import Blueprint, request, jsonify from flask import Blueprint, request, jsonify, current_app
from sqlalchemy import ( from sqlalchemy import (
Table, insert, select, delete, func, and_, or_, MetaData Table, insert, select, delete, func, and_, or_, MetaData
) )
@@ -128,13 +128,8 @@ def init_log_routes(app, engine, metadata,
app.config['LOG_CLEANUP_INTERVAL'] = cleanup_interval app.config['LOG_CLEANUP_INTERVAL'] = cleanup_interval
# 建表 + 反射 # 建表 + 反射
count = 0
try: try:
table = _ensure_logs_table(metadata, engine) _ensure_logs_table(metadata, engine)
with engine.connect() as conn:
count = conn.execute(
select(func.count()).select_from(table)
).scalar() or 0
except Exception as e: except Exception as e:
log_internal(engine, metadata, 'db', 'ERROR', log_internal(engine, metadata, 'db', 'ERROR',
f'Logs 表初始化失败: {e}') f'Logs 表初始化失败: {e}')
@@ -165,8 +160,8 @@ def ingest():
'error': f'单次最多 {MAX_BATCH} 条,当前 {len(logs)} 条' 'error': f'单次最多 {MAX_BATCH} 条,当前 {len(logs)} 条'
}), 400 }), 400
engine = app.config['LOG_ENGINE'] engine = current_app.config['LOG_ENGINE']
metadata = app.config['LOG_METADATA'] metadata = current_app.config['LOG_METADATA']
try: try:
table = _ensure_logs_table(metadata, engine) table = _ensure_logs_table(metadata, engine)
@@ -236,8 +231,8 @@ def query():
"""结构化查询日志。""" """结构化查询日志。"""
data = request.get_json(silent=True) or {} data = request.get_json(silent=True) or {}
engine = app.config['LOG_ENGINE'] engine = current_app.config['LOG_ENGINE']
metadata = app.config['LOG_METADATA'] metadata = current_app.config['LOG_METADATA']
try: try:
table = _ensure_logs_table(metadata, engine) table = _ensure_logs_table(metadata, engine)
@@ -337,8 +332,8 @@ def stats():
"""日志分类统计。""" """日志分类统计。"""
data = request.get_json(silent=True) or {} data = request.get_json(silent=True) or {}
engine = app.config['LOG_ENGINE'] engine = current_app.config['LOG_ENGINE']
metadata = app.config['LOG_METADATA'] metadata = current_app.config['LOG_METADATA']
try: try:
table = _ensure_logs_table(metadata, engine) table = _ensure_logs_table(metadata, engine)
@@ -400,8 +395,8 @@ def purge():
if not before: if not before:
return jsonify({'success': False, 'error': '缺少 before 字段'}), 400 return jsonify({'success': False, 'error': '缺少 before 字段'}), 400
engine = app.config['LOG_ENGINE'] engine = current_app.config['LOG_ENGINE']
metadata = app.config['LOG_METADATA'] metadata = current_app.config['LOG_METADATA']
try: try:
table = _ensure_logs_table(metadata, engine) table = _ensure_logs_table(metadata, engine)
@@ -429,8 +424,8 @@ def purge():
@bp.route('/health', methods=['GET']) @bp.route('/health', methods=['GET'])
def health(): def health():
"""日志模块健康检查。""" """日志模块健康检查。"""
engine = app.config['LOG_ENGINE'] engine = current_app.config['LOG_ENGINE']
metadata = app.config['LOG_METADATA'] metadata = current_app.config['LOG_METADATA']
try: try:
table = _ensure_logs_table(metadata, engine) table = _ensure_logs_table(metadata, engine)