Skip to content

Commit b1e948a

Browse files
kylecarbspull[bot]
authored andcommitted
chore: remove unused workspace_owner_count field (#5958)
This added unnecessary database load, because it's not used!
1 parent 3096380 commit b1e948a

File tree

13 files changed

+23
-242
lines changed

13 files changed

+23
-242
lines changed

coderd/apidoc/docs.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/databasefake/databasefake.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,41 +1272,6 @@ func (q *fakeQuerier) GetWorkspaceAppsByAgentIDs(_ context.Context, ids []uuid.U
12721272
return apps, nil
12731273
}
12741274

1275-
func (q *fakeQuerier) GetWorkspaceOwnerCountsByTemplateIDs(_ context.Context, templateIDs []uuid.UUID) ([]database.GetWorkspaceOwnerCountsByTemplateIDsRow, error) {
1276-
q.mutex.RLock()
1277-
defer q.mutex.RUnlock()
1278-
1279-
counts := map[uuid.UUID]map[uuid.UUID]struct{}{}
1280-
for _, templateID := range templateIDs {
1281-
counts[templateID] = map[uuid.UUID]struct{}{}
1282-
for _, workspace := range q.workspaces {
1283-
if workspace.TemplateID != templateID {
1284-
continue
1285-
}
1286-
if workspace.Deleted {
1287-
continue
1288-
}
1289-
countByOwnerID, ok := counts[templateID]
1290-
if !ok {
1291-
countByOwnerID = map[uuid.UUID]struct{}{}
1292-
}
1293-
countByOwnerID[workspace.OwnerID] = struct{}{}
1294-
counts[templateID] = countByOwnerID
1295-
}
1296-
}
1297-
res := make([]database.GetWorkspaceOwnerCountsByTemplateIDsRow, 0)
1298-
for key, value := range counts {
1299-
res = append(res, database.GetWorkspaceOwnerCountsByTemplateIDsRow{
1300-
TemplateID: key,
1301-
Count: int64(len(value)),
1302-
})
1303-
}
1304-
if len(res) == 0 {
1305-
return nil, sql.ErrNoRows
1306-
}
1307-
return res, nil
1308-
}
1309-
13101275
func (q *fakeQuerier) GetWorkspaceBuildByID(_ context.Context, id uuid.UUID) (database.WorkspaceBuild, error) {
13111276
q.mutex.RLock()
13121277
defer q.mutex.RUnlock()

coderd/database/querier.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 0 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,6 @@ WHERE
223223
AND LOWER("name") = LOWER(@name)
224224
ORDER BY created_at DESC;
225225

226-
-- name: GetWorkspaceOwnerCountsByTemplateIDs :many
227-
SELECT
228-
template_id,
229-
COUNT(DISTINCT owner_id)
230-
FROM
231-
workspaces
232-
WHERE
233-
template_id = ANY(@ids :: uuid [ ])
234-
-- Ignore deleted workspaces
235-
AND deleted != true
236-
GROUP BY
237-
template_id;
238-
239226
-- name: InsertWorkspace :one
240227
INSERT INTO
241228
workspaces (

coderd/templates.go

Lines changed: 9 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
4242
return
4343
}
4444

45-
workspaceCounts, err := api.Database.GetWorkspaceOwnerCountsByTemplateIDs(ctx, []uuid.UUID{template.ID})
46-
if errors.Is(err, sql.ErrNoRows) {
47-
err = nil
48-
}
49-
if err != nil {
50-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
51-
Message: "Internal error fetching workspace count.",
52-
Detail: err.Error(),
53-
})
54-
return
55-
}
56-
57-
count := uint32(0)
58-
if len(workspaceCounts) > 0 {
59-
count = uint32(workspaceCounts[0].Count)
60-
}
61-
6245
createdByNameMap, err := getCreatedByNamesByTemplateIDs(ctx, api.Database, []database.Template{template})
6346
if err != nil {
6447
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
@@ -68,7 +51,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
6851
return
6952
}
7053

71-
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(template, count, createdByNameMap[template.ID.String()]))
54+
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(template, createdByNameMap[template.ID.String()]))
7255
}
7356

