Implement the following plan: Usage Dashboard Query Optimization Change 1: Partial…
- User
Implement the following plan:
Usage Dashboard Query Optimization
Change 1: Partial indexes for running jobs
- New
internal/repository/migrations/sqlite3/14_running-partial-indexes.up.sql— partial covering indexesWHERE job_state = 'running'for user/project/subcluster groupings. Only running jobs in B-tree (tiny vs millions). - New
internal/repository/migrations/sqlite3/14_running-partial-indexes.down.sql— drop them. internal/repository/migration.go:30—Version13 → 14.internal/repository/jobQuery.go:200-202— inline literal state value (fmt.Sprintf("job.job_state = '%s'", singleStat)) so SQLite matches partial indexes. Safe: values from validated GraphQL enum.
Change 2: Request-scoped cache for grouped stats (6 SQL → 2)
internal/graph/stats_cache.go(new) — definestatsGroupCachetype (wrapssync.Map), context key,getOrComputemethod usingsync.Onceper cache entry.cmd/cc-backend/server.go:87-88— addgraphQLServer.AroundOperationsmiddleware to inject empty cache into context per request.internal/graph/schema.resolvers.go:671-678— inJobsStatistics, whengroupBy != nil: check cache for (filter+groupBy) key → on miss, callJobsStatsGroupedwith nil sortBy/page (all results), cache them → sort+slice in Go per caller's sortBy/page.
Change 3: Parallelize histogram queries
internal/repository/stats.go:643-668— replace 4 sequential histogram calls inAddHistogramswitherrgroup.WithContext. Importgolang.org/x/sync/errgroup. Each goroutine writes distinct struct field.
Change 4: Frontend uses consolidated results
web/frontend/src/status/dashdetails/UsageDash.svelte— replace 6-alias topStatsQuery with 2 aliases (USER, PROJECT) requesting all fields, no sort/page. Add 6$derivedvars to sort+slice top-10. Update template refs.
Verification
bash - New
Assistant
Let me read the key files first.
Assistant
Let me read the remaining files needed.
Assistant
Let me check a few more things before implementing.
Assistant
Now I have all the context. Let me implement all 4 changes. I'll work on them in parallel where possible.
Change 1: Migration files + version bump + inline literal state value
Assistant
Now inline the literal state value in
BuildWhereClausefor single-state filters:Assistant
Change 2: Request-scoped stats cache
Assistant
Now add the AroundOperations middleware in server.go:
Assistant
I need to add the
graphqlimport and export theWithStatsGroupCachefunction. Let me fix the export name first:Assistant
Now add the
graphqlimport in server.go:Assistant
Now update the
JobsStatisticsresolver to use the cache (Change 2 continued):Assistant
Now I need to add the
sortAndPageStatshelper function. Let me add it tostats_cache.go:Assistant
The
slicesimport: I'm usingslices.SortFuncso it should be used. Let me check the Go version:Assistant
Go 1.25 should have
slicesin stdlib. The diagnostic might be stale. Let me verify the build compiles. But first let me do Change 3: Parallelize histogram queries.Assistant
Good,
errgroupis available. Now parallelize the histogram queries inAddHistograms:Assistant
Now replace the sequential histogram calls with parallel ones:
Assistant
The LSP says errgroup is unused but it should be used by the code I just added. Let me run
go mod tidyand verify compilation:Assistant
Change 4: Frontend consolidated query
Now update the UsageDash.svelte to use 2 aliases instead of 6:
Assistant
Now update all template references from
$topStatsQuery.data.topUserJobstotopUserJobs, etc.:Assistant
Now let's run the verification steps — frontend build and backend build + tests in parallel:
Assistant
All 4 changes are implemented and verified:
-
Partial indexes — Migration 14 creates partial covering indexes
WHERE job_state = 'running'for user/project/subcluster groupings. Version bumped to 14. TheBuildWhereClausenow inlines the literal state value (job.job_state = 'running') so SQLite matches the partial index instead of using a parameterized placeholder. -
Request-scoped cache —
stats_cache.goprovides astatsGroupCacheinjected per GraphQL operation viaAroundOperationsmiddleware. When the same(filter, groupBy)pair is requested multiple times (e.g. topUserJobs, topUserNodes, topUserAccs), the DB query runs only once; sort+page is done in Go. -
Parallel histograms —
AddHistogramsnow runs all 4 histogram queries concurrently usingerrgroup.WithContext. -
Frontend consolidation —
UsageDash.sveltesends 2 GraphQL aliases (allUsers, allProjects) requesting all fields, then derives 6 sorted+sliced top-10 lists client-side via$derived.
-
- User
commit it
Assistant
Let me gather the commit info.
Assistant
Committed as
d586fe4b.- User
Does it make sense to also inline the literal state value in other places?
Assistant
Good question. Let me explore where
job_stateis filtered with parameterized values across the codebase to see if there are other hot paths that would benefit.Assistant
The exploration found 4 parameterized sites that filter on a hardcoded
"running"value where inlining could help SQLite use the partial indexes:File Line Function Currently stats.go116 buildCountQuery"job.job_state = ?", "running"jobFind.go280 FindConcurrentJobs"job.job_state = ?", "running"jobFind.go285 FindConcurrentJobs"job.job_state != ?", "running"job.go1069 GetUsedNodessq.Eq{"job.job_state": "running"}However, the benefit is uneven:
stats.go:116(buildCountQuery) — this is the strongest candidate. It's called by / on the dashboard. Inlining here lets SQLite use the partial indexes we just created.
Assistant
Two sites worth inlining (
stats.go:116andjob.go:1069), one intentionally skipped (jobFind.go—!=doesn't match partial indexes, and time overlap dominates selectivity there).