Lab2 grade 4: PostgreSQL, Redis sessions, Compose, student tests and evidence.

This commit is contained in:
2026-10-30 12:37:18 +03:00
parent 65003b6119
commit 950ff93573
34 changed files with 2252 additions and 41 deletions
+16
View File
@@ -0,0 +1,16 @@
@echo off
setlocal
cd /d "%~dp0\.."
docker compose --env-file .env restart postgres redis api
if errorlevel 1 exit /b 1
set /a i=0
:wait
docker compose --env-file .env exec -T api python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" >nul 2>&1
if not errorlevel 1 exit /b 0
set /a i+=1
if %i% geq 60 (
echo restart: API not ready within 60s >&2
exit /b 1
)
timeout /t 1 /nobreak >nul
goto wait
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
# Restart API and all state stores while preserving volumes.
# Readiness is waited by tests/resilience.py separately (do not block here forever).
cd "$(dirname "$0")/.."
docker compose --env-file .env restart postgres redis api
exit 0
+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
"""Seed >=10k messages and print EXPLAIN (ANALYZE, BUFFERS) for keyset page."""
from __future__ import annotations
import uuid
from datetime import datetime, timezone
import psycopg
from psycopg.rows import dict_row
def utc_now():
return datetime.now(timezone.utc).replace(microsecond=0)
def main() -> None:
url = open("/run/secrets/database_url", encoding="utf-8").read().strip()
with psycopg.connect(url, row_factory=dict_row) as conn:
with conn.transaction():
u1, u2 = uuid.uuid4(), uuid.uuid4()
now = utc_now()
conn.execute(
"""
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES (%s, %s, %s, %s, %s)
""",
(u1, f"explain_a_{u1.hex[:8]}", "A", "x", now),
)
conn.execute(
"""
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES (%s, %s, %s, %s, %s)
""",
(u2, f"explain_b_{u2.hex[:8]}", "B", "x", now),
)
chat = uuid.uuid4()
conn.execute(
"INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)",
(chat, "explain", now),
)
conn.execute(
"INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s), (%s, %s)",
(chat, u1, chat, u2),
)
rows = [(uuid.uuid4(), chat, u1, f"m{i}", now) for i in range(10000)]
with conn.cursor() as cur:
cur.executemany(
"""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
VALUES (%s, %s, %s, %s, %s)
""",
rows,
)
mid = rows[5000][0]
plan = conn.execute(
"""
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = %s AND (created_at, id) > (%s, %s)
ORDER BY created_at ASC, id ASC
LIMIT 51
""",
(chat, now, mid),
).fetchall()
for row in plan:
print(next(iter(row.values())))
print("CHAT_ID", chat)
print("SEEDED", 10000)
if __name__ == "__main__":
main()