Skip to content

Commit 25556f0

Browse files
committed
chore: Join together tables to prevent excessive queries
1 parent 15c862f commit 25556f0

File tree

4 files changed

+487
-270
lines changed

4 files changed

+487
-270
lines changed

coderd/database/modelqueries.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,35 @@ func (q *sqlQuerier) GetTemplateGroupRoles(ctx context.Context, id uuid.UUID) ([
176176
}
177177

178178
type workspaceQuerier interface {
179-
GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, prepared rbac.PreparedAuthorized) ([]GetWorkspacesRow, error)
179+
GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, prepared rbac.PreparedAuthorized) ([]WorkspaceWithData, error)
180+
}
181+
182+
// WorkspaceWithData includes information returned by the api for a workspace.
183+
type WorkspaceWithData struct {
184+
Workspace
185+
186+
// These template fields are included in the response for a workspace.
187+
// This means if you can read a workspace, you can also read these limited
188+
// template fields as they are metadata of the workspace.
189+
TemplateName string
190+
TemplateIcon string
191+
TemplateDisplayName string
192+
TemplateAllowUserCancelWorkspaceJobs bool
193+
TemplateActiveVersionID uuid.UUID
194+
195+
LatestBuild WorkspaceBuild
196+
LatestBuildJob ProvisionerJob
197+
198+
// Count is the total number of workspaces applicable to the query.
199+
// This is used for pagination as the total number of returned workspaces
200+
// could be less than this number.
201+
Count int64 `db:"count" json:"count"`
180202
}
181203

182204
// GetAuthorizedWorkspaces returns all workspaces that the user is authorized to access.
183205
// This code is copied from `GetWorkspaces` and adds the authorized filter WHERE
184206
// clause.
185-
func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, prepared rbac.PreparedAuthorized) ([]GetWorkspacesRow, error) {
207+
func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, prepared rbac.PreparedAuthorized) ([]WorkspaceWithData, error) {
186208
authorizedFilter, err := prepared.CompileToSQL(ctx, rbac.ConfigWithoutACL())
187209
if err != nil {
188210
return nil, xerrors.Errorf("compile authorized filter: %w", err)
@@ -204,6 +226,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
204226
arg.OwnerUsername,
205227
arg.TemplateName,
206228
pq.Array(arg.TemplateIds),
229+
pq.Array(arg.WorkspaceIds),
207230
arg.Name,
208231
arg.HasAgent,
209232
arg.AgentInactiveDisconnectTimeoutSeconds,
@@ -214,9 +237,9 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
214237
return nil, xerrors.Errorf("get authorized workspaces: %w", err)
215238
}
216239
defer rows.Close()
217-
var items []GetWorkspacesRow
240+
var items []WorkspaceWithData
218241
for rows.Next() {
219-
var i GetWorkspacesRow
242+
var i WorkspaceWithData
220243
if err := rows.Scan(
221244
&i.ID,
222245
&i.CreatedAt,
@@ -229,6 +252,35 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
229252
&i.AutostartSchedule,
230253
&i.Ttl,
231254
&i.LastUsedAt,
255+
&i.TemplateName,
256+
&i.TemplateIcon,
257+
&i.TemplateDisplayName,
258+
&i.TemplateAllowUserCancelWorkspaceJobs,
259+
&i.TemplateActiveVersionID,
260+
&i.LatestBuild.ID,
261+
&i.LatestBuild.CreatedAt,
262+
&i.LatestBuild.UpdatedAt,
263+
&i.LatestBuild.TemplateVersionID,
264+
&i.LatestBuild.BuildNumber,
265+
&i.LatestBuild.Transition,
266+
&i.LatestBuild.JobID,
267+
&i.LatestBuild.InitiatorID,
268+
&i.LatestBuild.Reason,
269+
&i.LatestBuild.DailyCost,
270+
&i.LatestBuildJob.ID,
271+
&i.LatestBuildJob.CreatedAt,
272+
&i.LatestBuildJob.UpdatedAt,
273+
&i.LatestBuildJob.StartedAt,
274+
&i.LatestBuildJob.CompletedAt,
275+
&i.LatestBuildJob.CanceledAt,
276+
&i.LatestBuildJob.Error,
277+
&i.LatestBuildJob.OrganizationID,
278+
&i.LatestBuildJob.Provisioner,
279+
&i.LatestBuildJob.StorageMethod,
280+
&i.LatestBuildJob.Type,
281+
&i.LatestBuildJob.WorkerID,
282+
&i.LatestBuildJob.FileID,
283+
&i.LatestBuildJob.Tags,
232284
&i.Count,
233285
); err != nil {
234286
return nil, err

0 commit comments

Comments
 (0)