-
Notifications
You must be signed in to change notification settings - Fork 0
Speed up dashboard navigation #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,15 @@ import { publicId } from '$lib/server/public-id'; | |
|
|
||
| type RunStatus = (typeof runStatusEnum.enumValues)[number]; | ||
|
|
||
| function nowMs() { | ||
| return performance.now(); | ||
| } | ||
|
|
||
| function logTiming(label: string, startedAt: number) { | ||
| if (process.env.NODE_ENV === 'production') return; | ||
| console.info(`[runs] ${label} ${Math.round(performance.now() - startedAt)}ms`); | ||
| } | ||
|
Comment on lines
+18
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| export async function createRun(input: { | ||
| ownerUserId: string; | ||
| schemaVersion?: string; | ||
|
|
@@ -118,6 +127,7 @@ export async function listRuns(input: { | |
| limit: number; | ||
| offset: number; | ||
| }) { | ||
| const startedAt = nowMs(); | ||
| const filters = [ | ||
| eq(runs.ownerUserId, input.ownerUserId), | ||
| input.status ? eq(runs.status, input.status) : undefined, | ||
|
|
@@ -131,61 +141,68 @@ export async function listRuns(input: { | |
| ].filter(Boolean); | ||
| const where = filters.length ? and(...filters) : undefined; | ||
|
|
||
| const rows = await db | ||
| .select({ | ||
| id: runs.publicId, | ||
| name: runs.name, | ||
| goal: runs.goal, | ||
| status: runs.status, | ||
| agentName: runs.agentName, | ||
| agentVersion: runs.agentVersion, | ||
| environment: runs.environment, | ||
| startedAt: runs.startedAt, | ||
| endedAt: runs.endedAt, | ||
| eventCount: sql<number>`count(distinct ${events.id})`, | ||
| latestEvaluationScore: sql<number | null>`( | ||
| const [rows, [{ total }]] = await Promise.all([ | ||
| db | ||
| .select({ | ||
| id: runs.publicId, | ||
| name: runs.name, | ||
| goal: runs.goal, | ||
| status: runs.status, | ||
| agentName: runs.agentName, | ||
| agentVersion: runs.agentVersion, | ||
| environment: runs.environment, | ||
| startedAt: runs.startedAt, | ||
| endedAt: runs.endedAt, | ||
| eventCount: sql<number>`( | ||
| select count(*) | ||
| from ${events} | ||
| where ${events.runId} = ${runs.id} | ||
| )`, | ||
| latestEvaluationScore: sql<number | null>`( | ||
| select ${evaluations.score} | ||
| from ${evaluations} | ||
| where ${evaluations.runId} = ${runs.id} | ||
| order by ${evaluations.createdAt} desc | ||
| limit 1 | ||
| )`, | ||
| latestEvaluationStatus: sql<string | null>`( | ||
| latestEvaluationStatus: sql<string | null>`( | ||
| select ${evaluations.status} | ||
| from ${evaluations} | ||
| where ${evaluations.runId} = ${runs.id} | ||
| order by ${evaluations.createdAt} desc | ||
| limit 1 | ||
| )` | ||
| }) | ||
| .from(runs) | ||
| .leftJoin(events, eq(events.runId, runs.id)) | ||
| .where(where) | ||
| .groupBy(runs.id) | ||
| .orderBy(desc(runs.startedAt)) | ||
| .limit(input.limit) | ||
| .offset(input.offset); | ||
|
|
||
| const [{ total }] = await db.select({ total: count() }).from(runs).where(where); | ||
| }) | ||
| .from(runs) | ||
| .where(where) | ||
| .orderBy(desc(runs.startedAt)) | ||
| .limit(input.limit) | ||
| .offset(input.offset), | ||
| db.select({ total: count() }).from(runs).where(where) | ||
| ]); | ||
| logTiming('listRuns', startedAt); | ||
| return { runs: rows, total }; | ||
| } | ||
|
|
||
| export async function getRunDashboardMetricsForUser(ownerUserId: string) { | ||
| const rows = await db | ||
| .select({ status: runs.status, total: count() }) | ||
| .from(runs) | ||
| .where(eq(runs.ownerUserId, ownerUserId)) | ||
| .groupBy(runs.status); | ||
| const warningRows = await db | ||
| .select({ total: count() }) | ||
| .from(evaluationFindings) | ||
| .innerJoin(runs, eq(evaluationFindings.runId, runs.id)) | ||
| .where( | ||
| and( | ||
| eq(runs.ownerUserId, ownerUserId), | ||
| or(eq(evaluationFindings.severity, 'warning'), eq(evaluationFindings.severity, 'error')) | ||
| const startedAt = nowMs(); | ||
| const [rows, warningRows] = await Promise.all([ | ||
| db | ||
| .select({ status: runs.status, total: count() }) | ||
| .from(runs) | ||
| .where(eq(runs.ownerUserId, ownerUserId)) | ||
| .groupBy(runs.status), | ||
| db | ||
| .select({ total: count() }) | ||
| .from(evaluationFindings) | ||
| .innerJoin(runs, eq(evaluationFindings.runId, runs.id)) | ||
| .where( | ||
| and( | ||
| eq(runs.ownerUserId, ownerUserId), | ||
| or(eq(evaluationFindings.severity, 'warning'), eq(evaluationFindings.severity, 'error')) | ||
| ) | ||
| ) | ||
| ); | ||
| ]); | ||
|
|
||
| const metrics = { | ||
| running: 0, | ||
|
|
@@ -197,5 +214,6 @@ export async function getRunDashboardMetricsForUser(ownerUserId: string) { | |
| for (const row of rows) metrics[row.status] = row.total; | ||
| const completed = metrics.success + metrics.failed + metrics.cancelled; | ||
| const successRate = completed ? Math.round((metrics.success / completed) * 100) : 0; | ||
| logTiming('getRunDashboardMetricsForUser', startedAt); | ||
| return { ...metrics, successRate }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nowMs()is defined identically in bothruns.tsand+page.server.ts. Extracting it to a shared utility avoids drift if the implementation ever needs to change.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!