mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 01:38:30 +02:00
simplify some code and implement metrics storage
This commit is contained in:
@@ -3,6 +3,7 @@ package systeminfo
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -54,17 +55,18 @@ type (
|
||||
UploadSpeed float64 `json:"upload_speed"`
|
||||
DownloadSpeed float64 `json:"download_speed"`
|
||||
}
|
||||
Sensors []sensors.TemperatureStat
|
||||
Aggregated []map[string]any
|
||||
)
|
||||
|
||||
type SystemInfo struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
CPUAverage *float64 `json:"cpu_average"`
|
||||
Memory *MemoryUsage `json:"memory"`
|
||||
Disks map[string]*Disk `json:"disks"` // disk usage by partition
|
||||
DisksIO map[string]*DiskIO `json:"disks_io"` // disk IO by device
|
||||
Network *Network `json:"network"`
|
||||
Sensors []sensors.TemperatureStat `json:"sensors"` // sensor temperature by key
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
CPUAverage *float64 `json:"cpu_average"`
|
||||
Memory *MemoryUsage `json:"memory"`
|
||||
Disks map[string]*Disk `json:"disks"` // disk usage by partition
|
||||
DisksIO map[string]*DiskIO `json:"disks_io"` // disk IO by device
|
||||
Network *Network `json:"network"`
|
||||
Sensors Sensors `json:"sensors"` // sensor temperature by key
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -93,7 +95,7 @@ var allQueries = []string{
|
||||
querySensorTemperature,
|
||||
}
|
||||
|
||||
var Poller = period.NewPollerWithAggregator("system_info", getSystemInfo, aggregate)
|
||||
var Poller = period.NewPoller("system_info", getSystemInfo, aggregate)
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() any {
|
||||
@@ -421,6 +423,26 @@ func (s *SystemInfo) MarshalJSON() ([]byte, error) {
|
||||
return []byte(b.String()), nil
|
||||
}
|
||||
|
||||
func (s *Sensors) UnmarshalJSON(data []byte) error {
|
||||
var v map[string]map[string]any
|
||||
if err := json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(v) == 0 {
|
||||
return nil
|
||||
}
|
||||
*s = make(Sensors, 0, len(v))
|
||||
for k, v := range v {
|
||||
*s = append(*s, sensors.TemperatureStat{
|
||||
SensorKey: k,
|
||||
Temperature: v["temperature"].(float64),
|
||||
High: v["high"].(float64),
|
||||
Critical: v["critical"].(float64),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// recharts friendly
|
||||
func aggregate(entries []*SystemInfo, query url.Values) (total int, result Aggregated) {
|
||||
n := len(entries)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/sensors"
|
||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
)
|
||||
|
||||
@@ -60,6 +61,20 @@ var testInfo = &SystemInfo{
|
||||
UploadSpeed: 1024.5,
|
||||
DownloadSpeed: 2048.5,
|
||||
},
|
||||
Sensors: []sensors.TemperatureStat{
|
||||
{
|
||||
SensorKey: "cpu_temp",
|
||||
Temperature: 30.0,
|
||||
High: 40.0,
|
||||
Critical: 50.0,
|
||||
},
|
||||
{
|
||||
SensorKey: "gpu_temp",
|
||||
Temperature: 40.0,
|
||||
High: 50.0,
|
||||
Critical: 60.0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestSystemInfo(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user