Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb committed Feb 10, 2025
1 parent 9229c85 commit 37b492d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/dashboard/controller/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func createNAT(c *gin.Context) (uint64, error) {
return 0, err
}

if server, ok := singleton.ServerShared.GetList()[nf.ServerID]; ok {
if server, ok := singleton.ServerShared.GetServer(nf.ServerID); ok {
if !server.HasPermission(c) {
return 0, singleton.Localizer.ErrorT("permission denied")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/dashboard/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ func getServerConfig(c *gin.Context) (string, error) {
return "", err
}

slist := singleton.ServerShared.GetList()
s, ok := slist[id]
s, ok := singleton.ServerShared.GetServer(id)
if !ok || s.TaskStream == nil {
return "", nil
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/dashboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ func main() {
rpc.DispatchKeepalive()
go rpc.DispatchTask(serviceSentinelDispatchBus)
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
singleton.ServiceSentinelShared, err = singleton.NewServiceSentinel(serviceSentinelDispatchBus)
if err != nil {
log.Fatal(err)
}

grpcHandler := rpc.ServeRPC()
httpHandler := controller.ServeWeb(frontendDist)
Expand Down
38 changes: 19 additions & 19 deletions service/singleton/servicesentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const (
_CurrentStatusSize = 30 // 统计 15 分钟内的数据为当前状态
)

var ServiceSentinelShared *ServiceSentinel

type serviceResponseItem struct {
model.ServiceResponseItem

Expand All @@ -42,8 +40,8 @@ type _TodayStatsOfService struct {
}

// NewServiceSentinel 创建服务监控器
func NewServiceSentinel(serviceSentinelDispatchBus chan<- model.Service) {
ServiceSentinelShared = &ServiceSentinel{
func NewServiceSentinel(serviceSentinelDispatchBus chan<- model.Service) (*ServiceSentinel, error) {
ss := &ServiceSentinel{
serviceReportChannel: make(chan ReportData, 200),
serviceStatusToday: make(map[uint64]*_TodayStatsOfService),
serviceCurrentStatusIndex: make(map[uint64]*indexStore),
Expand All @@ -60,7 +58,7 @@ func NewServiceSentinel(serviceSentinelDispatchBus chan<- model.Service) {
dispatchBus: serviceSentinelDispatchBus,
}
// 加载历史记录
ServiceSentinelShared.loadServiceHistory()
ss.loadServiceHistory()

year, month, day := time.Now().Date()
today := time.Date(year, month, day, 0, 0, 0, 0, Loc)
Expand All @@ -73,23 +71,25 @@ func NewServiceSentinel(serviceSentinelDispatchBus chan<- model.Service) {
for i := 0; i < len(mhs); i++ {
totalDelay[mhs[i].ServiceID] += mhs[i].AvgDelay
totalDelayCount[mhs[i].ServiceID]++
ServiceSentinelShared.serviceStatusToday[mhs[i].ServiceID].Up += int(mhs[i].Up)
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].TotalUp += mhs[i].Up
ServiceSentinelShared.serviceStatusToday[mhs[i].ServiceID].Down += int(mhs[i].Down)
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].TotalDown += mhs[i].Down
ss.serviceStatusToday[mhs[i].ServiceID].Up += int(mhs[i].Up)
ss.monthlyStatus[mhs[i].ServiceID].TotalUp += mhs[i].Up
ss.serviceStatusToday[mhs[i].ServiceID].Down += int(mhs[i].Down)
ss.monthlyStatus[mhs[i].ServiceID].TotalDown += mhs[i].Down
}
for id, delay := range totalDelay {
ServiceSentinelShared.serviceStatusToday[id].Delay = delay / float32(totalDelayCount[id])
ss.serviceStatusToday[id].Delay = delay / float32(totalDelayCount[id])
}

// 启动服务监控器
go ServiceSentinelShared.worker()
go ss.worker()

// 每日将游标往后推一天
_, err := Cron.AddFunc("0 0 0 * * *", ServiceSentinelShared.refreshMonthlyServiceStatus)
_, err := Cron.AddFunc("0 0 0 * * *", ss.refreshMonthlyServiceStatus)
if err != nil {
panic(err)
return nil, err
}

return ss, nil
}

/*
Expand Down Expand Up @@ -210,7 +210,7 @@ func (ss *ServiceSentinel) loadServiceHistory() {
today := time.Date(year, month, day, 0, 0, 0, 0, Loc)

for i := 0; i < len(services); i++ {
ServiceSentinelShared.monthlyStatus[services[i].ID] = &serviceResponseItem{
ss.monthlyStatus[services[i].ID] = &serviceResponseItem{
service: services[i],
ServiceResponseItem: model.ServiceResponseItem{
Delay: &[30]float32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Expand All @@ -229,12 +229,12 @@ func (ss *ServiceSentinel) loadServiceHistory() {
if dayIndex < 0 {
continue
}
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].Delay[dayIndex] = (ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].Delay[dayIndex]*float32(delayCount[dayIndex]) + mhs[i].AvgDelay) / float32(delayCount[dayIndex]+1)
ss.monthlyStatus[mhs[i].ServiceID].Delay[dayIndex] = (ss.monthlyStatus[mhs[i].ServiceID].Delay[dayIndex]*float32(delayCount[dayIndex]) + mhs[i].AvgDelay) / float32(delayCount[dayIndex]+1)
delayCount[dayIndex]++
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].Up[dayIndex] += int(mhs[i].Up)
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].TotalUp += mhs[i].Up
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].Down[dayIndex] += int(mhs[i].Down)
ServiceSentinelShared.monthlyStatus[mhs[i].ServiceID].TotalDown += mhs[i].Down
ss.monthlyStatus[mhs[i].ServiceID].Up[dayIndex] += int(mhs[i].Up)
ss.monthlyStatus[mhs[i].ServiceID].TotalUp += mhs[i].Up
ss.monthlyStatus[mhs[i].ServiceID].Down[dayIndex] += int(mhs[i].Down)
ss.monthlyStatus[mhs[i].ServiceID].TotalDown += mhs[i].Down
}
}

Expand Down
3 changes: 2 additions & 1 deletion service/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var (
FrontendTemplates []model.FrontendTemplate
DashboardBootTime = uint64(time.Now().Unix())

ServerShared *ServerClass
ServerShared *ServerClass
ServiceSentinelShared *ServiceSentinel
)

//go:embed frontend-templates.yaml
Expand Down

0 comments on commit 37b492d

Please sign in to comment.