zfc 8fbcb72a3f feat(core): 完成 Phase 2 核心功能开发
- 实现查询API (query.py): 支持star_id/unique_id/nickname三种查询方式
- 实现计算模块 (calculator.py): CPM/自然搜索UV/搜索成本计算
- 实现品牌API集成 (brand_api.py): 批量并发调用,10并发限制
- 实现导出服务 (export_service.py): Excel/CSV导出
- 前端组件: QueryForm/ResultTable/ExportButton
- 主页面集成: 支持6种页面状态
- 测试: 44个测试全部通过,覆盖率88%

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 14:38:38 +08:00

103 lines
2.7 KiB
Python

from typing import Optional, Dict
def calculate_natural_cpm(
estimated_video_cost: float,
natural_play_cnt: int,
) -> Optional[float]:
"""
计算预估自然CPM.
公式: estimated_video_cost / natural_play_cnt * 1000
Args:
estimated_video_cost: 预估视频成本
natural_play_cnt: 自然播放量
Returns:
预估自然CPM (元/千次曝光), 除零时返回 None
"""
if natural_play_cnt <= 0:
return None
return round((estimated_video_cost / natural_play_cnt) * 1000, 2)
def calculate_natural_search_uv(
natural_play_cnt: int,
total_play_cnt: int,
after_view_search_uv: int,
) -> Optional[float]:
"""
计算预估自然看后搜人数.
公式: natural_play_cnt / total_play_cnt * after_view_search_uv
Args:
natural_play_cnt: 自然播放量
total_play_cnt: 总播放量
after_view_search_uv: 看后搜人数
Returns:
预估自然看后搜人数, 除零时返回 None
"""
if total_play_cnt <= 0:
return None
return round((natural_play_cnt / total_play_cnt) * after_view_search_uv, 2)
def calculate_natural_search_cost(
estimated_video_cost: float,
estimated_natural_search_uv: Optional[float],
) -> Optional[float]:
"""
计算预估自然看后搜人数成本.
公式: estimated_video_cost / 预估自然看后搜人数
Args:
estimated_video_cost: 预估视频成本
estimated_natural_search_uv: 预估自然看后搜人数
Returns:
预估自然看后搜人数成本 (元/人), 除零时返回 None
"""
if estimated_natural_search_uv is None or estimated_natural_search_uv <= 0:
return None
return round(estimated_video_cost / estimated_natural_search_uv, 2)
def calculate_metrics(
estimated_video_cost: float,
natural_play_cnt: int,
total_play_cnt: int,
after_view_search_uv: int,
) -> Dict[str, Optional[float]]:
"""
批量计算所有预估指标.
Args:
estimated_video_cost: 预估视频成本
natural_play_cnt: 自然播放量
total_play_cnt: 总播放量
after_view_search_uv: 看后搜人数
Returns:
包含所有计算结果的字典
"""
# 计算 CPM
cpm = calculate_natural_cpm(estimated_video_cost, natural_play_cnt)
# 计算看后搜人数
search_uv = calculate_natural_search_uv(
natural_play_cnt, total_play_cnt, after_view_search_uv
)
# 计算看后搜成本
search_cost = calculate_natural_search_cost(estimated_video_cost, search_uv)
return {
"estimated_natural_cpm": cpm,
"estimated_natural_search_uv": search_uv,
"estimated_natural_search_cost": search_cost,
}