#!/usr/bin/env python3 import sys,os,platform,json,subprocess,datetime from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parent)) from experiments import command,dc,save info={'collected_at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'host':platform.platform(),'machine':platform.machine(),'logical_cpus':os.cpu_count(),'host_memory_bytes':int(command(['sysctl','-n','hw.memsize']).strip()),'host_cpu':command(['sysctl','-n','machdep.cpu.brand_string']).strip(),'docker_version':command(['docker','version','--format','{{json .Server}}']).strip(),'compose_version':dc('version').strip(),'docker_resources':json.loads(command(['docker','info','--format','{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}},"os":"{{.OperatingSystem}}","architecture":"{{.Architecture}}"}']))} info['dataset']=json.loads(dc('exec','-T','postgres','psql','-U','messenger','-d','messenger','-Atc',"SELECT json_build_object('users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),'messages',(SELECT count(*) FROM messages),'idempotency',(SELECT count(*) FROM idempotency),'database_bytes',pg_database_size(current_database()))")) info['versions']={'python':command(['docker','exec',dc('ps','-q','api').split()[0],'python','--version']).strip(),'postgres':dc('exec','-T','postgres','postgres','--version').strip(),'redis':dc('exec','-T','redis','redis-server','--version').strip(),'haproxy':dc('exec','-T','lb','haproxy','-v').strip()} # Hash inputs so measurements can be tied to saved sources without publishing secrets. import hashlib info['source_sha256']={str(p):hashlib.sha256(p.read_bytes()).hexdigest() for pattern in ('app/*.py','migrations/*.sql','infra/*.cfg','scripts/experiments.py','compose.yaml','Dockerfile','requirements.lock') for p in Path('.').glob(pattern)} save(sys.argv[1] if len(sys.argv)>1 else 'environment.json',info) print('Sanitized environment and source hashes saved.')