@@ -42,23 +42,6 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
42
42
return
43
43
}
44
44
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
-
62
45
createdByNameMap , err := getCreatedByNamesByTemplateIDs (ctx , api .Database , []database.Template {template })
63
46
if err != nil {
64
47
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
@@ -68,7 +51,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
68
51
return
69
52
}
70
53
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 ()]))
72
55
}
73
56
74
57
// @Summary Delete template by ID
@@ -313,7 +296,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
313
296
return xerrors .Errorf ("get creator name: %w" , err )
314
297
}
315
298
316
- template = api .convertTemplate (dbTemplate , 0 , createdByNameMap [dbTemplate .ID .String ()])
299
+ template = api .convertTemplate (dbTemplate , createdByNameMap [dbTemplate .ID .String ()])
317
300
return nil
318
301
}, nil )
319
302
if err != nil {
@@ -369,23 +352,6 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
369
352
return
370
353
}
371
354
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
-
389
355
createdByNameMap , err := getCreatedByNamesByTemplateIDs (ctx , api .Database , templates )
390
356
if err != nil {
391
357
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
@@ -395,7 +361,7 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
395
361
return
396
362
}
397
363
398
- httpapi .Write (ctx , rw , http .StatusOK , api .convertTemplates (templates , workspaceCounts , createdByNameMap ))
364
+ httpapi .Write (ctx , rw , http .StatusOK , api .convertTemplates (templates , createdByNameMap ))
399
365
}
400
366
401
367
// @Summary Get templates by organization and template name
@@ -433,23 +399,6 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
433
399
return
434
400
}
435
401
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
-
453
402
createdByNameMap , err := getCreatedByNamesByTemplateIDs (ctx , api .Database , []database.Template {template })
454
403
if err != nil {
455
404
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
@@ -459,7 +408,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
459
408
return
460
409
}
461
410
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 ()]))
463
412
}
464
413
465
414
// @Summary Update template metadata by ID
@@ -508,22 +457,8 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
508
457
return
509
458
}
510
459
511
- count := uint32 (0 )
512
460
var updated database.Template
513
461
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
-
527
462
if req .Name == template .Name &&
528
463
req .Description == template .Description &&
529
464
req .DisplayName == template .DisplayName &&
@@ -550,6 +485,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
550
485
desc = template .Description
551
486
}
552
487
488
+ var err error
553
489
updated , err = tx .UpdateTemplateMetaByID (ctx , database.UpdateTemplateMetaByIDParams {
554
490
ID : template .ID ,
555
491
UpdatedAt : database .Now (),
@@ -587,7 +523,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
587
523
return
588
524
}
589
525
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 ()]))
591
527
}
592
528
593
529
// @Summary Get template DAUs by ID
@@ -659,22 +595,11 @@ func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, temp
659
595
return creators , nil
660
596
}
661
597
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 {
663
599
apiTemplates := make ([]codersdk.Template , 0 , len (templates ))
664
600
665
601
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 ()]))
678
603
}
679
604
680
605
// Sort templates by ActiveUserCount DESC
@@ -686,7 +611,7 @@ func (api *API) convertTemplates(templates []database.Template, workspaceCounts
686
611
}
687
612
688
613
func (api * API ) convertTemplate (
689
- template database.Template , workspaceOwnerCount uint32 , createdByName string ,
614
+ template database.Template , createdByName string ,
690
615
) codersdk.Template {
691
616
activeCount , _ := api .metricsCache .TemplateUniqueUsers (template .ID )
692
617
@@ -701,7 +626,6 @@ func (api *API) convertTemplate(
701
626
DisplayName : template .DisplayName ,
702
627
Provisioner : codersdk .ProvisionerType (template .Provisioner ),
703
628
ActiveVersionID : template .ActiveVersionID ,
704
- WorkspaceOwnerCount : workspaceOwnerCount ,
705
629
ActiveUserCount : activeCount ,
706
630
BuildTimeStats : buildTimeStats ,
707
631
Description : template .Description ,
0 commit comments