@@ -154,50 +154,48 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
154154 if singleton .Conf .DatabaseType == "badger" {
155155 // BadgerDB 模式下使用 MonitorAPI,只查询最近7天的ICMP/TCP监控数据
156156 if singleton .MonitorAPI != nil {
157- // 恢复3天数据展示,通过优化查询效率解决性能问题
157+ // 恢复原始查询逻辑:查询所有监控历史记录
158158 endTime := time .Now ()
159- startTime := endTime .AddDate (0 , 0 , - 3 ) // 恢复3天数据
159+ startTime := endTime .AddDate (0 , 0 , - 7 ) // 恢复原始的7天数据
160160
161161 if db .DB != nil {
162- // 性能优化:直接按服务器ID和时间范围查询,避免查询所有记录
162+ // 恢复原始的查询方法
163163 monitorOps := db .NewMonitorHistoryOps (db .DB )
164+ allHistories , err := monitorOps .GetAllMonitorHistoriesInRange (startTime , endTime )
165+ if err != nil {
166+ log .Printf ("查询网络监控历史记录失败: %v" , err )
167+ c .JSON (200 , []any {})
168+ return
169+ }
164170
165- // 预先获取监控配置
171+ // 恢复原始的过滤逻辑
166172 monitors := singleton .ServiceSentinelShared .Monitors ()
167173 monitorTypeMap := make (map [uint64 ]uint8 )
168- icmpTcpMonitorIDs := []uint64 {}
169174 if monitors != nil {
170175 for _ , monitor := range monitors {
171176 monitorTypeMap [monitor .ID ] = monitor .Type
172- if monitor .Type == model .TaskTypeICMPPing || monitor .Type == model .TaskTypeTCPPing {
173- icmpTcpMonitorIDs = append (icmpTcpMonitorIDs , monitor .ID )
174- }
175177 }
176178 }
177179
178- log .Printf ("服务器 %d 的ICMP/TCP监控器ID列表: %v" , server .ID , icmpTcpMonitorIDs )
179-
180- // 根本性能优化:使用高效的数据库查询,恢复合理的数据量
180+ // 恢复原始的记录过滤
181181 var networkHistories []* model.MonitorHistory
182+ count := 0
183+ maxRecords := 1000 // 恢复原始的记录数限制
182184
183- for _ , monitorID := range icmpTcpMonitorIDs {
184- // 使用优化的查询方法,每个监控器200条记录(3天数据)
185- histories , err := monitorOps .GetMonitorHistoriesByServerAndMonitorOptimized (server .ID , monitorID , startTime , endTime , 200 )
186- if err != nil {
187- log .Printf ("查询监控器 %d 的历史记录失败: %v" , monitorID , err )
188- continue
185+ for _ , history := range allHistories {
186+ if count >= maxRecords {
187+ break
188+ }
189+ if history != nil && history .ServerID == server .ID {
190+ if monitorType , exists := monitorTypeMap [history .MonitorID ]; exists &&
191+ (monitorType == model .TaskTypeICMPPing || monitorType == model .TaskTypeTCPPing ) {
192+ networkHistories = append (networkHistories , history )
193+ count ++
194+ }
189195 }
190-
191- log .Printf ("监控器 %d 在服务器 %d 上找到 %d 条记录" , monitorID , server .ID , len (histories ))
192- networkHistories = append (networkHistories , histories ... )
193196 }
194197
195198 log .Printf ("服务器 %d 最终返回 %d 条ICMP/TCP监控记录" , server .ID , len (networkHistories ))
196-
197- // 确保返回空数组而不是null
198- if networkHistories == nil {
199- networkHistories = []* model.MonitorHistory {}
200- }
201199 c .JSON (200 , networkHistories )
202200 } else {
203201 c .JSON (200 , []any {})
@@ -206,17 +204,16 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
206204 c .JSON (200 , []any {})
207205 }
208206 } else {
209- // SQLite 模式下直接查询ICMP/TCP监控数据,限制时间范围和数量
207+ // SQLite 模式下恢复原始查询逻辑
210208 if singleton .DB != nil {
211209 var networkHistories []* model.MonitorHistory
212210
213- // 性能优化:只查询最近7天的数据
214- startTime := time .Now ().AddDate (0 , 0 , - 7 )
211+ // 恢复原始的30天数据查询
212+ startTime := time .Now ().AddDate (0 , 0 , - 30 )
215213
216214 err := singleton .DB .Where ("server_id = ? AND created_at > ? AND monitor_id IN (SELECT id FROM monitors WHERE type IN (?, ?))" ,
217215 server .ID , startTime , model .TaskTypeICMPPing , model .TaskTypeTCPPing ).
218216 Order ("created_at DESC" ).
219- Limit (500 ). // 减少到500条记录
220217 Find (& networkHistories ).Error
221218
222219 if err != nil {
0 commit comments