mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
chore: simplify some steps (#912)
This commit is contained in:
parent
6cf0e7f1cf
commit
7d15838548
@ -79,11 +79,7 @@ func SearchByIDCtx[S ~[]E, E CommonInterface](c *gin.Context, x S) S {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if i, ok := slices.BinarySearchFunc(x, id, func(e E, t uint64) int {
|
s = appendBinarySearch(s, x, id)
|
||||||
return cmp.Compare(e.GetID(), t)
|
|
||||||
}); ok {
|
|
||||||
s = append(s, x[i])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return utils.IfOr(len(s) > 0, s, x)
|
return utils.IfOr(len(s) > 0, s, x)
|
||||||
}
|
}
|
||||||
@ -99,25 +95,19 @@ func searchByIDCtxServer(c *gin.Context, x []*Server) []*Server {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if i, ok := slices.BinarySearchFunc(list1, id, func(e *Server, t uint64) int {
|
clist1 = appendBinarySearch(clist1, list1, id)
|
||||||
return cmp.Compare(e.ID, t)
|
clist2 = appendBinarySearch(clist2, list2, id)
|
||||||
}); ok {
|
|
||||||
clist1 = append(clist1, list1[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
if i, ok := slices.BinarySearchFunc(list2, id, func(e *Server, t uint64) int {
|
|
||||||
return cmp.Compare(e.ID, t)
|
|
||||||
}); ok {
|
|
||||||
clist2 = append(clist2, list2[i])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
l := slices.Concat(clist1, clist2)
|
l := slices.Concat(clist1, clist2)
|
||||||
return utils.IfOr(len(l) > 0, l, x)
|
return utils.IfOr(len(l) > 0, l, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
func appendBinarySearch[S ~[]E, E CommonInterface](x, y S, target uint64) S {
|
||||||
Code int `json:"code,omitempty"`
|
if i, ok := slices.BinarySearchFunc(y, target, func(e E, t uint64) int {
|
||||||
Message string `json:"message,omitempty"`
|
return cmp.Compare(e.GetID(), t)
|
||||||
Result interface{} `json:"result,omitempty"`
|
}); ok {
|
||||||
|
x = append(x, y[i])
|
||||||
|
}
|
||||||
|
return x
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/nezhahq/nezha/model"
|
"github.com/nezhahq/nezha/model"
|
||||||
|
"github.com/nezhahq/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -45,10 +46,7 @@ func BlockByIPs(ipList []string) error {
|
|||||||
func GetOnlineUsers(limit, offset int) []*model.OnlineUser {
|
func GetOnlineUsers(limit, offset int) []*model.OnlineUser {
|
||||||
OnlineUserMapLock.Lock()
|
OnlineUserMapLock.Lock()
|
||||||
defer OnlineUserMapLock.Unlock()
|
defer OnlineUserMapLock.Unlock()
|
||||||
var users []*model.OnlineUser
|
users := utils.MapValuesToSlice(OnlineUserMap)
|
||||||
for _, user := range OnlineUserMap {
|
|
||||||
users = append(users, user)
|
|
||||||
}
|
|
||||||
slices.SortFunc(users, func(i, j *model.OnlineUser) int {
|
slices.SortFunc(users, func(i, j *model.OnlineUser) int {
|
||||||
return i.ConnectedAt.Compare(j.ConnectedAt)
|
return i.ConnectedAt.Compare(j.ConnectedAt)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user