From adb4be58447c50f7a40bb14465cdbdf78b265182 Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Mon, 29 Aug 2022 17:56:36 +0000 Subject: [PATCH 1/3] feat: Sort templates by workspaces count --- coderd/httpmw/workspaceparam.go | 9 +++++---- coderd/templates.go | 9 ++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/coderd/httpmw/workspaceparam.go b/coderd/httpmw/workspaceparam.go index 1ce23a20d9f40..7c38c41f3227c 100644 --- a/coderd/httpmw/workspaceparam.go +++ b/coderd/httpmw/workspaceparam.go @@ -8,11 +8,12 @@ import ( "net/http" "strings" + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + "github.com/coder/coder/coderd/database" "github.com/coder/coder/coderd/httpapi" "github.com/coder/coder/codersdk" - "github.com/go-chi/chi/v5" - "github.com/google/uuid" ) type workspaceParamContextKey struct{} @@ -57,8 +58,8 @@ func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler { // "workspace_and_agent" URL parameter. `ExtractUserParam` must be called // before this. // This can be in the form of: -// - ".[workspace-agent]" : If multiple agents exist -// - "" : If one agent exists +// - ".[workspace-agent]" : If multiple agents exist +// - "" : If one agent exists func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { diff --git a/coderd/templates.go b/coderd/templates.go index eee18e0be1c14..6d132c257b9f5 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "net/http" + "sort" "time" "github.com/go-chi/chi/v5" @@ -338,7 +339,13 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request) return } - httpapi.Write(rw, http.StatusOK, convertTemplates(templates, workspaceCounts, createdByNameMap)) + // Sort templates by WorkspaceOwnerCount DESC + templateList := convertTemplates(templates, workspaceCounts, createdByNameMap) + sort.SliceStable(templateList, func(i, j int) bool { + return templateList[i].WorkspaceOwnerCount > templateList[j].WorkspaceOwnerCount + }) + + httpapi.Write(rw, http.StatusOK, templateList) } func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Request) { From cbf778cc2cec19cce4b3866878bedbfe286b2fdd Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Mon, 29 Aug 2022 17:59:03 +0000 Subject: [PATCH 2/3] Undo changes --- coderd/httpmw/workspaceparam.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/coderd/httpmw/workspaceparam.go b/coderd/httpmw/workspaceparam.go index 7c38c41f3227c..1ce23a20d9f40 100644 --- a/coderd/httpmw/workspaceparam.go +++ b/coderd/httpmw/workspaceparam.go @@ -8,12 +8,11 @@ import ( "net/http" "strings" - "github.com/go-chi/chi/v5" - "github.com/google/uuid" - "github.com/coder/coder/coderd/database" "github.com/coder/coder/coderd/httpapi" "github.com/coder/coder/codersdk" + "github.com/go-chi/chi/v5" + "github.com/google/uuid" ) type workspaceParamContextKey struct{} @@ -58,8 +57,8 @@ func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler { // "workspace_and_agent" URL parameter. `ExtractUserParam` must be called // before this. // This can be in the form of: -// - ".[workspace-agent]" : If multiple agents exist -// - "" : If one agent exists +// - ".[workspace-agent]" : If multiple agents exist +// - "" : If one agent exists func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { From 32b4f71c8a828bff1c5e562a97f3d635e90e1497 Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Tue, 30 Aug 2022 17:17:04 +0000 Subject: [PATCH 3/3] Move sort logic to convertTemplate --- coderd/templates.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/coderd/templates.go b/coderd/templates.go index 6d132c257b9f5..e43fdb61e007a 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -339,13 +339,7 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request) return } - // Sort templates by WorkspaceOwnerCount DESC - templateList := convertTemplates(templates, workspaceCounts, createdByNameMap) - sort.SliceStable(templateList, func(i, j int) bool { - return templateList[i].WorkspaceOwnerCount > templateList[j].WorkspaceOwnerCount - }) - - httpapi.Write(rw, http.StatusOK, templateList) + httpapi.Write(rw, http.StatusOK, convertTemplates(templates, workspaceCounts, createdByNameMap)) } func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Request) { @@ -680,6 +674,7 @@ func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, temp func convertTemplates(templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow, createdByNameMap map[string]string) []codersdk.Template { apiTemplates := make([]codersdk.Template, 0, len(templates)) + for _, template := range templates { found := false for _, workspaceCount := range workspaceCounts { @@ -694,6 +689,12 @@ func convertTemplates(templates []database.Template, workspaceCounts []database. apiTemplates = append(apiTemplates, convertTemplate(template, uint32(0), createdByNameMap[template.ID.String()])) } } + + // Sort templates by WorkspaceOwnerCount DESC + sort.SliceStable(apiTemplates, func(i, j int) bool { + return apiTemplates[i].WorkspaceOwnerCount > apiTemplates[j].WorkspaceOwnerCount + }) + return apiTemplates }