mirror of https://github.com/coder/coder.git
feat: Sort templates by workspaces count (#3734)
This commit is contained in:
parent
190310464d
commit
0708e37a38
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue