fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解
This commit is contained in:
@@ -55,7 +55,7 @@ class CacheStats:
|
||||
expired: int = 0
|
||||
hit_rate: float = 0.0
|
||||
|
||||
def update_hit_rate(self):
|
||||
def update_hit_rate(self) -> None:
|
||||
"""更新命中率"""
|
||||
if self.total_requests > 0:
|
||||
self.hit_rate = round(self.hits / self.total_requests, 4)
|
||||
@@ -194,7 +194,7 @@ class CacheManager:
|
||||
# 初始化缓存统计表
|
||||
self._init_cache_tables()
|
||||
|
||||
def _init_cache_tables(self):
|
||||
def _init_cache_tables(self) -> None:
|
||||
"""初始化缓存统计表"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
|
||||
@@ -234,7 +234,7 @@ class CacheManager:
|
||||
except BaseException:
|
||||
return 1024 # 默认估算
|
||||
|
||||
def _evict_lru(self, required_space: int = 0):
|
||||
def _evict_lru(self, required_space: int = 0) -> None:
|
||||
"""LRU 淘汰策略"""
|
||||
with self.cache_lock:
|
||||
while self.current_memory_size + required_space > self.max_memory_size and self.memory_cache:
|
||||
@@ -444,7 +444,7 @@ class CacheManager:
|
||||
|
||||
return stats
|
||||
|
||||
def save_stats(self):
|
||||
def save_stats(self) -> None:
|
||||
"""保存缓存统计到数据库"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
|
||||
@@ -618,7 +618,7 @@ class DatabaseSharding:
|
||||
# 初始化分片
|
||||
self._init_shards()
|
||||
|
||||
def _init_shards(self):
|
||||
def _init_shards(self) -> None:
|
||||
"""初始化分片"""
|
||||
# 计算每个分片的 key 范围
|
||||
chars = "0123456789abcdef"
|
||||
@@ -645,7 +645,7 @@ class DatabaseSharding:
|
||||
if not os.path.exists(db_path):
|
||||
self._create_shard_db(db_path)
|
||||
|
||||
def _create_shard_db(self, db_path: str):
|
||||
def _create_shard_db(self, db_path: str) -> None:
|
||||
"""创建分片数据库"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
|
||||
@@ -792,7 +792,7 @@ class DatabaseSharding:
|
||||
print(f"迁移失败: {e}")
|
||||
return False
|
||||
|
||||
def _update_shard_stats(self, shard_id: str):
|
||||
def _update_shard_stats(self, shard_id: str) -> None:
|
||||
"""更新分片统计"""
|
||||
shard_info = self.shard_map.get(shard_id)
|
||||
if not shard_info:
|
||||
@@ -923,7 +923,7 @@ class TaskQueue:
|
||||
except Exception as e:
|
||||
print(f"Celery 初始化失败,使用内存任务队列: {e}")
|
||||
|
||||
def _init_task_tables(self):
|
||||
def _init_task_tables(self) -> None:
|
||||
"""初始化任务队列表"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
|
||||
@@ -953,7 +953,7 @@ class TaskQueue:
|
||||
"""检查任务队列是否可用"""
|
||||
return self.use_celery or True # 内存模式也可用
|
||||
|
||||
def register_handler(self, task_type: str, handler: Callable):
|
||||
def register_handler(self, task_type: str, handler: Callable) -> None:
|
||||
"""注册任务处理器"""
|
||||
self.task_handlers[task_type] = handler
|
||||
|
||||
@@ -1014,7 +1014,7 @@ class TaskQueue:
|
||||
|
||||
return task_id
|
||||
|
||||
def _execute_task(self, task_id: str):
|
||||
def _execute_task(self, task_id: str) -> None:
|
||||
"""执行任务(内存模式)"""
|
||||
with self.task_lock:
|
||||
task = self.tasks.get(task_id)
|
||||
@@ -1055,7 +1055,7 @@ class TaskQueue:
|
||||
|
||||
self._update_task_status(task)
|
||||
|
||||
def _save_task(self, task: TaskInfo):
|
||||
def _save_task(self, task: TaskInfo) -> None:
|
||||
"""保存任务到数据库"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
|
||||
@@ -1084,7 +1084,7 @@ class TaskQueue:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def _update_task_status(self, task: TaskInfo):
|
||||
def _update_task_status(self, task: TaskInfo) -> None:
|
||||
"""更新任务状态"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
|
||||
@@ -1143,9 +1143,7 @@ class TaskQueue:
|
||||
with self.task_lock:
|
||||
return self.tasks.get(task_id)
|
||||
|
||||
def list_tasks(
|
||||
self, status: str | None = None, task_type: str | None = None, limit: int = 100
|
||||
) -> list[TaskInfo]:
|
||||
def list_tasks(self, status: str | None = None, task_type: str | None = None, limit: int = 100) -> list[TaskInfo]:
|
||||
"""列出任务"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
@@ -1333,7 +1331,7 @@ class PerformanceMonitor:
|
||||
if metric_type == "db_query" and duration_ms > self.slow_query_threshold:
|
||||
self._record_slow_query(metric)
|
||||
|
||||
def _flush_metrics(self):
|
||||
def _flush_metrics(self) -> None:
|
||||
"""将缓冲区指标写入数据库"""
|
||||
if not self.metrics_buffer:
|
||||
return
|
||||
@@ -1362,12 +1360,12 @@ class PerformanceMonitor:
|
||||
|
||||
self.metrics_buffer = []
|
||||
|
||||
def _record_slow_query(self, metric: PerformanceMetric):
|
||||
def _record_slow_query(self, metric: PerformanceMetric) -> None:
|
||||
"""记录慢查询"""
|
||||
# 可以发送到专门的慢查询日志或监控系统
|
||||
print(f"[SLOW QUERY] {metric.endpoint}: {metric.duration_ms}ms")
|
||||
|
||||
def _trigger_alert(self, metric: PerformanceMetric):
|
||||
def _trigger_alert(self, metric: PerformanceMetric) -> None:
|
||||
"""触发告警"""
|
||||
alert_data = {
|
||||
"type": "performance_alert",
|
||||
@@ -1382,7 +1380,7 @@ class PerformanceMonitor:
|
||||
except Exception as e:
|
||||
print(f"告警处理失败: {e}")
|
||||
|
||||
def register_alert_handler(self, handler: Callable):
|
||||
def register_alert_handler(self, handler: Callable) -> None:
|
||||
"""注册告警处理器"""
|
||||
self.alert_handlers.append(handler)
|
||||
|
||||
@@ -1585,7 +1583,9 @@ class PerformanceMonitor:
|
||||
# ==================== 性能装饰器 ====================
|
||||
|
||||
|
||||
def cached(cache_manager: CacheManager, key_prefix: str = "", ttl: int = 3600, key_func: Callable | None = None):
|
||||
def cached(
|
||||
cache_manager: CacheManager, key_prefix: str = "", ttl: int = 3600, key_func: Callable | None = None
|
||||
) -> None:
|
||||
"""
|
||||
缓存装饰器
|
||||
|
||||
@@ -1598,7 +1598,7 @@ def cached(cache_manager: CacheManager, key_prefix: str = "", ttl: int = 3600, k
|
||||
|
||||
def decorator(func: Callable) -> Callable:
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
def wrapper(*args, **kwargs) -> None:
|
||||
# 生成缓存键
|
||||
if key_func:
|
||||
cache_key = key_func(*args, **kwargs)
|
||||
@@ -1625,7 +1625,7 @@ def cached(cache_manager: CacheManager, key_prefix: str = "", ttl: int = 3600, k
|
||||
return decorator
|
||||
|
||||
|
||||
def monitored(monitor: PerformanceMonitor, metric_type: str, endpoint: str | None = None):
|
||||
def monitored(monitor: PerformanceMonitor, metric_type: str, endpoint: str | None = None) -> None:
|
||||
"""
|
||||
性能监控装饰器
|
||||
|
||||
|
||||
Reference in New Issue
Block a user