"""Build a deterministic aggregate for the known Beacon schemas.""" import sys tables = { 'native': [('public.packets', 'packet_hash'), ('public.channel_messages', 'id'), ('public.channels', 'id')], 'projection': [('ukmesh_public.state', 'singleton'), ('ukmesh_public.channels', 'id'), ('ukmesh_public.messages', 'id'), ('ukmesh_public.observations', 'event_id')], } layout = sys.argv[1] selected = tables['native'] + tables['projection'] if layout == 'combined' else tables[layout] fields = [] for relation, key in selected: fields.append(f"""'{relation}', jsonb_build_object( 'rows', (SELECT count(*) FROM {relation}), 'sample_sha256', (SELECT encode(sha256(convert_to( COALESCE(jsonb_agg(to_jsonb(s) ORDER BY s.{key}), '[]')::text, 'UTF8')), 'hex') FROM (SELECT * FROM {relation} ORDER BY {key} LIMIT 256) s))""") print('SELECT jsonb_build_object(' + ','.join(fields) + ');')