41 lines
2.8 KiB
Python
41 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Summarize observed alert transitions; sampling bounds detection precision."""
|
|
import json
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
data = json.loads((root / 'evidence/slo-incidents.json').read_text())
|
|
samples = sorted((s for s in data['samples'] if 'firing' in s), key=lambda s: s['monotonic'])
|
|
alerts = ['MessengerApiBurnFast', 'MessengerApiBurnSlow']
|
|
output = {'sampling_interval_seconds': 5, 'monitor_errors': sum('monitor_error' in s for s in data['samples']), 'phases': {}}
|
|
drifts = [abs((datetime.fromisoformat(b['time']) - datetime.fromisoformat(a['time'])).total_seconds()
|
|
- (b['monotonic'] - a['monotonic'])) for a, b in zip(samples, samples[1:])]
|
|
gaps = [b['monotonic'] - a['monotonic'] for a, b in zip(samples, samples[1:])]
|
|
output['timing_quality'] = {'max_wall_monotonic_disagreement_seconds': round(max(drifts, default=0), 3),
|
|
'max_observed_sampling_gap_seconds': round(max(gaps, default=0), 3),
|
|
'valid_for_elapsed_time_claims': max(drifts, default=0) <= 1,
|
|
'note': 'Disagreement does not identify its cause; rerun if invalid. Precision is bounded by actual gaps, not the configured sleep.'}
|
|
base = [s for s in samples if s['phase'] == 'baseline']
|
|
output['baseline'] = {'samples': len(base), 'samples_with_burn_alert': sum(bool(s['firing']) for s in base), 'scope': 'one observed healthy baseline, not a long-term false-positive rate'}
|
|
for name, period in data['periods'].items():
|
|
start, end = period['lock_observed']['monotonic'], period['command_completed']['monotonic']
|
|
active = [s for s in samples if start <= s['monotonic'] <= end]
|
|
recovery = [s for s in samples if s['monotonic'] > end and s['phase'] == name + '_recovery']
|
|
previous = [s for s in samples if s['monotonic'] < start]
|
|
row = {'observed_lock_seconds': round(end-start, 3), 'alerts': {}}
|
|
for alert in alerts:
|
|
before = bool(previous and alert in previous[-1]['firing'])
|
|
hits = [s for s in active + recovery if alert in s['firing']]
|
|
clear = [s for s in recovery if hits and s['monotonic'] > hits[0]['monotonic'] and alert not in s['firing']]
|
|
row['alerts'][alert] = {'active_before_lock': before,
|
|
'first_firing': hits[0]['time'] if hits else None,
|
|
'first_firing_after_incident': bool(hits and hits[0]['monotonic'] > end),
|
|
'detection_seconds': round(hits[0]['monotonic']-start, 3) if hits and not before else None,
|
|
'first_clear': clear[0]['time'] if hits and clear else None,
|
|
'recovery_seconds': round(clear[0]['monotonic']-end, 3) if hits and clear else None}
|
|
output['phases'][name] = row
|
|
path = root / 'evidence/slo-analysis.json'
|
|
path.write_text(json.dumps(output, ensure_ascii=False, indent=2) + '\n')
|
|
print(json.dumps(output, ensure_ascii=False, indent=2))
|