#!/usr/bin/env python
# coding=utf-8
"""
Step2: 量化影响面 - 多少视频有这个问题，占比多少
"""
import pandas as pd
from pathlib import Path

# 读取数据
data_file = Path(__file__).parent / "output" / "step2_影响面_20260101_20260113.csv"
df = pd.read_csv(data_file)

print("=" * 70)
print("Step2: 量化影响面")
print("=" * 70)

# 1. 数据概览
print("\n【1. 数据概览】")
print(f"样本数: {len(df)} 条（item x 天）")
print(f"唯一视频数: {df['vid'].nunique()}")
print(f"日期范围: {df['dt'].min()} ~ {df['dt'].max()}")
print(f"天数: {df['dt'].nunique()}")

# 2. 定义问题item
# 使用整体vov0中位数作为阈值
median_vov = df['vov0'].median()
print(f"\n【2. 问题定义】")
print(f"vov0中位数: {median_vov:.4f}")
print(f"问题定义: 曝光rank<=10 且 vov0<{median_vov:.4f}")

# 3. 统计问题item
top10 = df[df['曝光rank'] <= 10]
problem_items = top10[top10['vov0'] < median_vov]

print(f"\n【3. 问题item统计】")
print(f"Top10总记录数: {len(top10)}")
print(f"问题记录数: {len(problem_items)}")
print(f"问题占比: {len(problem_items)/len(top10)*100:.1f}%")

# 4. 按天统计
print(f"\n【4. 按天统计问题占比】")
daily_stats = []
for dt in sorted(df['dt'].unique()):
    day_top10 = df[(df['dt'] == dt) & (df['曝光rank'] <= 10)]
    day_problem = day_top10[day_top10['vov0'] < median_vov]
    daily_stats.append({
        'dt': dt,
        'top10_count': len(day_top10),
        'problem_count': len(day_problem),
        'problem_ratio': len(day_problem) / len(day_top10) * 100 if len(day_top10) > 0 else 0
    })
daily_df = pd.DataFrame(daily_stats)
print(daily_df.to_string(index=False))

# 5. 问题视频列表（去重）
print(f"\n【5. 问题视频列表（去重）】")
problem_vids = problem_items.groupby('vid').agg({
    'dt': 'count',
    'vov0': 'mean',
    '曝光rank': 'mean',
    'exp': 'sum',
    '标题': 'first'
}).reset_index()
problem_vids.columns = ['vid', '出现天数', '平均vov0', '平均rank', '总曝光', '标题']
problem_vids = problem_vids.sort_values('出现天数', ascending=False)
print(f"问题视频数: {len(problem_vids)}")
print(problem_vids.to_string(index=False))

# 6. 影响的曝光量
print(f"\n【6. 影响面量化】")
total_exp_top10 = top10['exp'].sum()
problem_exp = problem_items['exp'].sum()
print(f"Top10总曝光: {total_exp_top10:,.0f}")
print(f"问题item曝光: {problem_exp:,.0f}")
print(f"问题曝光占比: {problem_exp/total_exp_top10*100:.1f}%")

# 保存结果
print("\n" + "=" * 70)
print("【Step2 结论】")
print("=" * 70)
print(f"1. 问题占比: {len(problem_items)}/{len(top10)} ({len(problem_items)/len(top10)*100:.1f}%) 的Top10记录存在低vov高曝光")
print(f"2. 问题视频: {len(problem_vids)} 个视频至少出现过1次问题")
print(f"3. 影响曝光: {problem_exp:,.0f} ({problem_exp/total_exp_top10*100:.1f}% 的Top10曝光)")

# 保存到文件
output_dir = Path(__file__).parent / "output"

# 保存问题视频列表
problem_vids.to_csv(output_dir / "step2_问题视频列表.csv", index=False)

# 保存每日统计
daily_df.to_csv(output_dir / "step2_每日统计.csv", index=False)

# 保存结论
with open(output_dir / "step2_结论.txt", 'w', encoding='utf-8') as f:
    f.write("=" * 70 + "\n")
    f.write("Step2: 量化影响面\n")
    f.write("=" * 70 + "\n\n")

    f.write("【数据范围】\n")
    f.write(f"日期: {df['dt'].min()} ~ {df['dt'].max()} ({df['dt'].nunique()}天)\n")
    f.write(f"样本数: {len(df)} 条\n\n")

    f.write("【问题定义】\n")
    f.write(f"曝光rank<=10 且 vov0<{median_vov:.4f}(中位数)\n\n")

    f.write("【影响面统计】\n")
    f.write(f"问题记录占比: {len(problem_items)}/{len(top10)} = {len(problem_items)/len(top10)*100:.1f}%\n")
    f.write(f"问题视频数: {len(problem_vids)} 个\n")
    f.write(f"影响曝光量: {problem_exp:,.0f} ({problem_exp/total_exp_top10*100:.1f}%)\n\n")

    f.write("【每日问题占比】\n")
    f.write(daily_df.to_string(index=False))

print(f"\n结果已保存到 output/ 目录")
