Files
treeandClaude Opus 5.5 e9616af34d 修复 P0:根因判定、退服率口径、影响评估与告警关联量化
- 根因判定改为三级(衍生关系/时间窗/疑似)并带置信度;小区退服告警 ±2 分钟匹配;
  衍生告警按父告警归类;优先选随业务恢复清除的告警;时间窗按历史流水 95% 分位数标定
- 退服率按日历统计周期计算;站点数分母支持站名关键字;重复退服率除以全部站点;
  月均按统计周期自然月数;导出与看板同一口径
- 影响评估新增受影响用户·小时与 500 米邻站覆盖补偿
- 新增告警—退服关联量化(支持度/置信度/提升度)及 /api/association,根因页展示
- 新增 tests/(扰动数据集留出检验、指标口径检验)与 bench.py;旧库启动时自动迁移

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:43:34 +08:00

88 lines
4.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""考核指标口径、影响评估与关联量化检验(仓库样例数据)。
运行:python tests/test_kpi.py
"""
import calendar
from harness import check, core, finish, load, sample_data # 先导入 harness:它把数据库指向 tests/_work
import app as webapp # noqa: E402
s, a, o = sample_data()
full = load('kpi', s, a, o)
N = core.site_count()
DAYS = 273 # 样例数据 2026-01-01 ~ 2026-09-30
def calendar_rate(df, n_sites, days):
return round(df['duration_min'].sum() / (n_sites * days * 1440) * 100, 4)
print('== 1. 统计周期')
minutes, months = core.period()
check('全量周期 = 273 天、9 个月', (round(minutes / 1440), months) == (DAYS, 9), f'({minutes / 1440} 天, {months} 月)')
check('全量退服率按日历周期', core.kpis(full, N)['outage_rate'] == calendar_rate(full, N, DAYS))
for m in ['2026-02', '2026-07']:
f = {'month': m}
days = calendar.monthrange(*map(int, m.split('-')))[1]
k = core.kpis(core.frame(f), N, *core.period(f))
check(f'{m} 月退服率按 {days} 天', k['outage_rate'] == calendar_rate(core.frame(f), N, days), f'({k["outage_rate"]}%)')
print('\n== 2. 稀疏筛选')
counts = full.groupby('ne_id').size().sort_values()
ne = counts.index[0]
f = {'site': ne}
df = core.frame(f)
k = core.kpis(df, core.site_count(f), *core.period(f))
check('单站筛选分母为 1 个站', k['sites'] == 1, f'({k["sites"]})')
check('单站退服率按 273 天', k['outage_rate'] == calendar_rate(df, 1, DAYS), f'({k["outage_rate"]}%)')
day = df['start_time'].iloc[0][:10]
f = {'site': ne, 'date_from': day, 'date_to': day}
df = core.frame(f)
k = core.kpis(df, core.site_count(f), *core.period(f))
check('单站单日退服率按 1 站 × 1 天', k['outage_rate'] == calendar_rate(df, 1, 1), f'({k["outage_rate"]}%)')
print('\n== 3. 月均口径:单独查看一个站与全量查看判定一致')
whole = core.site_stats(full, months).set_index('ne_id')
flips = 0
for ne in whole.index:
f = {'site': ne}
one = core.site_stats(core.frame(f), core.period(f)[1]).set_index('ne_id').loc[ne]
flips += (bool(one['long']) != bool(whole.loc[ne, 'long'])) + (bool(one['repeat']) != bool(whole.loc[ne, 'repeat']))
check('超长 / 高频判定翻转数为 0', flips == 0, f'({flips})')
check('重复退服率分母为全部站点', core.kpis(full, N)['repeat_rate'] == round(whole['repeat'].sum() / N * 100, 2))
print('\n== 4. 导出与看板同一口径')
client = webapp.app.test_client()
client.post('/login', json={'username': 'admin', 'password': 'admin123'})
for scene in core.SCENES:
dash = client.get('/api/dashboard', query_string={'scene': scene}).get_json()['kpi']['outage_rate']
f = {'scene': scene}
exported = core.kpis(core.frame(f), core.site_count(f), *core.period(f))['outage_rate']
check(f'{scene} 看板 = 导出', dash == exported, f'({dash}% / {exported}%)')
response = client.get('/api/export/stats', query_string={'scene': '商圈'})
check('导出接口可用', response.status_code == 200)
print('\n== 5. 影响评估')
corr = full['user_hours'].corr(full['duration_min'])
check('受影响用户·小时与时长正相关(修复前受影响用户与时长 r = 0.03)', corr > 0.5, f'(r = {corr:.3f})')
expected = full['affected_users'] * full['duration_min'] / 60
relative = ((full['user_hours'] - expected).abs() / expected.clip(lower=1)).max()
check('用户·小时 = 受影响用户 × 时长', relative < 0.02, f'(最大相对误差 {relative:.4f},来自用户数取整)')
check('覆盖补偿系数在 [0.4, 1]', full['cover_factor'].between(core.COVER_FLOOR, 1).all(),
f'(均值 {full["cover_factor"].mean():.2f})')
isolated = core.site_cover()
check('有邻站的站补偿系数 < 1', min(isolated.values()) < 1)
print(f' 用户·小时合计 {core.kpis(full, N)["user_h"]:,.0f},覆盖补偿系数分布 '
f'{full.drop_duplicates("ne_id")["cover_factor"].value_counts().sort_index().to_dict()}')
print('\n== 6. 告警—退服关联量化')
rows = core.association()
for r in rows:
print(f' {r["category"]:<6} 窗口 {r["window_min"]:>6} 分 告警 {r["alarms"]:>5} 支持度 {r["support"]:>6}% '
f'置信度 {r["confidence"]:>6}% 提升度 {r["lift"]:>6}')
check('6 类告警均有关联指标', len(rows) == 6 and all(r['alarms'] > 0 for r in rows))
check('各类提升度 > 1(告警出现后退服概率高于随机)', all(r['lift'] > 1 for r in rows))
check('关联接口可用', client.get('/api/association').status_code == 200)
finish()