Files
mtusi2026lab5/scripts/final_state.py
T

37 lines
2.2 KiB
Python

"""Финальные агрегаты собственного стенда без пользовательских данных и секретов."""
import json
from pathlib import Path
import subprocess
import re
ROOT = Path(__file__).resolve().parents[1]
load = json.loads((ROOT / 'evidence/load-pipeline.json').read_text(encoding='utf-8'))
failed_ids = sorted(set(re.findall(r'[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}',
' '.join(error for row in load for error in row['failures']))))
script = 'FAILED_IDS=' + repr(failed_ids) + '\n' + '''
import json
from src.storage import storage
storage.pool.open(wait=True)
try:
with storage.pool.connection() as conn:
result = {
'attachments': conn.execute('SELECT state,count(*) AS count FROM attachments GROUP BY state').fetchall(),
'jobs': conn.execute('SELECT state,pipeline_version,count(*) AS count FROM attachment_jobs GROUP BY state,pipeline_version ORDER BY pipeline_version,state').fetchall(),
'replays': conn.execute('SELECT count(*) AS count FROM attachment_replays').fetchone()['count'],
'load_observation_errors': conn.execute('SELECT id,state,published_pipeline_version,target_pipeline_version FROM attachments WHERE id=ANY(%s::uuid[])', (FAILED_IDS,)).fetchall(),
'violations': conn.execute("""SELECT count(*) AS count FROM attachments a
WHERE state <> 'ready' OR published_pipeline_version <> target_pipeline_version
OR (SELECT count(*) FROM attachment_variants v WHERE v.attachment_id=a.id
AND v.pipeline_version=a.published_pipeline_version) <> 3""").fetchone()['count']
}
print(json.dumps(result, default=str))
finally:
storage.pool.close()
'''
output = subprocess.check_output(['docker','compose','-p','lyakhonov-lab5','exec','-T','--index','1','api','python','-'],
input=script, cwd=ROOT, text=True, encoding='utf-8')
result = json.loads(output)
(ROOT / 'evidence/final-state.json').write_text(json.dumps(result, indent=2), encoding='utf-8')
assert result['violations'] == 0, 'Incomplete or inconsistent current manifests'
print(output)