+87
@@ -0,0 +1,87 @@
|
||||
"""Synthetic data seeder for EXPLAIN evidence (never used by the public smoke).
|
||||
|
||||
Runs INSIDE the api container so it can reach PostgreSQL over the compose
|
||||
network:
|
||||
|
||||
docker compose exec -T api python -m src.seed --messages 10000
|
||||
|
||||
It writes a small number of users/chats and one "hot" chat with many messages,
|
||||
then prints the hot chat id so the operator can run EXPLAIN against it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import datetime as dt
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from .db import pool
|
||||
|
||||
|
||||
def _now() -> dt.datetime:
|
||||
return dt.datetime.now(dt.timezone.utc)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Seed synthetic chat history")
|
||||
parser.add_argument("--users", type=int, default=10)
|
||||
parser.add_argument("--chats", type=int, default=3)
|
||||
parser.add_argument("--messages", type=int, default=10000)
|
||||
args = parser.parse_args()
|
||||
|
||||
user_ids = [str(uuid.uuid4()) for _ in range(args.users)]
|
||||
chat_ids = [str(uuid.uuid4()) for _ in range(args.chats)]
|
||||
base = _now()
|
||||
|
||||
with pool().connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.executemany(
|
||||
"""
|
||||
insert into users (id, username, display_name, password_hash, created_at)
|
||||
values (%s, %s, %s, %s, %s)
|
||||
""",
|
||||
[
|
||||
(uid, f"seed_{i}_{uuid.uuid4().hex[:6]}", f"Seed {i}", "seed-not-loginable", base)
|
||||
for i, uid in enumerate(user_ids)
|
||||
],
|
||||
)
|
||||
cur.executemany(
|
||||
"insert into chats (id, title, created_at) values (%s, %s, %s)",
|
||||
[(cid, f"Seed chat {i}", base) for i, cid in enumerate(chat_ids)],
|
||||
)
|
||||
cur.executemany(
|
||||
"insert into chat_members (chat_id, user_id) values (%s, %s)",
|
||||
[(cid, uid) for cid in chat_ids for uid in user_ids],
|
||||
)
|
||||
|
||||
hot_chat = chat_ids[0]
|
||||
per_chat = max(1, args.messages // len(chat_ids))
|
||||
rows = []
|
||||
total = 0
|
||||
for chat_index, cid in enumerate(chat_ids):
|
||||
count = args.messages - total if chat_index == len(chat_ids) - 1 else per_chat
|
||||
count = max(0, count)
|
||||
for _ in range(count):
|
||||
rows.append(
|
||||
(
|
||||
str(uuid.uuid4()),
|
||||
cid,
|
||||
user_ids[0],
|
||||
"synthetic history message",
|
||||
base,
|
||||
)
|
||||
)
|
||||
total += count
|
||||
cur.executemany(
|
||||
"""
|
||||
insert into messages (id, chat_id, sender_id, text, created_at)
|
||||
values (%s, %s, %s, %s, %s)
|
||||
""",
|
||||
rows,
|
||||
)
|
||||
|
||||
print(json.dumps({"hot_chat_id": hot_chat, "messages": args.messages, "chats": chat_ids}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user