mirror of
https://github.com/komari-monitor/komari.git
synced 2025-11-03 05:23:32 +00:00
移除 远程配置下发。
修改 客户端基本属性逻辑。 修改 最新上报key改为uuid 新增 客户端命名 新增 历史记录数据库压缩。 新增 在线节点信息。 新增 公开API /api/nodes 显示客户端基础属性
This commit is contained in:
@@ -4,21 +4,16 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/akizon77/komari/common"
|
||||
"github.com/akizon77/komari/database/clients"
|
||||
"github.com/akizon77/komari/database/dbcore"
|
||||
"github.com/akizon77/komari/database/history"
|
||||
"github.com/akizon77/komari/database/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AddClient(c *gin.Context) {
|
||||
var config common.ClientConfig
|
||||
if err := c.ShouldBindJSON(&config); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
uuid, token, err := clients.CreateClient(config)
|
||||
uuid, token, err := clients.CreateClient()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
@@ -29,10 +24,9 @@ func AddClient(c *gin.Context) {
|
||||
|
||||
func EditClient(c *gin.Context) {
|
||||
var req struct {
|
||||
UUID string `json:"uuid" binding:"required"`
|
||||
ClientName string `json:"client_name,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
Config common.ClientConfig `json:"config,omitempty"`
|
||||
UUID string `json:"uuid" binding:"required"`
|
||||
ClientName string `json:"name,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -40,49 +34,19 @@ func EditClient(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证配置
|
||||
if req.Config.Interval <= 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "Interval must be greater than 0"})
|
||||
return
|
||||
updates := make(map[string]interface{})
|
||||
updates["updated_at"] = time.Now()
|
||||
if req.ClientName != "" {
|
||||
updates["client_name"] = req.ClientName
|
||||
}
|
||||
|
||||
// 获取原始配置
|
||||
rawConfig, err := clients.GetClientConfig(req.UUID)
|
||||
if err != nil {
|
||||
if req.Token != "" {
|
||||
updates["token"] = req.Token
|
||||
}
|
||||
db := dbcore.GetDBInstance()
|
||||
if err := db.Model(&models.Client{}).Where("uuid = ?", req.UUID).Updates(updates).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 更新配置
|
||||
if req.Config.ClientUUID != "" {
|
||||
// 确保创建时间不变
|
||||
req.Config.CreatedAt = rawConfig.CreatedAt
|
||||
req.Config.UpdatedAt = time.Now()
|
||||
err = clients.UpdateClientConfig(req.Config)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 更新客户端名称
|
||||
if req.ClientName != "" {
|
||||
err = clients.EditClientName(req.UUID, req.ClientName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 更新token
|
||||
if req.Token != "" {
|
||||
err = clients.EditClientToken(req.UUID, req.Token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
||||
}
|
||||
|
||||
@@ -128,72 +92,21 @@ func GetClient(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result := getClientByUUID(uuid)
|
||||
if result == nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": "Failed to get client"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func getClientByUUID(uuid string) map[string]interface{} {
|
||||
clientBasicInfo, err := clients.GetClientBasicInfo(uuid)
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
clientBasicInfo = clients.ClientBasicInfo{}
|
||||
}
|
||||
config, err := clients.GetClientConfig(uuid)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
client, err := clients.GetClientByUUID(uuid)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
result := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"token": client.Token,
|
||||
"info": clientBasicInfo,
|
||||
"config": config,
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func getClientInfo(uuid string) (map[string]interface{}, error) {
|
||||
clientBasicInfo, err := clients.GetClientBasicInfo(uuid)
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
clientBasicInfo = clients.ClientBasicInfo{}
|
||||
}
|
||||
config, err := clients.GetClientConfig(uuid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client, err := clients.GetClientByUUID(uuid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"token": client.Token,
|
||||
"name": client.ClientName,
|
||||
"info": clientBasicInfo,
|
||||
"config": config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ListClients(c *gin.Context) {
|
||||
cls, err := clients.GetAllClients()
|
||||
result, err := clients.GetClientByUUID(uuid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
result := []map[string]interface{}{}
|
||||
for i := range cls {
|
||||
clientInfo, err := getClientInfo(cls[i].UUID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
result = append(result, clientInfo)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func ListClients(c *gin.Context) {
|
||||
cls, err := clients.GetAllClientBasicInfo()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, cls)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user