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, }