Skip to content

Commit d59003f

Browse files
committed
Optimizations
1 parent 5b55ef4 commit d59003f

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/command-handlers/stats/getOverallHistory.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function getOverallHistory(
2727
limit?: number,
2828
): Promise<OverallHistoryEntry[]> {
2929
try {
30-
// Get overall match history for the queue
30+
// Get overall match history with players in a single query using JSON aggregation
3131
let query = `
3232
SELECT
3333
m.id as match_id,
@@ -36,9 +36,22 @@ export async function getOverallHistory(
3636
m.stake,
3737
m.best_of_3,
3838
m.best_of_5,
39-
m.created_at
39+
m.created_at,
40+
COALESCE(
41+
json_agg(
42+
json_build_object(
43+
'user_id', mu.user_id,
44+
'team', mu.team,
45+
'elo_change', mu.elo_change
46+
)
47+
ORDER BY mu.team, mu.user_id
48+
) FILTER (WHERE mu.user_id IS NOT NULL),
49+
'[]'::json
50+
) as players
4051
FROM matches m
52+
LEFT JOIN match_users mu ON m.id = mu.match_id
4153
WHERE m.queue_id = $1 AND m.winning_team IS NOT NULL
54+
GROUP BY m.id, m.winning_team, m.deck, m.stake, m.best_of_3, m.best_of_5, m.created_at
4255
ORDER BY m.created_at DESC
4356
`
4457

@@ -50,37 +63,16 @@ export async function getOverallHistory(
5063

5164
const historyRes = await pool.query(query, params)
5265

53-
// For each match, get the players
54-
const matches = await Promise.all(
55-
historyRes.rows.map(async (row) => {
56-
const playersRes = await pool.query(
57-
`
58-
SELECT user_id, team, elo_change
59-
FROM match_users
60-
WHERE match_id = $1
61-
ORDER BY team, user_id
62-
`,
63-
[row.match_id],
64-
)
65-
66-
return {
67-
match_id: row.match_id,
68-
winning_team: row.winning_team,
69-
deck: row.deck,
70-
stake: row.stake,
71-
best_of_3: row.best_of_3,
72-
best_of_5: row.best_of_5,
73-
created_at: row.created_at.toISOString(),
74-
players: playersRes.rows.map((player) => ({
75-
user_id: player.user_id,
76-
team: player.team,
77-
elo_change: player.elo_change,
78-
})),
79-
}
80-
}),
81-
)
82-
83-
return matches
66+
return historyRes.rows.map((row) => ({
67+
match_id: row.match_id,
68+
winning_team: row.winning_team,
69+
deck: row.deck,
70+
stake: row.stake,
71+
best_of_3: row.best_of_3,
72+
best_of_5: row.best_of_5,
73+
created_at: row.created_at.toISOString(),
74+
players: row.players,
75+
}))
8476
} catch (error) {
8577
console.error('Error fetching overall match history:', error)
8678
throw error

src/db.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ dotenv.config()
55

66
export const pool = new Pool({
77
connectionString: process.env.DATABASE_URL,
8+
max: 20, // Increase pool size from default 10 to handle bot + API load
9+
idleTimeoutMillis: 30000, // Close idle connections after 30 seconds
10+
connectionTimeoutMillis: 5000, // Fail fast if no connection available
11+
allowExitOnIdle: false, // Keep pool alive
812
})

0 commit comments

Comments
 (0)