7457
// @Summary Delete template by ID
@@ -313,7 +296,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
313296
return xerrors.Errorf("get creator name: %w", err)
314297
}
315298

316-
template = api.convertTemplate(dbTemplate, 0, createdByNameMap[dbTemplate.ID.String()])
299+
template = api.convertTemplate(dbTemplate, createdByNameMap[dbTemplate.ID.String()])
317300
return nil
318301
}, nil)
319302
if err != nil {
@@ -369,23 +352,6 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
369352
return
370353
}
371354

372-
templateIDs := make([]uuid.UUID, 0, len(templates))
373-
374-
for _, template := range templates {
375-
templateIDs = append(templateIDs, template.ID)
376-
}
377-
workspaceCounts, err := api.Database.GetWorkspaceOwnerCountsByTemplateIDs(ctx, templateIDs)
378-
if errors.Is(err, sql.ErrNoRows) {
379-
err = nil
380-
}
381-
if err != nil {
382-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
383-
Message: "Internal error fetching workspace counts.",
384-
Detail: err.Error(),
385-
})
386-
return
387-
}
388-
389355
createdByNameMap, err := getCreatedByNamesByTemplateIDs(ctx, api.Database, templates)
390356
if err != nil {
391357
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
@@ -395,7 +361,7 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
395361
return
396362
}
397363

398-
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplates(templates, workspaceCounts, createdByNameMap))
364+
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplates(templates, createdByNameMap))
399365
}
400366

401367
// @Summary Get templates by organization and template name
@@ -433,23 +399,6 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
433399
return
434400
}
435401

436-
workspaceCounts, err := api.Database.GetWorkspaceOwnerCountsByTemplateIDs(ctx, []uuid.UUID{template.ID})
437-
if errors.Is(err, sql.ErrNoRows) {
438-
err = nil
439-
}
440-
if err != nil {
441-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
442-
Message: "Internal error fetching workspace counts.",
443-
Detail: err.Error(),
444-
})
445-
return
446-
}
447-
448-
count := uint32(0)
449-
if len(workspaceCounts) > 0 {
450-
count = uint32(workspaceCounts[0].Count)
451-
}
452-
453402
createdByNameMap, err := getCreatedByNamesByTemplateIDs(ctx, api.Database, []database.Template{template})
454403
if err != nil {
455404
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
@@ -459,7 +408,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
459408
return
460409
}
461410

462-
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(template, count, createdByNameMap[template.ID.String()]))
411+
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(template, createdByNameMap[template.ID.String()]))
463412
}
464413

465414
// @Summary Update template metadata by ID
@@ -508,22 +457,8 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
508457
return
509458
}
510459

511-
count := uint32(0)
512460
var updated database.Template
513461
err := api.Database.InTx(func(tx database.Store) error {
514-
// Fetch workspace counts
515-
workspaceCounts, err := tx.GetWorkspaceOwnerCountsByTemplateIDs(ctx, []uuid.UUID{template.ID})
516-
if xerrors.Is(err, sql.ErrNoRows) {
517-
err = nil
518-
}
519-
if err != nil {
520-
return err
521-
}
522-
523-
if len(workspaceCounts) > 0 {
524-
count = uint32(workspaceCounts[0].Count)
525-
}
526-
527462
if req.Name == template.Name &&
528463
req.Description == template.Description &&
529464
req.DisplayName == template.DisplayName &&
@@ -550,6 +485,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
550485
desc = template.Description
551486
}
552487

488+
var err error
553489
updated, err = tx.UpdateTemplateMetaByID(ctx, database.UpdateTemplateMetaByIDParams{
554490
ID: template.ID,
555491
UpdatedAt: database.Now(),
@@ -587,7 +523,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
587523
return
588524
}
589525

590-
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(updated, count, createdByNameMap[updated.ID.String()]))
526+
httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplate(updated, createdByNameMap[updated.ID.String()]))
591527
}
592528

593529
// @Summary Get template DAUs by ID
@@ -659,22 +595,11 @@ func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, temp
659595
return creators, nil
660596
}
661597

