⚡ Bolt: Optimize sequential DB queries#121
Conversation
💡 What: Grouped independent database queries in src/pages/api/work-diagnostic.ts using Promise.all(). 🎯 Why: Sequential queries that do not depend on each other add unnecessary latency. By executing them concurrently, we reduce the total DB wait time. 📊 Impact: Reduces the wait time for fetching Project, AgentInstruction, and AgentBudget from the sum of their individual execution times to just the duration of the longest query. 🔬 Measurement: Verify by executing the API endpoint /api/work-diagnostic and observing improved response times compared to baseline. Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Code Review
This pull request optimizes the src/pages/api/work-diagnostic.ts endpoint by fetching projects, agents, and budgets concurrently using Promise.all, and documents this change in .jules/bolt.md. The reviewer suggests further optimizing this endpoint by grouping other independent asynchronous operations—such as the costs query, logs query, and work status fetch—into the same Promise.all call to minimize latency.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| const [projects, agents, budgets] = await Promise.all([ | ||
| db.select().from(Project), | ||
| db.select().from(AgentInstruction), | ||
| db.select().from(AgentBudget), | ||
| ]); |
There was a problem hiding this comment.
While grouping the Project, AgentInstruction, and AgentBudget queries into Promise.all is a great step, the API route still executes several independent asynchronous operations sequentially further down:
- The
costsquery (lines 20-25) - The
logsquery (lines 30-40) - The
workstatus fetch (line 42)
Since none of these queries depend on the results of each other, they can all be executed concurrently. We can compute startOfMonth first, and then run all 6 queries/fetches in a single Promise.all to minimize the response latency of this endpoint.
Here is how the fully optimized code would look:
export const GET: APIRoute = async () => {
try {
const { db, Project, AgentInstruction, ActivityLog, AgentBudget, CostEvent, sql } = await loadAstroDb();
const now = new Date();
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
// Fetch all independent data concurrently
const [projects, agents, budgets, costsResult, logs, work] = await Promise.all([
db.select().from(Project),
db.select().from(AgentInstruction),
db.select().from(AgentBudget),
db.select({
total: sql`sum(${CostEvent.costCents})`,
})
.from(CostEvent)
.where(sql`${CostEvent.occurredAt} >= ${startOfMonth}`),
db.select()
.from(ActivityLog)
.where(
or(
eq(ActivityLog.action, 'swarm.task.dispatch_failed'),
eq(ActivityLog.action, 'swarm.issue.dispatch_failed'),
),
)
.orderBy(desc(ActivityLog.createdAt))
.limit(20),
getWorkSystemStatus(),
]);
const activeBudgets = budgets.filter((b) => b.enabled === 1);
const currentMonthlyCents = Number(costsResult[0]?.total || 0);
const globalHardStopCents =
activeBudgets.reduce((acc, b) => acc + Number(b.monthlyCents || 0), 0) || 5000;
💡 What: Grouped independent database queries in src/pages/api/work-diagnostic.ts using Promise.all().
🎯 Why: Sequential queries that do not depend on each other add unnecessary latency. By executing them concurrently, we reduce the total DB wait time.
📊 Impact: Reduces the wait time for fetching Project, AgentInstruction, and AgentBudget from the sum of their individual execution times to just the duration of the longest query.
🔬 Measurement: Verify by executing the API endpoint /api/work-diagnostic and observing improved response times compared to baseline.
PR created automatically by Jules for task 4427519362897185717 started by @bobdivx