ЛР2 на 4: PostgreSQL, Argon2id, Redis-сессии и persistence.
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Compose-стенд, student-tests (expiry/EXPLAIN), REPORT и шпаргалка DEFENSE.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// Утилита для снятия EXPLAIN (ANALYZE, BUFFERS) в evidence/ без трека 5.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dsn := flag.String("database-url", os.Getenv("DATABASE_URL"), "PostgreSQL DSN")
|
||||
out := flag.String("out", "evidence/explain-history.json", "output file")
|
||||
minMessages := flag.Int("min-messages", 10000, "minimum messages in largest chat")
|
||||
flag.Parse()
|
||||
if *dsn == "" {
|
||||
fmt.Fprintln(os.Stderr, "set DATABASE_URL or -database-url")
|
||||
os.Exit(1)
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, *dsn)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
var chatID uuid.UUID
|
||||
var count int
|
||||
err = pool.QueryRow(ctx, `
|
||||
SELECT chat_id, count(*)::int FROM messages GROUP BY chat_id ORDER BY count(*) DESC LIMIT 1`).
|
||||
Scan(&chatID, &count)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "need seeded messages:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if count < *minMessages {
|
||||
fmt.Fprintf(os.Stderr, "largest chat has %d messages, need %d (run student TestMessagesHistoryIndexExplain or load)\n", count, *minMessages)
|
||||
os.Exit(1)
|
||||
}
|
||||
var curAt time.Time
|
||||
var curID uuid.UUID
|
||||
off := count / 2
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT created_at, id FROM messages
|
||||
WHERE chat_id = $1 ORDER BY created_at, id OFFSET $2 LIMIT 1`, chatID, off).
|
||||
Scan(&curAt, &curID); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var plan []byte
|
||||
if err := pool.QueryRow(ctx, `
|
||||
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51`, chatID, curAt, curID).Scan(&plan); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
planStr := string(plan)
|
||||
if strings.Contains(strings.ToLower(planStr), "seq scan on messages") {
|
||||
fmt.Fprintln(os.Stderr, "plan uses seq scan")
|
||||
os.Exit(1)
|
||||
}
|
||||
report := map[string]any{
|
||||
"chat_id": chatID.String(),
|
||||
"message_count": count,
|
||||
"cursor_offset": off,
|
||||
"generated_at": time.Now().UTC().Format(time.RFC3339),
|
||||
"index_expected": "messages_history_idx",
|
||||
"explain_analyze": json.RawMessage(plan),
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(*out), 0o755); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
b, err := json.MarshalIndent(report, "", " ")
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := os.WriteFile(*out, b, 0o644); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("wrote %s chat=%s messages=%d\n", *out, chatID, count)
|
||||
}
|
||||
Reference in New Issue
Block a user