662-
func (api *API) convertTemplates(templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow, createdByNameMap map[string]string) []codersdk.Template {
598+
func (api *API) convertTemplates(templates []database.Template, createdByNameMap map[string]string) []codersdk.Template {
663599
apiTemplates := make([]codersdk.Template, 0, len(templates))
664600

665601
for _, template := range templates {
666-
found := false
667-
for _, workspaceCount := range workspaceCounts {
668-
if workspaceCount.TemplateID.String() != template.ID.String() {
669-
continue
670-
}
671-
apiTemplates = append(apiTemplates, api.convertTemplate(template, uint32(workspaceCount.Count), createdByNameMap[template.ID.String()]))
672-
found = true
673-
break
674-
}
675-
if !found {
676-
apiTemplates = append(apiTemplates, api.convertTemplate(template, uint32(0), createdByNameMap[template.ID.String()]))
677-
}
602+
apiTemplates = append(apiTemplates, api.convertTemplate(template, createdByNameMap[template.ID.String()]))
678603
}
679604

680605
// Sort templates by ActiveUserCount DESC
@@ -686,7 +611,7 @@ func (api *API) convertTemplates(templates []database.Template, workspaceCounts
686611
}
687612

688613
func (api *API) convertTemplate(
689-
template database.Template, workspaceOwnerCount uint32, createdByName string,
614+
template database.Template, createdByName string,
690615
) codersdk.Template {
691616
activeCount, _ := api.metricsCache.TemplateUniqueUsers(template.ID)
692617

@@ -701,7 +626,6 @@ func (api *API) convertTemplate(
701626
DisplayName: template.DisplayName,
702627
Provisioner: codersdk.ProvisionerType(template.Provisioner),
703628
ActiveVersionID: template.ActiveVersionID,
704-
WorkspaceOwnerCount: workspaceOwnerCount,
705629
ActiveUserCount: activeCount,
706630
BuildTimeStats: buildTimeStats,
707631
Description: template.Description,

coderd/templates_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/coder/coder/coderd/audit"
1616
"github.com/coder/coder/coderd/coderdtest"
1717
"github.com/coder/coder/coderd/database"
18-
"github.com/coder/coder/coderd/rbac"
1918
"github.com/coder/coder/coderd/util/ptr"
2019
"github.com/coder/coder/codersdk"
2120
"github.com/coder/coder/codersdk/agentsdk"
@@ -40,40 +39,6 @@ func TestTemplate(t *testing.T) {
4039
_, err := client.Template(ctx, template.ID)
4140
require.NoError(t, err)
4241
})
43-
44-
t.Run("WorkspaceCount", func(t *testing.T) {
45-
t.Parallel()
46-
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
47-
user := coderdtest.CreateFirstUser(t, client)
48-
member := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleOwner())
49-
memberWithDeleted := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleOwner())
50-
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
51-
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
52-
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
53-
54-
// Create 3 workspaces with 3 users. 2 workspaces exist, 1 is deleted
55-
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
56-
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
57-
58-
memberWorkspace := coderdtest.CreateWorkspace(t, member, user.OrganizationID, template.ID)
59-
coderdtest.AwaitWorkspaceBuildJob(t, member, memberWorkspace.LatestBuild.ID)
60-
61-
deletedWorkspace := coderdtest.CreateWorkspace(t, memberWithDeleted, user.OrganizationID, template.ID)
62-
coderdtest.AwaitWorkspaceBuildJob(t, client, deletedWorkspace.LatestBuild.ID)
63-
64-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
65-
defer cancel()
66-
67-
build, err := client.CreateWorkspaceBuild(ctx, deletedWorkspace.ID, codersdk.CreateWorkspaceBuildRequest{
68-
Transition: codersdk.WorkspaceTransitionDelete,
69-
})
70-
require.NoError(t, err)
71-
coderdtest.AwaitWorkspaceBuildJob(t, client, build.ID)
72-
73-
template, err = client.Template(ctx, template.ID)
74-
require.NoError(t, err)
75-
require.Equal(t, 2, int(template.WorkspaceOwnerCount), "workspace count")
76-
})
7742
}
7843

7944
func TestPostTemplateByOrganization(t *testing.T) {

0 commit comments

Comments
 (0)