mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
optimize sorting, fix a bug (#15)
This commit is contained in:
parent
e7679a3fa6
commit
867f840265
@ -106,9 +106,8 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) {
|
|||||||
serverList = singleton.SortedServerListForGuest
|
serverList = singleton.SortedServerListForGuest
|
||||||
}
|
}
|
||||||
|
|
||||||
var servers []model.StreamServer
|
servers := make([]model.StreamServer, 0, len(serverList))
|
||||||
for i := 0; i < len(serverList); i++ {
|
for _, server := range serverList {
|
||||||
server := serverList[i]
|
|
||||||
servers = append(servers, model.StreamServer{
|
servers = append(servers, model.StreamServer{
|
||||||
ID: server.ID,
|
ID: server.ID,
|
||||||
Name: server.Name,
|
Name: server.Name,
|
||||||
@ -125,5 +124,6 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) {
|
|||||||
Servers: servers,
|
Servers: servers,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return v.([]byte), err
|
return v.([]byte), err
|
||||||
}
|
}
|
||||||
|
@ -114,3 +114,40 @@ func Itoa[T constraints.Integer](i T) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// From go1.23
|
||||||
|
|
||||||
|
// Compare returns
|
||||||
|
//
|
||||||
|
// -1 if x is less than y,
|
||||||
|
// 0 if x equals y,
|
||||||
|
// +1 if x is greater than y.
|
||||||
|
//
|
||||||
|
// For floating-point types, a NaN is considered less than any non-NaN,
|
||||||
|
// a NaN is considered equal to a NaN, and -0.0 is equal to 0.0.
|
||||||
|
func Compare[T constraints.Ordered](x, y T) int {
|
||||||
|
xNaN := isNaN(x)
|
||||||
|
yNaN := isNaN(y)
|
||||||
|
if xNaN {
|
||||||
|
if yNaN {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if yNaN {
|
||||||
|
return +1
|
||||||
|
}
|
||||||
|
if x < y {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if x > y {
|
||||||
|
return +1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// isNaN reports whether x is a NaN without requiring the math package.
|
||||||
|
// This will always return false if T is not floating-point.
|
||||||
|
func isNaN[T constraints.Ordered](x T) bool {
|
||||||
|
return x != x
|
||||||
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
pb "github.com/naiba/nezha/proto"
|
pb "github.com/naiba/nezha/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -83,12 +84,7 @@ func UpdateCronList() {
|
|||||||
CronList = append(CronList, c)
|
CronList = append(CronList, c)
|
||||||
}
|
}
|
||||||
slices.SortFunc(CronList, func(a, b *model.Cron) int {
|
slices.SortFunc(CronList, func(a, b *model.Cron) int {
|
||||||
if a.ID < b.ID {
|
return utils.Compare(a.ID, b.ID)
|
||||||
return -1
|
|
||||||
} else if a.ID == b.ID {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
ddns2 "github.com/naiba/nezha/pkg/ddns"
|
ddns2 "github.com/naiba/nezha/pkg/ddns"
|
||||||
"github.com/naiba/nezha/pkg/ddns/dummy"
|
"github.com/naiba/nezha/pkg/ddns/dummy"
|
||||||
"github.com/naiba/nezha/pkg/ddns/webhook"
|
"github.com/naiba/nezha/pkg/ddns/webhook"
|
||||||
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -56,12 +57,7 @@ func UpdateDDNSList() {
|
|||||||
DDNSList = append(DDNSList, p)
|
DDNSList = append(DDNSList, p)
|
||||||
}
|
}
|
||||||
slices.SortFunc(DDNSList, func(a, b *model.DDNSProfile) int {
|
slices.SortFunc(DDNSList, func(a, b *model.DDNSProfile) int {
|
||||||
if a.ID < b.ID {
|
return utils.Compare(a.ID, b.ID)
|
||||||
return -1
|
|
||||||
} else if a.ID == b.ID {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -59,12 +60,7 @@ func UpdateNATList() {
|
|||||||
NATList = append(NATList, n)
|
NATList = append(NATList, n)
|
||||||
}
|
}
|
||||||
slices.SortFunc(NATList, func(a, b *model.NAT) int {
|
slices.SortFunc(NATList, func(a, b *model.NAT) int {
|
||||||
if a.ID < b.ID {
|
return utils.Compare(a.ID, b.ID)
|
||||||
return -1
|
|
||||||
} else if a.ID == b.ID {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -85,12 +86,7 @@ func UpdateNotificationList() {
|
|||||||
NotificationListSorted = append(NotificationListSorted, n)
|
NotificationListSorted = append(NotificationListSorted, n)
|
||||||
}
|
}
|
||||||
slices.SortFunc(NotificationListSorted, func(a, b *model.Notification) int {
|
slices.SortFunc(NotificationListSorted, func(a, b *model.Notification) int {
|
||||||
if a.ID < b.ID {
|
return utils.Compare(a.ID, b.ID)
|
||||||
return -1
|
|
||||||
} else if a.ID == b.ID {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,7 @@ func ReSortServer() {
|
|||||||
defer SortedServerLock.Unlock()
|
defer SortedServerLock.Unlock()
|
||||||
|
|
||||||
SortedServerList = make([]*model.Server, 0, len(ServerList))
|
SortedServerList = make([]*model.Server, 0, len(ServerList))
|
||||||
|
SortedServerListForGuest = make([]*model.Server, 0)
|
||||||
for _, s := range ServerList {
|
for _, s := range ServerList {
|
||||||
SortedServerList = append(SortedServerList, s)
|
SortedServerList = append(SortedServerList, s)
|
||||||
if !s.HideForGuest {
|
if !s.HideForGuest {
|
||||||
|
Loading…
Reference in New Issue
Block a user