@@ -632,95 +632,95 @@ func buildTrafficData() []map[string]interface{} {
632632 var trafficData []map [string ]interface {}
633633
634634 for cycleID , stats := range statsStore {
635- // 查找对应的Alert规则,用于获取周期设置
636- var alert * model.AlertRule
637- for _ , a := range singleton .Alerts {
638- if a .ID == cycleID {
639- alert = a
640- break
641- }
635+ // 查找对应的Alert规则,用于获取周期设置
636+ var alert * model.AlertRule
637+ for _ , a := range singleton .Alerts {
638+ if a .ID == cycleID {
639+ alert = a
640+ break
642641 }
642+ }
643643
644- // 如果找不到Alert规则,跳过此项
645- if alert == nil {
646- continue
647- }
644+ // 如果找不到Alert规则,跳过此项
645+ if alert == nil {
646+ continue
647+ }
648648
649- // 找到与流量相关的Rule
650- var flowRule * model.Rule
651- for i := range alert .Rules {
652- if alert .Rules [i ].IsTransferDurationRule () {
653- flowRule = & alert .Rules [i ]
654- break
655- }
649+ // 找到与流量相关的Rule
650+ var flowRule * model.Rule
651+ for i := range alert .Rules {
652+ if alert .Rules [i ].IsTransferDurationRule () {
653+ flowRule = & alert .Rules [i ]
654+ break
656655 }
656+ }
657657
658- // 如果没有流量相关规则,跳过
659- if flowRule == nil {
660- continue
661- }
658+ // 如果没有流量相关规则,跳过
659+ if flowRule == nil {
660+ continue
661+ }
662662
663- // 确保周期开始和结束时间正确设置
664- from := flowRule .GetTransferDurationStart ()
665- to := flowRule .GetTransferDurationEnd ()
666-
667- // 更新stats中的周期时间,确保与规则一致
668- stats .From = from
669- stats .To = to
670- stats .Max = uint64 (flowRule .Max )
671- stats .Name = alert .Name
672-
673- // 在读锁外部异步更新周期信息,避免死锁
674- go updateCycleStatsInfo (cycleID , from , to , uint64 (flowRule .Max ), alert .Name )
675-
676- // 生成流量数据条目
677- for serverID , transfer := range stats .Transfer {
678- serverName := ""
679- if stats .ServerName != nil {
680- if name , exists := stats .ServerName [serverID ]; exists {
681- serverName = name
682- }
683- }
663+ // 确保周期开始和结束时间正确设置
664+ from := flowRule .GetTransferDurationStart ()
665+ to := flowRule .GetTransferDurationEnd ()
684666
685- // 如果没有名称,尝试从ServerList获取
686- if serverName == "" {
687- singleton . ServerLock . RLock ()
688- if server := singleton . ServerList [ serverID ]; server != nil {
689- serverName = server .Name
690- }
691- singleton . ServerLock . RUnlock ()
692- }
667+ // 更新stats中的周期时间,确保与规则一致
668+ stats . From = from
669+ stats . To = to
670+ stats . Max = uint64 ( flowRule . Max )
671+ stats . Name = alert .Name
672+
673+ // 在读锁外部异步更新周期信息,避免死锁
674+ go updateCycleStatsInfo ( cycleID , from , to , uint64 ( flowRule . Max ), alert . Name )
693675
694- // 计算使用百分比
695- usedPercent := float64 (0 )
696- if stats .Max > 0 {
697- usedPercent = (float64 (transfer ) / float64 (stats .Max )) * 100
698- usedPercent = math .Max (0 , math .Min (100 , usedPercent )) // 限制在0-100范围
676+ // 生成流量数据条目
677+ for serverID , transfer := range stats .Transfer {
678+ serverName := ""
679+ if stats .ServerName != nil {
680+ if name , exists := stats .ServerName [serverID ]; exists {
681+ serverName = name
699682 }
683+ }
700684
701- // 获取周期单位和开始时间,用于前端展示
702- cycleUnit := flowRule .CycleUnit
703-
704- // 构建完整的流量数据项,包含周期信息
705- trafficItem := map [string ]interface {}{
706- "server_id" : serverID ,
707- "server_name" : serverName ,
708- "max_bytes" : stats .Max ,
709- "used_bytes" : transfer ,
710- "max_formatted" : formatBytes (stats .Max ),
711- "used_formatted" : formatBytes (transfer ),
712- "used_percent" : math .Round (usedPercent * 100 ) / 100 ,
713- "cycle_name" : stats .Name ,
714- "cycle_id" : strconv .FormatUint (cycleID , 10 ),
715- "cycle_start" : stats .From .Format (time .RFC3339 ),
716- "cycle_end" : stats .To .Format (time .RFC3339 ),
717- "cycle_unit" : cycleUnit ,
718- "cycle_interval" : flowRule .CycleInterval ,
719- "is_bytes_source" : true ,
720- "now" : time .Now ().Unix () * 1000 ,
685+ // 如果没有名称,尝试从ServerList获取
686+ if serverName == "" {
687+ singleton .ServerLock .RLock ()
688+ if server := singleton .ServerList [serverID ]; server != nil {
689+ serverName = server .Name
721690 }
691+ singleton .ServerLock .RUnlock ()
692+ }
693+
694+ // 计算使用百分比
695+ usedPercent := float64 (0 )
696+ if stats .Max > 0 {
697+ usedPercent = (float64 (transfer ) / float64 (stats .Max )) * 100
698+ usedPercent = math .Max (0 , math .Min (100 , usedPercent )) // 限制在0-100范围
699+ }
700+
701+ // 获取周期单位和开始时间,用于前端展示
702+ cycleUnit := flowRule .CycleUnit
703+
704+ // 构建完整的流量数据项,包含周期信息
705+ trafficItem := map [string ]interface {}{
706+ "server_id" : serverID ,
707+ "server_name" : serverName ,
708+ "max_bytes" : stats .Max ,
709+ "used_bytes" : transfer ,
710+ "max_formatted" : formatBytes (stats .Max ),
711+ "used_formatted" : formatBytes (transfer ),
712+ "used_percent" : math .Round (usedPercent * 100 ) / 100 ,
713+ "cycle_name" : stats .Name ,
714+ "cycle_id" : strconv .FormatUint (cycleID , 10 ),
715+ "cycle_start" : stats .From .Format (time .RFC3339 ),
716+ "cycle_end" : stats .To .Format (time .RFC3339 ),
717+ "cycle_unit" : cycleUnit ,
718+ "cycle_interval" : flowRule .CycleInterval ,
719+ "is_bytes_source" : true ,
720+ "now" : time .Now ().Unix () * 1000 ,
721+ }
722722
723- trafficData = append (trafficData , trafficItem )
723+ trafficData = append (trafficData , trafficItem )
724724 }
725725 }
726726
0 commit comments