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

@@ -475,7 +475,7 @@ class GrowthManager:
async with httpx.AsyncClient() as client:
await client.post(
"https://api.mixpanel.com/track", headers=headers, json=[payload], timeout=10.0
"https://api.mixpanel.com/track", headers=headers, json=[payload], timeout=10.0,
)
except (RuntimeError, ValueError, TypeError) as e:
print(f"Failed to send to Mixpanel: {e}")
@@ -494,7 +494,7 @@ class GrowthManager:
"time": int(event.timestamp.timestamp() * 1000),
"event_properties": event.properties,
"user_properties": {},
}
},
],
}
@@ -509,7 +509,7 @@ class GrowthManager:
print(f"Failed to send to Amplitude: {e}")
async def _update_user_profile(
self, tenant_id: str, user_id: str, event_type: EventType, event_name: str
self, tenant_id: str, user_id: str, event_type: EventType, event_name: str,
) -> None:
"""更新用户画像"""
with self._get_db() as conn:
@@ -581,7 +581,7 @@ class GrowthManager:
return None
def get_user_analytics_summary(
self, tenant_id: str, start_date: datetime = None, end_date: datetime = None
self, tenant_id: str, start_date: datetime = None, end_date: datetime = None,
) -> dict:
"""获取用户分析汇总"""
with self._get_db() as conn:
@@ -635,7 +635,7 @@ class GrowthManager:
}
def create_funnel(
self, tenant_id: str, name: str, description: str, steps: list[dict], created_by: str
self, tenant_id: str, name: str, description: str, steps: list[dict], created_by: str,
) -> Funnel:
"""创建转化漏斗"""
funnel_id = f"fnl_{uuid.uuid4().hex[:16]}"
@@ -673,12 +673,12 @@ class GrowthManager:
return funnel
def analyze_funnel(
self, funnel_id: str, period_start: datetime = None, period_end: datetime = None
self, funnel_id: str, period_start: datetime = None, period_end: datetime = None,
) -> FunnelAnalysis | None:
"""分析漏斗转化率"""
with self._get_db() as conn:
funnel_row = conn.execute(
"SELECT * FROM funnels WHERE id = ?", (funnel_id,)
"SELECT * FROM funnels WHERE id = ?", (funnel_id,),
).fetchone()
if not funnel_row:
@@ -704,7 +704,7 @@ class GrowthManager:
WHERE event_name = ? AND timestamp >= ? AND timestamp <= ?
"""
row = conn.execute(
query, (event_name, period_start.isoformat(), period_end.isoformat())
query, (event_name, period_start.isoformat(), period_end.isoformat()),
).fetchone()
user_count = row["user_count"] if row else 0
@@ -723,7 +723,7 @@ class GrowthManager:
"user_count": user_count,
"conversion_rate": round(conversion_rate, 4),
"drop_off_rate": round(drop_off_rate, 4),
}
},
)
previous_count = user_count
@@ -752,7 +752,7 @@ class GrowthManager:
)
def calculate_retention(
self, tenant_id: str, cohort_date: datetime, periods: list[int] = None
self, tenant_id: str, cohort_date: datetime, periods: list[int] = None,
) -> dict:
"""计算留存率"""
if periods is None:
@@ -893,7 +893,7 @@ class GrowthManager:
"""获取实验详情"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM experiments WHERE id = ?", (experiment_id,)
"SELECT * FROM experiments WHERE id = ?", (experiment_id,),
).fetchone()
if row:
@@ -916,7 +916,7 @@ class GrowthManager:
return [self._row_to_experiment(row) for row in rows]
def assign_variant(
self, experiment_id: str, user_id: str, user_attributes: dict = None
self, experiment_id: str, user_id: str, user_attributes: dict = None,
) -> str | None:
"""为用户分配实验变体"""
experiment = self.get_experiment(experiment_id)
@@ -939,11 +939,11 @@ class GrowthManager:
variant_id = self._random_allocation(experiment.variants, experiment.traffic_split)
elif experiment.traffic_allocation == TrafficAllocationType.STRATIFIED:
variant_id = self._stratified_allocation(
experiment.variants, experiment.traffic_split, user_attributes
experiment.variants, experiment.traffic_split, user_attributes,
)
else: # TARGETED
variant_id = self._targeted_allocation(
experiment.variants, experiment.target_audience, user_attributes
experiment.variants, experiment.target_audience, user_attributes,
)
if variant_id:
@@ -978,7 +978,7 @@ class GrowthManager:
return random.choices(variant_ids, weights=normalized_weights, k=1)[0]
def _stratified_allocation(
self, variants: list[dict], traffic_split: dict[str, float], user_attributes: dict
self, variants: list[dict], traffic_split: dict[str, float], user_attributes: dict,
) -> str:
"""分层分配(基于用户属性)"""
# 简化的分层分配:根据用户 ID 哈希值分配
@@ -991,7 +991,7 @@ class GrowthManager:
return self._random_allocation(variants, traffic_split)
def _targeted_allocation(
self, variants: list[dict], target_audience: dict, user_attributes: dict
self, variants: list[dict], target_audience: dict, user_attributes: dict,
) -> str | None:
"""定向分配(基于目标受众条件)"""
# 检查用户是否符合目标受众条件
@@ -1005,13 +1005,7 @@ class GrowthManager:
user_value = user_attributes.get(attr_name) if user_attributes else None
if operator == "equals" and user_value != value:
matches = False
break
elif operator == "not_equals" and user_value == value:
matches = False
break
elif operator == "in" and user_value not in value:
if operator == "equals" and user_value != value or operator == "not_equals" and user_value == value or operator == "in" and user_value not in value:
matches = False
break
@@ -1248,7 +1242,7 @@ class GrowthManager:
"""获取邮件模板"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM email_templates WHERE id = ?", (template_id,)
"SELECT * FROM email_templates WHERE id = ?", (template_id,),
).fetchone()
if row:
@@ -1256,7 +1250,7 @@ class GrowthManager:
return None
def list_email_templates(
self, tenant_id: str, template_type: EmailTemplateType = None
self, tenant_id: str, template_type: EmailTemplateType = None,
) -> list[EmailTemplate]:
"""列出邮件模板"""
query = "SELECT * FROM email_templates WHERE tenant_id = ? AND is_active = 1"
@@ -1383,7 +1377,7 @@ class GrowthManager:
return campaign
async def send_email(
self, campaign_id: str, user_id: str, email: str, template_id: str, variables: dict
self, campaign_id: str, user_id: str, email: str, template_id: str, variables: dict,
) -> bool:
"""发送单封邮件"""
template = self.get_email_template(template_id)
@@ -1454,7 +1448,7 @@ class GrowthManager:
"""发送整个营销活动"""
with self._get_db() as conn:
campaign_row = conn.execute(
"SELECT * FROM email_campaigns WHERE id = ?", (campaign_id,)
"SELECT * FROM email_campaigns WHERE id = ?", (campaign_id,),
).fetchone()
if not campaign_row:
@@ -1484,7 +1478,7 @@ class GrowthManager:
variables = self._get_user_variables(log["tenant_id"], log["user_id"])
success = await self.send_email(
campaign_id, log["user_id"], log["email"], log["template_id"], variables
campaign_id, log["user_id"], log["email"], log["template_id"], variables,
)
if success:
@@ -1769,7 +1763,7 @@ class GrowthManager:
with self._get_db() as conn:
row = conn.execute(
"SELECT 1 FROM referrals WHERE referral_code = ?", (code,)
"SELECT 1 FROM referrals WHERE referral_code = ?", (code,),
).fetchone()
if not row:
@@ -1779,7 +1773,7 @@ class GrowthManager:
"""获取推荐计划"""
with self._get_db() as conn:
row = conn.execute(
"SELECT * FROM referral_programs WHERE id = ?", (program_id,)
"SELECT * FROM referral_programs WHERE id = ?", (program_id,),
).fetchone()
if row:
@@ -1865,7 +1859,7 @@ class GrowthManager:
"expired": stats["expired"] or 0,
"unique_referrers": stats["unique_referrers"] or 0,
"conversion_rate": round(
(stats["converted"] or 0) / max(stats["total_referrals"] or 1, 1), 4
(stats["converted"] or 0) / max(stats["total_referrals"] or 1, 1), 4,
),
}
@@ -1928,7 +1922,7 @@ class GrowthManager:
return incentive
def check_team_incentive_eligibility(
self, tenant_id: str, current_tier: str, team_size: int
self, tenant_id: str, current_tier: str, team_size: int,
) -> list[TeamIncentive]:
"""检查团队激励资格"""
with self._get_db() as conn:
@@ -2007,7 +2001,7 @@ class GrowthManager:
).fetchone()
hourly_trend.append(
{"hour": hour_end.strftime("%H:00"), "active_users": row["count"] or 0}
{"hour": hour_end.strftime("%H:00"), "active_users": row["count"] or 0},
)
return {