mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 07:49:41 +02:00
feat(edge): sort waiting room table [EE-6259] (#10577)
This commit is contained in:
parent
32d8dc311b
commit
25741e8c4c
10 changed files with 306 additions and 106 deletions
|
@ -1,46 +1,94 @@
|
|||
package endpoints
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"slices"
|
||||
|
||||
"github.com/fvbommel/sortorder"
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
)
|
||||
|
||||
type EndpointsByName []portainer.Endpoint
|
||||
type comp[T any] func(a, b T) int
|
||||
|
||||
func (e EndpointsByName) Len() int {
|
||||
return len(e)
|
||||
func stringComp(a, b string) int {
|
||||
if sortorder.NaturalLess(a, b) {
|
||||
return -1
|
||||
} else if sortorder.NaturalLess(b, a) {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (e EndpointsByName) Swap(i, j int) {
|
||||
e[i], e[j] = e[j], e[i]
|
||||
}
|
||||
|
||||
func (e EndpointsByName) Less(i, j int) bool {
|
||||
return sortorder.NaturalLess(strings.ToLower(e[i].Name), strings.ToLower(e[j].Name))
|
||||
}
|
||||
|
||||
type EndpointsByGroup struct {
|
||||
endpointGroupNames map[portainer.EndpointGroupID]string
|
||||
endpoints []portainer.Endpoint
|
||||
}
|
||||
|
||||
func (e EndpointsByGroup) Len() int {
|
||||
return len(e.endpoints)
|
||||
}
|
||||
|
||||
func (e EndpointsByGroup) Swap(i, j int) {
|
||||
e.endpoints[i], e.endpoints[j] = e.endpoints[j], e.endpoints[i]
|
||||
}
|
||||
|
||||
func (e EndpointsByGroup) Less(i, j int) bool {
|
||||
if e.endpoints[i].GroupID == e.endpoints[j].GroupID {
|
||||
return false
|
||||
func sortEnvironmentsByField(environments []portainer.Endpoint, environmentGroups []portainer.EndpointGroup, sortField sortKey, isSortDesc bool) {
|
||||
if sortField == "" {
|
||||
return
|
||||
}
|
||||
|
||||
groupA := e.endpointGroupNames[e.endpoints[i].GroupID]
|
||||
groupB := e.endpointGroupNames[e.endpoints[j].GroupID]
|
||||
var less comp[portainer.Endpoint]
|
||||
switch sortField {
|
||||
case sortKeyName:
|
||||
less = func(a, b portainer.Endpoint) int {
|
||||
return stringComp(a.Name, b.Name)
|
||||
}
|
||||
|
||||
case sortKeyGroup:
|
||||
environmentGroupNames := make(map[portainer.EndpointGroupID]string, 0)
|
||||
for _, group := range environmentGroups {
|
||||
environmentGroupNames[group.ID] = group.Name
|
||||
}
|
||||
|
||||
// set the "unassigned" group name to be empty string
|
||||
environmentGroupNames[1] = ""
|
||||
|
||||
less = func(a, b portainer.Endpoint) int {
|
||||
aGroup := environmentGroupNames[a.GroupID]
|
||||
bGroup := environmentGroupNames[b.GroupID]
|
||||
|
||||
return stringComp(aGroup, bGroup)
|
||||
}
|
||||
|
||||
case sortKeyStatus:
|
||||
less = func(a, b portainer.Endpoint) int {
|
||||
return int(a.Status - b.Status)
|
||||
}
|
||||
|
||||
case sortKeyLastCheckInDate:
|
||||
less = func(a, b portainer.Endpoint) int {
|
||||
return int(a.LastCheckInDate - b.LastCheckInDate)
|
||||
}
|
||||
case sortKeyEdgeID:
|
||||
less = func(a, b portainer.Endpoint) int {
|
||||
return stringComp(a.EdgeID, b.EdgeID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
slices.SortStableFunc(environments, func(a, b portainer.Endpoint) int {
|
||||
mul := 1
|
||||
if isSortDesc {
|
||||
mul = -1
|
||||
}
|
||||
|
||||
return less(a, b) * mul
|
||||
})
|
||||
|
||||
return sortorder.NaturalLess(strings.ToLower(groupA), strings.ToLower(groupB))
|
||||
}
|
||||
|
||||
type sortKey string
|
||||
|
||||
const (
|
||||
sortKeyName sortKey = "Name"
|
||||
sortKeyGroup sortKey = "Group"
|
||||
sortKeyStatus sortKey = "Status"
|
||||
sortKeyLastCheckInDate sortKey = "LastCheckIn"
|
||||
sortKeyEdgeID sortKey = "EdgeID"
|
||||
)
|
||||
|
||||
func getSortKey(sortField string) sortKey {
|
||||
fieldAsSortKey := sortKey(sortField)
|
||||
if slices.Contains([]sortKey{sortKeyName, sortKeyGroup, sortKeyStatus, sortKeyLastCheckInDate, sortKeyEdgeID}, fieldAsSortKey) {
|
||||
return fieldAsSortKey
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue