Files
mtusi2026lab6/student-tests/slo_analyze.py
T
2026-11-06 09:39:53 +03:00

33 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Summarize observed alert transitions; sampling bounds detection precision."""
import json
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': {}}
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))