2022-04-12 01:16:33 -04:00
|
|
|
|
package singleton
|
|
|
|
|
|
|
|
|
|
import (
|
2024-12-21 11:05:41 -05:00
|
|
|
|
"cmp"
|
|
|
|
|
"slices"
|
2022-04-14 04:41:34 -04:00
|
|
|
|
|
2024-11-28 06:38:54 -05:00
|
|
|
|
"github.com/nezhahq/nezha/model"
|
2024-12-21 11:05:41 -05:00
|
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
2022-04-12 01:16:33 -04:00
|
|
|
|
)
|
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
type ServerClass struct {
|
|
|
|
|
class[uint64, *model.Server]
|
2022-04-12 01:16:33 -04:00
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
uuidToID map[string]uint64
|
2022-04-12 01:16:33 -04:00
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
sortedListForGuest []*model.Server
|
2022-04-12 01:16:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
func NewServerClass() *ServerClass {
|
|
|
|
|
sc := &ServerClass{
|
|
|
|
|
class: class[uint64, *model.Server]{
|
|
|
|
|
list: make(map[uint64]*model.Server),
|
|
|
|
|
},
|
|
|
|
|
uuidToID: make(map[string]uint64),
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 01:16:33 -04:00
|
|
|
|
var servers []model.Server
|
|
|
|
|
DB.Find(&servers)
|
|
|
|
|
for _, s := range servers {
|
|
|
|
|
innerS := s
|
2025-01-31 00:33:53 -05:00
|
|
|
|
model.InitServer(&innerS)
|
2025-02-21 10:08:12 -05:00
|
|
|
|
sc.list[innerS.ID] = &innerS
|
|
|
|
|
sc.uuidToID[innerS.UUID] = innerS.ID
|
2022-04-12 01:16:33 -04:00
|
|
|
|
}
|
2025-02-21 10:08:12 -05:00
|
|
|
|
sc.sortList()
|
|
|
|
|
|
|
|
|
|
return sc
|
2022-04-12 01:16:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
func (c *ServerClass) Update(s *model.Server, uuid string) {
|
|
|
|
|
c.listMu.Lock()
|
|
|
|
|
|
|
|
|
|
c.list[s.ID] = s
|
|
|
|
|
if uuid != "" {
|
|
|
|
|
c.uuidToID[uuid] = s.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.listMu.Unlock()
|
|
|
|
|
|
|
|
|
|
c.sortList()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *ServerClass) Delete(idList []uint64) {
|
|
|
|
|
c.listMu.Lock()
|
|
|
|
|
|
|
|
|
|
for _, id := range idList {
|
|
|
|
|
serverUUID := c.list[id].UUID
|
|
|
|
|
delete(c.uuidToID, serverUUID)
|
|
|
|
|
delete(c.list, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.listMu.Unlock()
|
2022-04-12 01:16:33 -04:00
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
c.sortList()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *ServerClass) GetSortedListForGuest() []*model.Server {
|
|
|
|
|
c.sortedListMu.RLock()
|
|
|
|
|
defer c.sortedListMu.RUnlock()
|
|
|
|
|
|
|
|
|
|
return slices.Clone(c.sortedListForGuest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *ServerClass) UUIDToID(uuid string) (id uint64, ok bool) {
|
|
|
|
|
c.listMu.RLock()
|
|
|
|
|
defer c.listMu.RUnlock()
|
|
|
|
|
|
|
|
|
|
id, ok = c.uuidToID[uuid]
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *ServerClass) sortList() {
|
|
|
|
|
c.listMu.RLock()
|
|
|
|
|
defer c.listMu.RUnlock()
|
|
|
|
|
c.sortedListMu.Lock()
|
|
|
|
|
defer c.sortedListMu.Unlock()
|
|
|
|
|
|
|
|
|
|
c.sortedList = utils.MapValuesToSlice(c.list)
|
2022-04-12 01:16:33 -04:00
|
|
|
|
// 按照服务器 ID 排序的具体实现(ID越大越靠前)
|
2025-02-21 10:08:12 -05:00
|
|
|
|
slices.SortStableFunc(c.sortedList, func(a, b *model.Server) int {
|
2024-12-21 11:05:41 -05:00
|
|
|
|
if a.DisplayIndex == b.DisplayIndex {
|
|
|
|
|
return cmp.Compare(a.ID, b.ID)
|
2022-04-12 01:16:33 -04:00
|
|
|
|
}
|
2024-12-21 11:05:41 -05:00
|
|
|
|
return cmp.Compare(b.DisplayIndex, a.DisplayIndex)
|
2022-04-12 01:16:33 -04:00
|
|
|
|
})
|
2022-09-30 10:40:56 -04:00
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
|
c.sortedListForGuest = make([]*model.Server, 0, len(c.sortedList))
|
|
|
|
|
for _, s := range c.sortedList {
|
2024-12-21 11:05:41 -05:00
|
|
|
|
if !s.HideForGuest {
|
2025-02-21 10:08:12 -05:00
|
|
|
|
c.sortedListForGuest = append(c.sortedListForGuest, s)
|
2022-09-30 10:40:56 -04:00
|
|
|
|
}
|
2024-12-21 11:05:41 -05:00
|
|
|
|
}
|
2022-04-12 01:16:33 -04:00
|
|
|
|
}
|