Files
mtusi2026lab5/src/pipeline_metrics.py
T
2026-10-26 15:49:31 +03:00

38 lines
1.5 KiB
Python

"""Operator-only metrics for one synthetic chat; never exposes credentials."""
import asyncio
import json
import os
from pathlib import Path
import sys
from uuid import UUID
from .config import load_settings
from .storage.postgres import PostgresStore
async def main(chat):
store = PostgresStore(load_settings().database_url)
await store.connect()
try:
rows = await store.pool.fetch('SELECT a.id, a.status, extract(epoch from (j.first_started_at-j.created_at))::float8 AS queue_wait_seconds FROM attachments a JOIN attachment_jobs j ON j.attachment_id=a.id WHERE a.chat_id=$1', chat)
jobs = [dict(row, id=str(row['id'])) for row in rows]
pending = await store.pool.fetchval("SELECT count(*) FROM attachment_jobs WHERE status IN ('queued','processing')")
finally:
await store.close()
rss = 0
for path in Path('/proc').glob('[0-9]*/status'):
try:
if int(path.parent.name) == os.getpid():
continue
for line in path.read_text().splitlines():
if line.startswith('VmRSS:'):
rss += int(line.split()[1]) * 1024
except (OSError, ValueError):
pass
print(json.dumps({'jobs': jobs, 'pending': pending, 'rss_bytes': rss,
'concurrency': int(os.environ['WORKER_CONCURRENCY']),
'prefetch': int(os.environ['WORKER_PREFETCH'])}))
if __name__ == '__main__':
asyncio.run(main(UUID(sys.argv[1])))