fix: auto-fix code issues (cron)
- 修复隐式 Optional 类型注解 (RUF013) - 修复不必要的赋值后返回 (RET504) - 优化列表推导式 (PERF401) - 修复未使用的参数 (ARG002) - 清理重复导入 - 优化异常处理
This commit is contained in:
@@ -383,11 +383,11 @@ class GrowthManager:
|
||||
user_id: str,
|
||||
event_type: EventType,
|
||||
event_name: str,
|
||||
properties: dict = None,
|
||||
session_id: str = None,
|
||||
device_info: dict = None,
|
||||
referrer: str = None,
|
||||
utm_params: dict = None,
|
||||
properties: dict | None = None,
|
||||
session_id: str | None = None,
|
||||
device_info: dict | None = None,
|
||||
referrer: str | None = None,
|
||||
utm_params: dict | None = None,
|
||||
) -> AnalyticsEvent:
|
||||
"""追踪事件"""
|
||||
event_id = f"evt_{uuid.uuid4().hex[:16]}"
|
||||
@@ -475,7 +475,10 @@ 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}")
|
||||
@@ -509,7 +512,11 @@ 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 +588,10 @@ 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 = None,
|
||||
end_date: datetime = None,
|
||||
) -> dict:
|
||||
"""获取用户分析汇总"""
|
||||
with self._get_db() as conn:
|
||||
@@ -635,7 +645,12 @@ 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 +688,16 @@ 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 = 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 +723,8 @@ 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
|
||||
@@ -752,7 +772,10 @@ 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:
|
||||
@@ -825,7 +848,7 @@ class GrowthManager:
|
||||
secondary_metrics: list[str],
|
||||
min_sample_size: int = 100,
|
||||
confidence_level: float = 0.95,
|
||||
created_by: str = None,
|
||||
created_by: str | None = None,
|
||||
) -> Experiment:
|
||||
"""创建 A/B 测试实验"""
|
||||
experiment_id = f"exp_{uuid.uuid4().hex[:16]}"
|
||||
@@ -893,14 +916,17 @@ 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:
|
||||
return self._row_to_experiment(row)
|
||||
return None
|
||||
|
||||
def list_experiments(self, tenant_id: str, status: ExperimentStatus = None) -> list[Experiment]:
|
||||
def list_experiments(
|
||||
self, tenant_id: str, status: ExperimentStatus | None = None
|
||||
) -> list[Experiment]:
|
||||
"""列出实验"""
|
||||
query = "SELECT * FROM experiments WHERE tenant_id = ?"
|
||||
params = [tenant_id]
|
||||
@@ -916,7 +942,10 @@ 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 = None,
|
||||
) -> str | None:
|
||||
"""为用户分配实验变体"""
|
||||
experiment = self.get_experiment(experiment_id)
|
||||
@@ -939,11 +968,15 @@ 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 +1011,10 @@ 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 +1027,10 @@ 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,7 +1044,14 @@ class GrowthManager:
|
||||
|
||||
user_value = user_attributes.get(attr_name) if user_attributes else None
|
||||
|
||||
if operator == "equals" and user_value != value or operator == "not_equals" and user_value == value or 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
|
||||
|
||||
@@ -1177,11 +1223,11 @@ class GrowthManager:
|
||||
template_type: EmailTemplateType,
|
||||
subject: str,
|
||||
html_content: str,
|
||||
text_content: str = None,
|
||||
text_content: str | None = None,
|
||||
variables: list[str] = None,
|
||||
from_name: str = None,
|
||||
from_email: str = None,
|
||||
reply_to: str = None,
|
||||
from_name: str | None = None,
|
||||
from_email: str | None = None,
|
||||
reply_to: str | None = None,
|
||||
) -> EmailTemplate:
|
||||
"""创建邮件模板"""
|
||||
template_id = f"et_{uuid.uuid4().hex[:16]}"
|
||||
@@ -1242,7 +1288,8 @@ 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:
|
||||
@@ -1250,7 +1297,9 @@ class GrowthManager:
|
||||
return None
|
||||
|
||||
def list_email_templates(
|
||||
self, tenant_id: str, template_type: EmailTemplateType = None,
|
||||
self,
|
||||
tenant_id: str,
|
||||
template_type: EmailTemplateType | None = None,
|
||||
) -> list[EmailTemplate]:
|
||||
"""列出邮件模板"""
|
||||
query = "SELECT * FROM email_templates WHERE tenant_id = ? AND is_active = 1"
|
||||
@@ -1297,7 +1346,7 @@ class GrowthManager:
|
||||
name: str,
|
||||
template_id: str,
|
||||
recipient_list: list[dict],
|
||||
scheduled_at: datetime = None,
|
||||
scheduled_at: datetime | None = None,
|
||||
) -> EmailCampaign:
|
||||
"""创建邮件营销活动"""
|
||||
campaign_id = f"ec_{uuid.uuid4().hex[:16]}"
|
||||
@@ -1377,7 +1426,12 @@ 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)
|
||||
@@ -1448,7 +1502,8 @@ 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:
|
||||
@@ -1478,7 +1533,11 @@ 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:
|
||||
@@ -1763,7 +1822,8 @@ 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:
|
||||
@@ -1773,7 +1833,8 @@ 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:
|
||||
@@ -1859,7 +1920,8 @@ 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,
|
||||
),
|
||||
}
|
||||
|
||||
@@ -1922,7 +1984,10 @@ 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:
|
||||
|
||||
Reference in New Issue
Block a user