fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
This commit is contained in:
AutoFix Bot
2026-03-03 06:03:38 +08:00
parent 2a0ed6af4d
commit 9fd1da8fb7
41 changed files with 901 additions and 768 deletions

View File

@@ -604,7 +604,7 @@ class OpsManager:
updates["updated_at"] = datetime.now().isoformat()
with self._get_db() as conn:
set_clause = ", ".join([f"{k} = ?" for k in updates.keys()])
set_clause = ", ".join([f"{k} = ?" for k in updates])
conn.execute(
f"UPDATE alert_rules SET {set_clause} WHERE id = ?",
list(updates.values()) + [rule_id],
@@ -680,7 +680,7 @@ class OpsManager:
"""获取告警渠道"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM alert_channels WHERE id = ?", (channel_id,)
"SELECT * FROM alert_channels WHERE id = ?", (channel_id,),
).fetchone()
if row:
@@ -819,7 +819,7 @@ class OpsManager:
for rule in rules:
# 获取相关指标
metrics = self.get_recent_metrics(
tenant_id, rule.metric, seconds=rule.duration + rule.evaluation_interval
tenant_id, rule.metric, seconds=rule.duration + rule.evaluation_interval,
)
# 评估规则
@@ -1129,7 +1129,7 @@ class OpsManager:
async with httpx.AsyncClient() as client:
response = await client.post(
"https://events.pagerduty.com/v2/enqueue", json=message, timeout=30.0
"https://events.pagerduty.com/v2/enqueue", json=message, timeout=30.0,
)
success = response.status_code == 202
self._update_channel_stats(channel.id, success)
@@ -1299,12 +1299,12 @@ class OpsManager:
conn.commit()
def _update_alert_notification_status(
self, alert_id: str, channel_id: str, success: bool
self, alert_id: str, channel_id: str, success: bool,
) -> None:
"""更新告警通知状态"""
with self._get_db() as conn:
row = conn.execute(
"SELECT notification_sent FROM alerts WHERE id = ?", (alert_id,)
"SELECT notification_sent FROM alerts WHERE id = ?", (alert_id,),
).fetchone()
if row:
@@ -1394,7 +1394,7 @@ class OpsManager:
"""检查告警是否被抑制"""
with self._get_db() as conn:
rows = conn.execute(
"SELECT * FROM alert_suppression_rules WHERE tenant_id = ?", (rule.tenant_id,)
"SELECT * FROM alert_suppression_rules WHERE tenant_id = ?", (rule.tenant_id,),
).fetchall()
for row in rows:
@@ -1479,7 +1479,7 @@ class OpsManager:
return metric
def get_recent_metrics(
self, tenant_id: str, metric_name: str, seconds: int = 3600
self, tenant_id: str, metric_name: str, seconds: int = 3600,
) -> list[ResourceMetric]:
"""获取最近的指标数据"""
cutoff_time = (datetime.now() - timedelta(seconds=seconds)).isoformat()
@@ -1531,7 +1531,7 @@ class OpsManager:
# 基于历史数据预测
metrics = self.get_recent_metrics(
tenant_id, f"{resource_type.value}_usage", seconds=30 * 24 * 3600
tenant_id, f"{resource_type.value}_usage", seconds=30 * 24 * 3600,
)
if metrics:
@@ -1704,7 +1704,7 @@ class OpsManager:
"""获取自动扩缩容策略"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM auto_scaling_policies WHERE id = ?", (policy_id,)
"SELECT * FROM auto_scaling_policies WHERE id = ?", (policy_id,),
).fetchone()
if row:
@@ -1721,7 +1721,7 @@ class OpsManager:
return [self._row_to_auto_scaling_policy(row) for row in rows]
def evaluate_scaling_policy(
self, policy_id: str, current_instances: int, current_utilization: float
self, policy_id: str, current_instances: int, current_utilization: float,
) -> ScalingEvent | None:
"""评估扩缩容策略"""
policy = self.get_auto_scaling_policy(policy_id)
@@ -1826,7 +1826,7 @@ class OpsManager:
return None
def update_scaling_event_status(
self, event_id: str, status: str, error_message: str = None
self, event_id: str, status: str, error_message: str = None,
) -> ScalingEvent | None:
"""更新扩缩容事件状态"""
now = datetime.now().isoformat()
@@ -1864,7 +1864,7 @@ class OpsManager:
return None
def list_scaling_events(
self, tenant_id: str, policy_id: str = None, limit: int = 100
self, tenant_id: str, policy_id: str = None, limit: int = 100,
) -> list[ScalingEvent]:
"""列出租户的扩缩容事件"""
query = "SELECT * FROM scaling_events WHERE tenant_id = ?"
@@ -2056,7 +2056,7 @@ class OpsManager:
start_time = time.time()
try:
reader, writer = await asyncio.wait_for(
asyncio.open_connection(host, port), timeout=check.timeout
asyncio.open_connection(host, port), timeout=check.timeout,
)
response_time = (time.time() - start_time) * 1000
writer.close()
@@ -2153,7 +2153,7 @@ class OpsManager:
"""获取故障转移配置"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM failover_configs WHERE id = ?", (config_id,)
"SELECT * FROM failover_configs WHERE id = ?", (config_id,),
).fetchone()
if row:
@@ -2259,7 +2259,7 @@ class OpsManager:
"""获取故障转移事件"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM failover_events WHERE id = ?", (event_id,)
"SELECT * FROM failover_events WHERE id = ?", (event_id,),
).fetchone()
if row:
@@ -2430,7 +2430,7 @@ class OpsManager:
"""获取备份记录"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM backup_records WHERE id = ?", (record_id,)
"SELECT * FROM backup_records WHERE id = ?", (record_id,),
).fetchone()
if row:
@@ -2438,7 +2438,7 @@ class OpsManager:
return None
def list_backup_records(
self, tenant_id: str, job_id: str = None, limit: int = 100
self, tenant_id: str, job_id: str = None, limit: int = 100,
) -> list[BackupRecord]:
"""列出租户的备份记录"""
query = "SELECT * FROM backup_records WHERE tenant_id = ?"
@@ -2544,7 +2544,7 @@ class OpsManager:
"resource_id": util.resource_id,
"utilization_rate": util.utilization_rate,
"severity": "high" if util.utilization_rate < 0.05 else "medium",
}
},
)
# 检测高峰利用率
@@ -2556,7 +2556,7 @@ class OpsManager:
"resource_id": util.resource_id,
"peak_utilization": util.peak_utilization,
"severity": "medium",
}
},
)
return anomalies
@@ -2624,7 +2624,7 @@ class OpsManager:
return util
def get_resource_utilizations(
self, tenant_id: str, report_period: str
self, tenant_id: str, report_period: str,
) -> list[ResourceUtilization]:
"""获取资源利用率列表"""
with self._get_db() as conn:
@@ -2709,7 +2709,7 @@ class OpsManager:
return [self._row_to_idle_resource(row) for row in rows]
def generate_cost_optimization_suggestions(
self, tenant_id: str
self, tenant_id: str,
) -> list[CostOptimizationSuggestion]:
"""生成成本优化建议"""
suggestions = []
@@ -2777,7 +2777,7 @@ class OpsManager:
return suggestions
def get_cost_optimization_suggestions(
self, tenant_id: str, is_applied: bool = None
self, tenant_id: str, is_applied: bool = None,
) -> list[CostOptimizationSuggestion]:
"""获取成本优化建议"""
query = "SELECT * FROM cost_optimization_suggestions WHERE tenant_id = ?"
@@ -2794,7 +2794,7 @@ class OpsManager:
return [self._row_to_cost_optimization_suggestion(row) for row in rows]
def apply_cost_optimization_suggestion(
self, suggestion_id: str
self, suggestion_id: str,
) -> CostOptimizationSuggestion | None:
"""应用成本优化建议"""
now = datetime.now().isoformat()
@@ -2813,12 +2813,12 @@ class OpsManager:
return self.get_cost_optimization_suggestion(suggestion_id)
def get_cost_optimization_suggestion(
self, suggestion_id: str
self, suggestion_id: str,
) -> CostOptimizationSuggestion | None:
"""获取成本优化建议详情"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM cost_optimization_suggestions WHERE id = ?", (suggestion_id,)
"SELECT * FROM cost_optimization_suggestions WHERE id = ?", (suggestion_id,),
).fetchone()
if row: