feat: Sort templates by workspaces count (#3734)

This commit is contained in:
Bruno Quaresma 2022-08-30 14:27:33 -03:00 committed by GitHub
parent 190310464d
commit 0708e37a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net/http"
"sort"
"time"
"github.com/go-chi/chi/v5"
@ -673,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 {
@ -687,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
}