refactor: move models to their own package to use the same as in the importer

This commit is contained in:
Patryk Hegenberg 2025-09-24 22:57:37 +02:00
parent 553a85562b
commit 9aa1b7384d
14 changed files with 170 additions and 152 deletions

View file

@ -12,6 +12,7 @@ import (
"strings"
"syscall"
"time"
"tixel_watch/models"
"github.com/elastic/go-elasticsearch/v8"
"github.com/shirou/gopsutil/cpu"
@ -27,8 +28,8 @@ import (
type SystemMetricsCollector struct {
config SystemMetrics
pollInterval int
lastNetworkStats map[string]NetworkStat
lastDiskStats map[string]DiskIOStat
lastNetworkStats map[string]models.NetworkStat
lastDiskStats map[string]models.DiskIOStat
lastMeasureTime time.Time
}
@ -36,8 +37,8 @@ func NewSystemMetricsCollector(config SystemMetrics, pollInterval int) *SystemMe
return &SystemMetricsCollector{
config: config,
pollInterval: pollInterval,
lastNetworkStats: make(map[string]NetworkStat),
lastDiskStats: make(map[string]DiskIOStat),
lastNetworkStats: make(map[string]models.NetworkStat),
lastDiskStats: make(map[string]models.DiskIOStat),
lastMeasureTime: time.Now(),
}
}
@ -67,8 +68,8 @@ func (smc *SystemMetricsCollector) Start(ctx context.Context, es *elasticsearch.
}
}
func (smc *SystemMetricsCollector) collectMetrics() (SystemResources, error) {
result := NewSystemResources()
func (smc *SystemMetricsCollector) collectMetrics() (models.SystemResources, error) {
result := models.NewSystemResources(hostname)
var err error
@ -146,7 +147,7 @@ func (smc *SystemMetricsCollector) collectMetrics() (SystemResources, error) {
return result, nil
}
func (smc *SystemMetricsCollector) collectDiskIOMetrics(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectDiskIOMetrics(result *models.SystemResources) error {
diskIOStats, err := disk.IOCounters()
if err != nil {
return err
@ -155,10 +156,10 @@ func (smc *SystemMetricsCollector) collectDiskIOMetrics(result *SystemResources)
currentTime := time.Now()
timeDiff := currentTime.Sub(smc.lastMeasureTime).Seconds()
result.DiskIOStats = make(map[string]DiskIOStat)
result.DiskIOStats = make(map[string]models.DiskIOStat)
for device, stats := range diskIOStats {
ioStat := DiskIOStat{
ioStat := models.DiskIOStat{
ReadBytes: stats.ReadBytes,
WriteBytes: stats.WriteBytes,
ReadOps: stats.ReadCount,
@ -188,13 +189,13 @@ func (smc *SystemMetricsCollector) collectDiskIOMetrics(result *SystemResources)
return nil
}
func (smc *SystemMetricsCollector) collectNetworkConnections(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectNetworkConnections(result *models.SystemResources) error {
connections, err := psnet.Connections("all")
if err != nil {
return err
}
stats := ConnectionStats{
stats := models.ConnectionStats{
ConnectionsByState: make(map[string]int32),
}
@ -222,7 +223,7 @@ func (smc *SystemMetricsCollector) collectNetworkConnections(result *SystemResou
return nil
}
func (smc *SystemMetricsCollector) collectLoadAverage(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectLoadAverage(result *models.SystemResources) error {
loadAvg, err := load.Avg()
if err != nil {
return err
@ -235,8 +236,8 @@ func (smc *SystemMetricsCollector) collectLoadAverage(result *SystemResources) e
return nil
}
func (smc *SystemMetricsCollector) collectTCPStats(result *SystemResources) error {
tcpStats := TCPStatistics{}
func (smc *SystemMetricsCollector) collectTCPStats(result *models.SystemResources) error {
tcpStats := models.TCPStatistics{}
if data, err := os.ReadFile("/proc/net/netstat"); err == nil {
content := string(data)
@ -251,8 +252,8 @@ func (smc *SystemMetricsCollector) collectTCPStats(result *SystemResources) erro
return nil
}
func (smc *SystemMetricsCollector) collectNetworkLatency(result *SystemResources) error {
result.NetworkLatency = make(map[string]LatencyInfo)
func (smc *SystemMetricsCollector) collectNetworkLatency(result *models.SystemResources) error {
result.NetworkLatency = make(map[string]models.LatencyInfo)
for _, host := range smc.config.LatencyTestHosts {
latency := smc.measureLatency(host)
@ -262,7 +263,7 @@ func (smc *SystemMetricsCollector) collectNetworkLatency(result *SystemResources
return nil
}
func (smc *SystemMetricsCollector) measureLatency(host string) LatencyInfo {
func (smc *SystemMetricsCollector) measureLatency(host string) models.LatencyInfo {
var latencies []time.Duration
var successful int
@ -279,7 +280,7 @@ func (smc *SystemMetricsCollector) measureLatency(host string) LatencyInfo {
}
if len(latencies) == 0 {
return LatencyInfo{Host: host, PacketLoss: 100.0}
return models.LatencyInfo{Host: host, PacketLoss: 100.0}
}
var total time.Duration
@ -300,7 +301,7 @@ func (smc *SystemMetricsCollector) measureLatency(host string) LatencyInfo {
packetLoss := float64(5-successful) / 5.0 * 100.0
jitter := max - min
return LatencyInfo{
return models.LatencyInfo{
Host: host,
MinLatency: min,
MaxLatency: max,
@ -310,13 +311,13 @@ func (smc *SystemMetricsCollector) measureLatency(host string) LatencyInfo {
}
}
func (smc *SystemMetricsCollector) collectBandwidthUsage(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectBandwidthUsage(result *models.SystemResources) error {
netStats, err := psnet.IOCounters(true)
if err != nil {
return err
}
result.BandwidthUtilization = make(map[string]BandwidthInfo)
result.BandwidthUtilization = make(map[string]models.BandwidthInfo)
currentTime := time.Now()
timeDiff := currentTime.Sub(smc.lastMeasureTime).Seconds()
@ -326,7 +327,7 @@ func (smc *SystemMetricsCollector) collectBandwidthUsage(result *SystemResources
continue
}
bandwidth := BandwidthInfo{Interface: stat.Name}
bandwidth := models.BandwidthInfo{Interface: stat.Name}
if lastStat, exists := smc.lastNetworkStats[stat.Name]; exists && timeDiff > 0 {
bytesDiffIn := float64(stat.BytesRecv - lastStat.BytesRecv)
@ -347,7 +348,7 @@ func (smc *SystemMetricsCollector) collectBandwidthUsage(result *SystemResources
}
for _, stat := range netStats {
smc.lastNetworkStats[stat.Name] = NetworkStat{
smc.lastNetworkStats[stat.Name] = models.NetworkStat{
BytesSent: stat.BytesSent,
BytesRecv: stat.BytesRecv,
PacketsSent: stat.PacketsSent,
@ -359,8 +360,8 @@ func (smc *SystemMetricsCollector) collectBandwidthUsage(result *SystemResources
return nil
}
func (smc *SystemMetricsCollector) collectSystemLimits(result *SystemResources) error {
limits := SystemLimitInfo{}
func (smc *SystemMetricsCollector) collectSystemLimits(result *models.SystemResources) error {
limits := models.SystemLimitInfo{}
if data, err := os.ReadFile("/proc/sys/fs/file-max"); err == nil {
if maxFiles, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64); err == nil {
@ -389,13 +390,13 @@ func (smc *SystemMetricsCollector) collectSystemLimits(result *SystemResources)
return nil
}
func (smc *SystemMetricsCollector) collectProcessMetrics(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectProcessMetrics(result *models.SystemResources) error {
processes, err := process.Processes()
if err != nil {
return err
}
var processInfos []ProcessInfo
var processInfos []models.ProcessInfo
var totalOpenFiles int32
for _, p := range processes {
@ -428,7 +429,7 @@ func (smc *SystemMetricsCollector) collectProcessMetrics(result *SystemResources
totalOpenFiles += openFiles
}
processInfos = append(processInfos, ProcessInfo{
processInfos = append(processInfos, models.ProcessInfo{
PID: p.Pid,
Name: name,
CPUPercent: cpuPercent,
@ -453,7 +454,7 @@ func (smc *SystemMetricsCollector) collectProcessMetrics(result *SystemResources
return nil
}
func (smc *SystemMetricsCollector) collectCPUMetrics(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectCPUMetrics(result *models.SystemResources) error {
cpuPercents, err := cpu.Percent(time.Second, false)
if err != nil {
return err
@ -470,7 +471,7 @@ func (smc *SystemMetricsCollector) collectCPUMetrics(result *SystemResources) er
return nil
}
func (smc *SystemMetricsCollector) collectMemoryMetrics(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectMemoryMetrics(result *models.SystemResources) error {
vmStat, err := mem.VirtualMemory()
if err != nil {
return err
@ -483,7 +484,7 @@ func (smc *SystemMetricsCollector) collectMemoryMetrics(result *SystemResources)
return nil
}
func (smc *SystemMetricsCollector) collectDiskMetrics(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectDiskMetrics(result *models.SystemResources) error {
for _, path := range smc.config.DiskPaths {
diskStat, err := disk.Usage(path)
if err != nil {
@ -491,7 +492,7 @@ func (smc *SystemMetricsCollector) collectDiskMetrics(result *SystemResources) e
continue
}
result.DiskUsage[path] = DiskUsage{
result.DiskUsage[path] = models.DiskUsage{
Used: diskStat.Used,
Total: diskStat.Total,
UsedPercent: diskStat.UsedPercent,
@ -502,7 +503,7 @@ func (smc *SystemMetricsCollector) collectDiskMetrics(result *SystemResources) e
return nil
}
func (smc *SystemMetricsCollector) collectNetworkMetrics(result *SystemResources) error {
func (smc *SystemMetricsCollector) collectNetworkMetrics(result *models.SystemResources) error {
netStats, err := psnet.IOCounters(true)
if err != nil {
return err
@ -510,7 +511,7 @@ func (smc *SystemMetricsCollector) collectNetworkMetrics(result *SystemResources
for _, stat := range netStats {
if len(smc.config.NetworkInterfaces) == 0 || slices.Contains(smc.config.NetworkInterfaces, stat.Name) {
result.NetworkStats[stat.Name] = NetworkStat{
result.NetworkStats[stat.Name] = models.NetworkStat{
BytesSent: stat.BytesSent,
BytesRecv: stat.BytesRecv,
PacketsSent: stat.PacketsSent,