- database_handler, knowledge_handler, model_handler - neo4j_handler, sub_table_handler - system_handler, upload_handler - knowledge_service, upload_service Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
package handler
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"x-agents/server/internal/model"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type SystemHandler struct{}
|
||
|
||
func NewSystemHandler() *SystemHandler {
|
||
return &SystemHandler{}
|
||
}
|
||
|
||
// @Summary 获取系统信息
|
||
// @Description 获取服务器系统信息(CPU、内存等)
|
||
// @Tags 系统
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Success 200 {object} model.SystemInfo
|
||
// @Failure 500 {object} map[string]string
|
||
// @Router /system/info [get]
|
||
func (h *SystemHandler) GetSystemInfo(c *gin.Context) {
|
||
info, err := getSystemInfo()
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, info)
|
||
}
|
||
|
||
// getSystemInfo 获取系统信息
|
||
func getSystemInfo() (*model.SystemInfo, error) {
|
||
// 获取CPU使用率
|
||
cpuPercent, err := getCPUPercent()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 获取CPU核心数
|
||
coreCount, err := getCPUCoreCount()
|
||
if err != nil {
|
||
coreCount = 0
|
||
}
|
||
|
||
// 获取CPU型号
|
||
modelName, err := getCPUModelName()
|
||
if err != nil {
|
||
modelName = "Unknown"
|
||
}
|
||
|
||
// 获取内存信息
|
||
memoryInfo, err := getMemoryInfo()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &model.SystemInfo{
|
||
CPU: model.CPUInfo{
|
||
Percent: cpuPercent,
|
||
CoreCount: coreCount,
|
||
ModelName: modelName,
|
||
},
|
||
Memory: *memoryInfo,
|
||
}, nil
|
||
}
|