解决日志api的message字段为空的问题

This commit is contained in:
Alex Yang
2025-12-05 13:25:34 +08:00
parent f429c340fa
commit b9f32dff3b
11 changed files with 193033 additions and 70 deletions

View File

@@ -337,6 +337,16 @@ func HandleMetricsPost(c *gin.Context) {
compatDisk[mountpoint] = diskMetrics.UsedPercent
}
// 转换日志格式为LogMetrics
logMetricsList := make([]LogMetrics, 0, len(req.Logs))
for _, logEntry := range req.Logs {
logMetricsList = append(logMetricsList, LogMetrics{
Source: logEntry.Source,
Time: logEntry.Time.Format(time.RFC3339),
Message: logEntry.Message,
})
}
metrics := map[string]interface{}{
"cpu": req.CPU,
"cpu_hz": req.CPUHz,
@@ -349,6 +359,7 @@ func HandleMetricsPost(c *gin.Context) {
"rx_bytes": totalRxBytes,
},
"network_interfaces": req.Network,
"logs": logMetricsList,
}
broadcastMetricsUpdate(deviceID, metrics)
}
@@ -475,12 +486,12 @@ func broadcastMetricsUpdate(deviceID string, metrics map[string]interface{}) {
// GetCPUMetrics 获取CPU指标
func GetCPUMetrics(c *gin.Context) {
// 获取查询参数
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
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条记录
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数默认5000条记录
limit, _ := strconv.Atoi(limitStr)
// 查询数据
@@ -505,12 +516,12 @@ func GetCPUMetrics(c *gin.Context) {
// GetMemoryMetrics 获取内存指标
func GetMemoryMetrics(c *gin.Context) {
// 获取查询参数
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
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条记录
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数默认5000条记录
limit, _ := strconv.Atoi(limitStr)
// 查询数据
@@ -535,12 +546,12 @@ func GetMemoryMetrics(c *gin.Context) {
// GetDiskMetrics 获取磁盘指标
func GetDiskMetrics(c *gin.Context) {
// 获取查询参数
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
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条记录
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数默认5000条记录
limit, _ := strconv.Atoi(limitStr)
// 查询数据
@@ -580,12 +591,12 @@ func GetDiskMetrics(c *gin.Context) {
// GetNetworkMetrics 获取网络指标
func GetNetworkMetrics(c *gin.Context) {
// 获取查询参数
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
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条记录
limitStr := c.DefaultQuery("limit", "5000") // 添加limit参数默认5000条记录
limit, _ := strconv.Atoi(limitStr)
// 查询发送和接收的网络速率指标
@@ -779,18 +790,18 @@ func GetAllDeviceStatus(c *gin.Context) {
allDevices := deviceStorage.GetDevices()
// 查询每个设备的状态
result := make([]map[string]interface{}, 0, len(allDevices))
for _, device := range allDevices {
// 查询设备监控数据
_, status, _ := globalStorage.QueryDeviceStatus(context.Background(), device.ID)
result := make([]map[string]interface{}, 0, len(allDevices))
for _, device := range allDevices {
// 查询设备监控数据
_, status, _ := globalStorage.QueryDeviceStatus(context.Background(), device.ID)
// 总是返回设备信息,无论是否有监控数据
result = append(result, map[string]interface{}{
"id": device.ID,
"name": device.Name,
"status": status,
})
}
// 总是返回设备信息,无论是否有监控数据
result = append(result, map[string]interface{}{
"id": device.ID,
"name": device.Name,
"status": status,
})
}
c.JSON(http.StatusOK, gin.H{
"devices": result,
@@ -1011,7 +1022,13 @@ func GetDiskDetails(c *gin.Context) {
// GetLogs 获取系统日志
func GetLogs(c *gin.Context) {
// 获取查询参数
deviceID := c.Query("device_id") // 不使用默认值,空值表示查询所有设备
deviceID := c.Query("device_id") // 必须参数,不能为空
if deviceID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "device_id is required",
})
return
}
startTime := c.DefaultQuery("start_time", "-24h")
endTime := c.DefaultQuery("end_time", "now()")
@@ -1063,6 +1080,6 @@ func GetLogs(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"data": logs,
"logs": logs,
})
}