mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 17:45:17 +03:00
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
domainHub "github.com/lorsanstand/HomeOps-Hub/hub/internal/domain"
|
|
gen2 "github.com/lorsanstand/HomeOps-Hub/hub/internal/store/sqlc/gen"
|
|
"github.com/lorsanstand/HomeOps-Hub/shared/domain"
|
|
)
|
|
|
|
func toDBAgent(agent domainHub.CreateAgentModel) gen2.CreateAgentParams {
|
|
jsonCaps := toJSONCapabilities(agent.Capabilities)
|
|
capsStr := string(jsonCaps)
|
|
return gen2.CreateAgentParams{
|
|
AgentID: agent.AgentID,
|
|
AgentName: &agent.AgentName,
|
|
Architecture: agent.Architecture,
|
|
System: agent.System,
|
|
Hostname: agent.Hostname,
|
|
Version: agent.Version,
|
|
Capabilities: &capsStr,
|
|
}
|
|
}
|
|
|
|
func toUpdateDBAgent(agent domainHub.CreateAgentModel) gen2.UpdateAgentByIDParams {
|
|
jsonCaps := toJSONCapabilities(agent.Capabilities)
|
|
capsStr := string(jsonCaps)
|
|
return gen2.UpdateAgentByIDParams{
|
|
AgentID: agent.AgentID,
|
|
AgentName: &agent.AgentName,
|
|
Architecture: agent.Architecture,
|
|
System: agent.System,
|
|
Hostname: agent.Hostname,
|
|
Version: agent.Version,
|
|
Capabilities: &capsStr,
|
|
}
|
|
}
|
|
|
|
func toJSONCapabilities(caps []domain.Capability) []byte {
|
|
data, err := json.Marshal(caps)
|
|
if err != nil {
|
|
// Note: Error is silently handled - consider logging in production
|
|
return []byte{}
|
|
}
|
|
return data
|
|
}
|
|
|
|
func toAgentModel(dbAgent gen2.Agent) domainHub.AgentModel {
|
|
var dbAgentName string
|
|
if dbAgent.AgentName != nil {
|
|
dbAgentName = *dbAgent.AgentName
|
|
}
|
|
|
|
var capsBytes []byte
|
|
if dbAgent.Capabilities != nil {
|
|
capsBytes = []byte(*dbAgent.Capabilities)
|
|
}
|
|
|
|
return domainHub.AgentModel{
|
|
ID: int(dbAgent.ID),
|
|
AgentID: dbAgent.AgentID,
|
|
AgentName: dbAgentName,
|
|
Architecture: dbAgent.Architecture,
|
|
System: dbAgent.System,
|
|
Hostname: dbAgent.Hostname,
|
|
Capabilities: toDomainCapabilities(capsBytes),
|
|
}
|
|
}
|
|
|
|
func toDomainCapabilities(caps []byte) []domain.Capability {
|
|
var capabilities []domain.Capability
|
|
err := json.Unmarshal(caps, &capabilities)
|
|
if err != nil {
|
|
// Note: Error is silently handled - consider logging in production
|
|
return []domain.Capability{}
|
|
}
|
|
return capabilities
|
|
}
|
|
|
|
func toDBHeartbeat(heartbeat domainHub.CreateHeartbeatModel) gen2.InsertHeartbeatParams {
|
|
|
|
return gen2.InsertHeartbeatParams{
|
|
AgentID: heartbeat.AgentID,
|
|
HeartbeatTimestamp: heartbeat.Timestamp,
|
|
CpuUsage: heartbeat.Metrics.CPUUsage,
|
|
DiskUsage: heartbeat.Metrics.DiskUsage,
|
|
MemoryUsage: heartbeat.Metrics.MemoryUsage,
|
|
}
|
|
}
|
|
|
|
func toHeartBeatModel(heartbeat gen2.Heartbeat) domainHub.HeartbeatModel {
|
|
return domainHub.HeartbeatModel{
|
|
Timestamp: heartbeat.HeartbeatTimestamp,
|
|
AgentID: heartbeat.AgentID,
|
|
ID: int(heartbeat.ID),
|
|
Metrics: domainHub.SystemMetrics{
|
|
CPUUsage: heartbeat.CpuUsage,
|
|
DiskUsage: heartbeat.DiskUsage,
|
|
MemoryUsage: heartbeat.MemoryUsage,
|
|
},
|
|
}
|
|
}
|