解决内存占用异常高的问题
This commit is contained in:
@@ -476,13 +476,15 @@ func broadcastMetricsUpdate(deviceID string, metrics map[string]interface{}) {
|
||||
func GetCPUMetrics(c *gin.Context) {
|
||||
// 获取查询参数
|
||||
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
|
||||
startTime := c.DefaultQuery("start_time", "-24h")
|
||||
startTime := c.DefaultQuery("start_time", "-1h") // 缩短默认查询时间范围到1小时,减少默认数据量
|
||||
endTime := c.DefaultQuery("end_time", "now()")
|
||||
aggregation := c.DefaultQuery("aggregation", "average")
|
||||
interval := c.DefaultQuery("interval", "10s") // 添加interval参数,默认10秒
|
||||
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数,默认5000条记录
|
||||
limit, _ := strconv.Atoi(limitStr)
|
||||
|
||||
// 查询数据
|
||||
points, err := globalStorage.QueryMetrics(context.Background(), deviceID, "cpu", startTime, endTime)
|
||||
points, err := globalStorage.QueryMetrics(context.Background(), deviceID, "cpu", startTime, endTime, limit)
|
||||
if err != nil {
|
||||
// 只记录警告,返回空数据
|
||||
log.Printf("Warning: Failed to query CPU metrics: %v", err)
|
||||
@@ -504,13 +506,15 @@ func GetCPUMetrics(c *gin.Context) {
|
||||
func GetMemoryMetrics(c *gin.Context) {
|
||||
// 获取查询参数
|
||||
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
|
||||
startTime := c.DefaultQuery("start_time", "-24h")
|
||||
startTime := c.DefaultQuery("start_time", "-1h") // 缩短默认查询时间范围到1小时,减少默认数据量
|
||||
endTime := c.DefaultQuery("end_time", "now()")
|
||||
aggregation := c.DefaultQuery("aggregation", "average")
|
||||
interval := c.DefaultQuery("interval", "10s") // 添加interval参数,默认10秒
|
||||
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数,默认5000条记录
|
||||
limit, _ := strconv.Atoi(limitStr)
|
||||
|
||||
// 查询数据
|
||||
points, err := globalStorage.QueryMetrics(context.Background(), deviceID, "memory", startTime, endTime)
|
||||
points, err := globalStorage.QueryMetrics(context.Background(), deviceID, "memory", startTime, endTime, limit)
|
||||
if err != nil {
|
||||
// 只记录警告,返回空数据
|
||||
log.Printf("Warning: Failed to query memory metrics: %v", err)
|
||||
@@ -532,13 +536,15 @@ func GetMemoryMetrics(c *gin.Context) {
|
||||
func GetDiskMetrics(c *gin.Context) {
|
||||
// 获取查询参数
|
||||
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
|
||||
startTime := c.DefaultQuery("start_time", "-24h")
|
||||
startTime := c.DefaultQuery("start_time", "-1h") // 缩短默认查询时间范围到1小时,减少默认数据量
|
||||
endTime := c.DefaultQuery("end_time", "now()")
|
||||
aggregation := c.DefaultQuery("aggregation", "average")
|
||||
interval := c.DefaultQuery("interval", "10s") // 添加interval参数,默认10秒
|
||||
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数,默认5000条记录
|
||||
limit, _ := strconv.Atoi(limitStr)
|
||||
|
||||
// 查询数据
|
||||
points, err := globalStorage.QueryMetrics(context.Background(), deviceID, "disk", startTime, endTime)
|
||||
points, err := globalStorage.QueryMetrics(context.Background(), deviceID, "disk", startTime, endTime, limit)
|
||||
if err != nil {
|
||||
// 只记录警告,返回空数据
|
||||
log.Printf("Warning: Failed to query disk metrics: %v", err)
|
||||
@@ -575,18 +581,20 @@ func GetDiskMetrics(c *gin.Context) {
|
||||
func GetNetworkMetrics(c *gin.Context) {
|
||||
// 获取查询参数
|
||||
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
|
||||
startTime := c.DefaultQuery("start_time", "-24h")
|
||||
startTime := c.DefaultQuery("start_time", "-1h") // 缩短默认查询时间范围到1小时,减少默认数据量
|
||||
endTime := c.DefaultQuery("end_time", "now()")
|
||||
aggregation := c.DefaultQuery("aggregation", "average")
|
||||
interval := c.DefaultQuery("interval", "10s") // 添加interval参数,默认10秒
|
||||
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数,默认5000条记录
|
||||
limit, _ := strconv.Atoi(limitStr)
|
||||
|
||||
// 查询发送和接收的网络速率指标
|
||||
sentPoints, err1 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_sent", startTime, endTime)
|
||||
receivedPoints, err2 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_received", startTime, endTime)
|
||||
sentPoints, err1 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_sent", startTime, endTime, limit)
|
||||
receivedPoints, err2 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_received", startTime, endTime, limit)
|
||||
|
||||
// 查询发送和接收的累积总流量指标
|
||||
txBytesPoints, err3 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_tx_bytes", startTime, endTime)
|
||||
rxBytesPoints, err4 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_rx_bytes", startTime, endTime)
|
||||
txBytesPoints, err3 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_tx_bytes", startTime, endTime, limit)
|
||||
rxBytesPoints, err4 := globalStorage.QueryMetrics(context.Background(), deviceID, "network_rx_bytes", startTime, endTime, limit)
|
||||
|
||||
// 处理错误
|
||||
if err1 != nil {
|
||||
|
||||
Reference in New Issue
Block a user