- Client: Go-based Windows hardware monitoring (CPU, GPU, memory, disk, network, power) - Server: Go + Gin + SQLite backend with REST API - Frontend: Vue 3 + Element Plus dashboard - Docker deployment support - Windows service installation script
107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"pc-monitor-server/model"
|
|
"time"
|
|
)
|
|
|
|
type MetricsRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewMetricsRepository(db *sql.DB) *MetricsRepository {
|
|
return &MetricsRepository{db: db}
|
|
}
|
|
|
|
func (r *MetricsRepository) Save(m *model.Metrics) error {
|
|
cpuCoreJSON, _ := json.Marshal(m.CPUCoreUsage)
|
|
netJSON, _ := json.Marshal(m.NetworkInterfaces)
|
|
diskJSON, _ := json.Marshal(m.Disks)
|
|
|
|
_, err := r.db.Exec(
|
|
`INSERT INTO metrics (
|
|
device_id, timestamp, cpu_usage, cpu_temperature, cpu_core_usage,
|
|
memory_total, memory_used, memory_usage,
|
|
gpu_usage, gpu_temperature, gpu_memory_total, gpu_memory_used, gpu_name,
|
|
network_interfaces, disks,
|
|
power_status, battery_level, power_source
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
m.DeviceID, m.Timestamp,
|
|
m.CPUUsage, m.CPUTemperature, string(cpuCoreJSON),
|
|
m.MemoryTotal, m.MemoryUsed, m.MemoryUsage,
|
|
m.GPUUsage, m.GPUTemperature, m.GPUMemoryTotal, m.GPUMemoryUsed, m.GPUName,
|
|
string(netJSON), string(diskJSON),
|
|
m.PowerStatus, m.BatteryLevel, m.PowerSource,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (r *MetricsRepository) GetLatest(deviceID string) (*model.Metrics, error) {
|
|
m := &model.Metrics{}
|
|
var cpuCoreJSON, netJSON, diskJSON string
|
|
|
|
err := r.db.QueryRow(
|
|
`SELECT device_id, timestamp, cpu_usage, cpu_temperature, cpu_core_usage,
|
|
memory_total, memory_used, memory_usage,
|
|
gpu_usage, gpu_temperature, gpu_memory_total, gpu_memory_used, gpu_name,
|
|
network_interfaces, disks,
|
|
power_status, battery_level, power_source
|
|
FROM metrics WHERE device_id=? ORDER BY timestamp DESC LIMIT 1`, deviceID,
|
|
).Scan(&m.DeviceID, &m.Timestamp,
|
|
&m.CPUUsage, &m.CPUTemperature, &cpuCoreJSON,
|
|
&m.MemoryTotal, &m.MemoryUsed, &m.MemoryUsage,
|
|
&m.GPUUsage, &m.GPUTemperature, &m.GPUMemoryTotal, &m.GPUMemoryUsed, &m.GPUName,
|
|
&netJSON, &diskJSON,
|
|
&m.PowerStatus, &m.BatteryLevel, &m.PowerSource)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
json.Unmarshal([]byte(cpuCoreJSON), &m.CPUCoreUsage)
|
|
json.Unmarshal([]byte(netJSON), &m.NetworkInterfaces)
|
|
json.Unmarshal([]byte(diskJSON), &m.Disks)
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (r *MetricsRepository) GetHistory(deviceID string, start, end time.Time, limit int) ([]model.Metrics, error) {
|
|
rows, err := r.db.Query(
|
|
`SELECT device_id, timestamp, cpu_usage, cpu_temperature, cpu_core_usage,
|
|
memory_total, memory_used, memory_usage,
|
|
gpu_usage, gpu_temperature, gpu_memory_total, gpu_memory_used, gpu_name,
|
|
network_interfaces, disks,
|
|
power_status, battery_level, power_source
|
|
FROM metrics WHERE device_id=? AND timestamp BETWEEN ? AND ?
|
|
ORDER BY timestamp DESC LIMIT ?`, deviceID, start, end, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var metrics []model.Metrics
|
|
for rows.Next() {
|
|
var m model.Metrics
|
|
var cpuCoreJSON, netJSON, diskJSON string
|
|
if err := rows.Scan(&m.DeviceID, &m.Timestamp,
|
|
&m.CPUUsage, &m.CPUTemperature, &cpuCoreJSON,
|
|
&m.MemoryTotal, &m.MemoryUsed, &m.MemoryUsage,
|
|
&m.GPUUsage, &m.GPUTemperature, &m.GPUMemoryTotal, &m.GPUMemoryUsed, &m.GPUName,
|
|
&netJSON, &diskJSON,
|
|
&m.PowerStatus, &m.BatteryLevel, &m.PowerSource); err != nil {
|
|
return nil, err
|
|
}
|
|
json.Unmarshal([]byte(cpuCoreJSON), &m.CPUCoreUsage)
|
|
json.Unmarshal([]byte(netJSON), &m.NetworkInterfaces)
|
|
json.Unmarshal([]byte(diskJSON), &m.Disks)
|
|
metrics = append(metrics, m)
|
|
}
|
|
return metrics, nil
|
|
}
|
|
|
|
func (r *MetricsRepository) Cleanup(before time.Time) error {
|
|
_, err := r.db.Exec("DELETE FROM metrics WHERE timestamp < ?", before)
|
|
return err
|
|
}
|