fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
- 修复缺失的urllib.parse导入
This commit is contained in:
OpenClaw Bot
2026-02-28 06:03:09 +08:00
parent ff83cab6c7
commit fe3d64a1d2
41 changed files with 4501 additions and 1176 deletions

View File

@@ -42,6 +42,7 @@ except ImportError:
# ==================== 数据模型 ====================
@dataclass
class CacheStats:
"""缓存统计数据模型"""
@@ -58,6 +59,7 @@ class CacheStats:
if self.total_requests > 0:
self.hit_rate = round(self.hits / self.total_requests, 4)
@dataclass
class CacheEntry:
"""缓存条目数据模型"""
@@ -70,6 +72,7 @@ class CacheEntry:
last_accessed: float = 0
size_bytes: int = 0
@dataclass
class PerformanceMetric:
"""性能指标数据模型"""
@@ -91,6 +94,7 @@ class PerformanceMetric:
"metadata": self.metadata,
}
@dataclass
class TaskInfo:
"""任务信息数据模型"""
@@ -122,6 +126,7 @@ class TaskInfo:
"max_retries": self.max_retries,
}
@dataclass
class ShardInfo:
"""分片信息数据模型"""
@@ -134,8 +139,10 @@ class ShardInfo:
created_at: str = ""
last_accessed: str = ""
# ==================== Redis 缓存层 ====================
class CacheManager:
"""
缓存管理器
@@ -213,8 +220,12 @@ class CacheManager:
)
""")
conn.execute("CREATE INDEX IF NOT EXISTS idx_metrics_type ON performance_metrics(metric_type)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_metrics_time ON performance_metrics(timestamp)")
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_metrics_type ON performance_metrics(metric_type)"
)
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_metrics_time ON performance_metrics(timestamp)"
)
conn.commit()
conn.close()
@@ -229,7 +240,10 @@ class CacheManager:
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:
while (
self.current_memory_size + required_space > self.max_memory_size
and self.memory_cache
):
# 移除最久未访问的
oldest_key, oldest_entry = self.memory_cache.popitem(last=False)
self.current_memory_size -= oldest_entry.size_bytes
@@ -429,7 +443,9 @@ class CacheManager:
{
"memory_size_bytes": self.current_memory_size,
"max_memory_size_bytes": self.max_memory_size,
"memory_usage_percent": round(self.current_memory_size / self.max_memory_size * 100, 2),
"memory_usage_percent": round(
self.current_memory_size / self.max_memory_size * 100, 2
),
"cache_entries": len(self.memory_cache),
}
)
@@ -531,7 +547,9 @@ class CacheManager:
stats["transcripts"] += 1
# 预热项目知识库摘要
entity_count = conn.execute("SELECT COUNT(*) FROM entities WHERE project_id = ?", (project_id,)).fetchone()[0]
entity_count = conn.execute(
"SELECT COUNT(*) FROM entities WHERE project_id = ?", (project_id,)
).fetchone()[0]
relation_count = conn.execute(
"SELECT COUNT(*) FROM entity_relations WHERE project_id = ?", (project_id,)
@@ -581,8 +599,10 @@ class CacheManager:
return count
# ==================== 数据库分片 ====================
class DatabaseSharding:
"""
数据库分片管理器
@@ -594,7 +614,12 @@ class DatabaseSharding:
- 分片迁移工具
"""
def __init__(self, base_db_path: str = "insightflow.db", shard_db_dir: str = "./shards", shards_count: int = 4):
def __init__(
self,
base_db_path: str = "insightflow.db",
shard_db_dir: str = "./shards",
shards_count: int = 4,
):
self.base_db_path = base_db_path
self.shard_db_dir = shard_db_dir
self.shards_count = shards_count
@@ -731,7 +756,9 @@ class DatabaseSharding:
source_conn = sqlite3.connect(source_info.db_path)
source_conn.row_factory = sqlite3.Row
entities = source_conn.execute("SELECT * FROM entities WHERE project_id = ?", (project_id,)).fetchall()
entities = source_conn.execute(
"SELECT * FROM entities WHERE project_id = ?", (project_id,)
).fetchall()
relations = source_conn.execute(
"SELECT * FROM entity_relations WHERE project_id = ?", (project_id,)
@@ -875,8 +902,10 @@ class DatabaseSharding:
"message": "Rebalancing analysis completed",
}
# ==================== 异步任务队列 ====================
class TaskQueue:
"""
异步任务队列管理器
@@ -1031,7 +1060,9 @@ class TaskQueue:
if task.retry_count <= task.max_retries:
task.status = "retrying"
# 延迟重试
threading.Timer(10 * task.retry_count, self._execute_task, args=(task_id,)).start()
threading.Timer(
10 * task.retry_count, self._execute_task, args=(task_id,)
).start()
else:
task.status = "failed"
task.error_message = str(e)
@@ -1131,7 +1162,9 @@ 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
@@ -1254,8 +1287,10 @@ class TaskQueue:
"backend": "celery" if self.use_celery else "memory",
}
# ==================== 性能监控 ====================
class PerformanceMonitor:
"""
性能监控器
@@ -1268,7 +1303,10 @@ class PerformanceMonitor:
"""
def __init__(
self, db_path: str = "insightflow.db", slow_query_threshold: int = 1000, alert_threshold: int = 5000 # 毫秒
self,
db_path: str = "insightflow.db",
slow_query_threshold: int = 1000,
alert_threshold: int = 5000, # 毫秒
): # 毫秒
self.db_path = db_path
self.slow_query_threshold = slow_query_threshold
@@ -1283,7 +1321,11 @@ class PerformanceMonitor:
self.alert_handlers: list[Callable] = []
def record_metric(
self, metric_type: str, duration_ms: float, endpoint: str | None = None, metadata: dict | None = None
self,
metric_type: str,
duration_ms: float,
endpoint: str | None = None,
metadata: dict | None = None,
):
"""
记录性能指标
@@ -1565,10 +1607,15 @@ class PerformanceMonitor:
return deleted
# ==================== 性能装饰器 ====================
def cached(
cache_manager: CacheManager, key_prefix: str = "", ttl: int = 3600, key_func: Callable | None = None
cache_manager: CacheManager,
key_prefix: str = "",
ttl: int = 3600,
key_func: Callable | None = None,
) -> None:
"""
缓存装饰器
@@ -1608,6 +1655,7 @@ def cached(
return decorator
def monitored(monitor: PerformanceMonitor, metric_type: str, endpoint: str | None = None) -> None:
"""
性能监控装饰器
@@ -1635,8 +1683,10 @@ def monitored(monitor: PerformanceMonitor, metric_type: str, endpoint: str | Non
return decorator
# ==================== 性能管理器 ====================
class PerformanceManager:
"""
性能管理器 - 统一入口
@@ -1644,7 +1694,12 @@ class PerformanceManager:
整合缓存管理、数据库分片、任务队列和性能监控功能
"""
def __init__(self, db_path: str = "insightflow.db", redis_url: str | None = None, enable_sharding: bool = False):
def __init__(
self,
db_path: str = "insightflow.db",
redis_url: str | None = None,
enable_sharding: bool = False,
):
self.db_path = db_path
# 初始化各模块
@@ -1693,14 +1748,18 @@ class PerformanceManager:
return stats
# 单例模式
_performance_manager = None
def get_performance_manager(
db_path: str = "insightflow.db", redis_url: str | None = None, enable_sharding: bool = False
) -> PerformanceManager:
"""获取性能管理器单例"""
global _performance_manager
if _performance_manager is None:
_performance_manager = PerformanceManager(db_path=db_path, redis_url=redis_url, enable_sharding=enable_sharding)
_performance_manager = PerformanceManager(
db_path=db_path, redis_url=redis_url, enable_sharding=enable_sharding
)
return _performance_manager