mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 16:58:31 +02:00
refactor: move task, error and testing utils to separte repo; apply gofumpt
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
"github.com/yusing/godoxy/internal/task"
|
||||
"github.com/yusing/godoxy/internal/utils/atomic"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
"github.com/yusing/goutils/task"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
"github.com/shirou/gopsutil/v4/sensors"
|
||||
"github.com/shirou/gopsutil/v4/warning"
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
"github.com/yusing/godoxy/internal/metrics/period"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
// json tags are left for tests
|
||||
|
||||
@@ -10,96 +10,98 @@ import (
|
||||
"github.com/shirou/gopsutil/v4/mem"
|
||||
"github.com/shirou/gopsutil/v4/net"
|
||||
"github.com/shirou/gopsutil/v4/sensors"
|
||||
. "github.com/yusing/godoxy/internal/utils/testing"
|
||||
expect "github.com/yusing/goutils/testing"
|
||||
)
|
||||
|
||||
// Create test data
|
||||
var cpuAvg = 45.67
|
||||
var testInfo = &SystemInfo{
|
||||
Timestamp: 123456,
|
||||
CPUAverage: &cpuAvg,
|
||||
Memory: &mem.VirtualMemoryStat{
|
||||
Total: 16000000000,
|
||||
Available: 8000000000,
|
||||
Used: 8000000000,
|
||||
UsedPercent: 50.0,
|
||||
},
|
||||
Disks: map[string]*disk.UsageStat{
|
||||
"sda": {
|
||||
Path: "/",
|
||||
Fstype: "ext4",
|
||||
Total: 500000000000,
|
||||
Free: 250000000000,
|
||||
Used: 250000000000,
|
||||
var (
|
||||
cpuAvg = 45.67
|
||||
testInfo = &SystemInfo{
|
||||
Timestamp: 123456,
|
||||
CPUAverage: &cpuAvg,
|
||||
Memory: &mem.VirtualMemoryStat{
|
||||
Total: 16000000000,
|
||||
Available: 8000000000,
|
||||
Used: 8000000000,
|
||||
UsedPercent: 50.0,
|
||||
},
|
||||
"nvme0n1": {
|
||||
Path: "/",
|
||||
Fstype: "zfs",
|
||||
Total: 500000000000,
|
||||
Free: 250000000000,
|
||||
Used: 250000000000,
|
||||
UsedPercent: 50.0,
|
||||
Disks: map[string]*disk.UsageStat{
|
||||
"sda": {
|
||||
Path: "/",
|
||||
Fstype: "ext4",
|
||||
Total: 500000000000,
|
||||
Free: 250000000000,
|
||||
Used: 250000000000,
|
||||
UsedPercent: 50.0,
|
||||
},
|
||||
"nvme0n1": {
|
||||
Path: "/",
|
||||
Fstype: "zfs",
|
||||
Total: 500000000000,
|
||||
Free: 250000000000,
|
||||
Used: 250000000000,
|
||||
UsedPercent: 50.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
DisksIO: map[string]*disk.IOCountersStat{
|
||||
"media": {
|
||||
Name: "media",
|
||||
ReadBytes: 1000000,
|
||||
WriteBytes: 2000000,
|
||||
ReadSpeed: 100.5,
|
||||
WriteSpeed: 200.5,
|
||||
Iops: 1000,
|
||||
DisksIO: map[string]*disk.IOCountersStat{
|
||||
"media": {
|
||||
Name: "media",
|
||||
ReadBytes: 1000000,
|
||||
WriteBytes: 2000000,
|
||||
ReadSpeed: 100.5,
|
||||
WriteSpeed: 200.5,
|
||||
Iops: 1000,
|
||||
},
|
||||
"nvme0n1": {
|
||||
Name: "nvme0n1",
|
||||
ReadBytes: 1000000,
|
||||
WriteBytes: 2000000,
|
||||
ReadSpeed: 100.5,
|
||||
WriteSpeed: 200.5,
|
||||
Iops: 1000,
|
||||
},
|
||||
},
|
||||
"nvme0n1": {
|
||||
Name: "nvme0n1",
|
||||
ReadBytes: 1000000,
|
||||
WriteBytes: 2000000,
|
||||
ReadSpeed: 100.5,
|
||||
WriteSpeed: 200.5,
|
||||
Iops: 1000,
|
||||
Network: &net.IOCountersStat{
|
||||
BytesSent: 5000000,
|
||||
BytesRecv: 10000000,
|
||||
UploadSpeed: 1024.5,
|
||||
DownloadSpeed: 2048.5,
|
||||
},
|
||||
},
|
||||
Network: &net.IOCountersStat{
|
||||
BytesSent: 5000000,
|
||||
BytesRecv: 10000000,
|
||||
UploadSpeed: 1024.5,
|
||||
DownloadSpeed: 2048.5,
|
||||
},
|
||||
Sensors: []sensors.TemperatureStat{
|
||||
{
|
||||
SensorKey: "cpu_temp",
|
||||
Temperature: 30.0,
|
||||
High: 40.0,
|
||||
Critical: 50.0,
|
||||
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,
|
||||
},
|
||||
},
|
||||
{
|
||||
SensorKey: "gpu_temp",
|
||||
Temperature: 40.0,
|
||||
High: 50.0,
|
||||
Critical: 60.0,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
func TestSystemInfo(t *testing.T) {
|
||||
// Test marshaling
|
||||
data, err := json.Marshal(testInfo)
|
||||
ExpectNoError(t, err)
|
||||
expect.NoError(t, err)
|
||||
|
||||
// Test unmarshaling back
|
||||
var decoded SystemInfo
|
||||
err = json.Unmarshal(data, &decoded)
|
||||
ExpectNoError(t, err)
|
||||
expect.NoError(t, err)
|
||||
|
||||
// Compare original and decoded
|
||||
ExpectEqual(t, decoded.Timestamp, testInfo.Timestamp)
|
||||
ExpectEqual(t, *decoded.CPUAverage, *testInfo.CPUAverage)
|
||||
ExpectEqual(t, decoded.Memory, testInfo.Memory)
|
||||
ExpectEqual(t, decoded.Disks, testInfo.Disks)
|
||||
ExpectEqual(t, decoded.DisksIO, testInfo.DisksIO)
|
||||
ExpectEqual(t, decoded.Network, testInfo.Network)
|
||||
ExpectEqual(t, decoded.Sensors, testInfo.Sensors)
|
||||
expect.Equal(t, decoded.Timestamp, testInfo.Timestamp)
|
||||
expect.Equal(t, *decoded.CPUAverage, *testInfo.CPUAverage)
|
||||
expect.Equal(t, decoded.Memory, testInfo.Memory)
|
||||
expect.Equal(t, decoded.Disks, testInfo.Disks)
|
||||
expect.Equal(t, decoded.DisksIO, testInfo.DisksIO)
|
||||
expect.Equal(t, decoded.Network, testInfo.Network)
|
||||
expect.Equal(t, decoded.Sensors, testInfo.Sensors)
|
||||
|
||||
// Test nil fields
|
||||
nilInfo := &SystemInfo{
|
||||
@@ -107,18 +109,18 @@ func TestSystemInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
data, err = json.Marshal(nilInfo)
|
||||
ExpectNoError(t, err)
|
||||
expect.NoError(t, err)
|
||||
|
||||
var decodedNil SystemInfo
|
||||
err = json.Unmarshal(data, &decodedNil)
|
||||
ExpectNoError(t, err)
|
||||
expect.NoError(t, err)
|
||||
|
||||
ExpectEqual(t, decodedNil.Timestamp, nilInfo.Timestamp)
|
||||
ExpectTrue(t, decodedNil.CPUAverage == nil)
|
||||
ExpectTrue(t, decodedNil.Memory == nil)
|
||||
ExpectTrue(t, decodedNil.Disks == nil)
|
||||
ExpectTrue(t, decodedNil.Network == nil)
|
||||
ExpectTrue(t, decodedNil.Sensors == nil)
|
||||
expect.Equal(t, decodedNil.Timestamp, nilInfo.Timestamp)
|
||||
expect.True(t, decodedNil.CPUAverage == nil)
|
||||
expect.True(t, decodedNil.Memory == nil)
|
||||
expect.True(t, decodedNil.Disks == nil)
|
||||
expect.True(t, decodedNil.Network == nil)
|
||||
expect.True(t, decodedNil.Sensors == nil)
|
||||
}
|
||||
|
||||
func TestSerialize(t *testing.T) {
|
||||
@@ -130,15 +132,15 @@ func TestSerialize(t *testing.T) {
|
||||
t.Run(string(query), func(t *testing.T) {
|
||||
_, result := aggregate(entries, url.Values{"aggregate": []string{string(query)}})
|
||||
s, err := result.MarshalJSON()
|
||||
ExpectNoError(t, err)
|
||||
expect.NoError(t, err)
|
||||
var v []map[string]any
|
||||
ExpectNoError(t, json.Unmarshal(s, &v))
|
||||
ExpectEqual(t, len(v), len(result.Entries))
|
||||
expect.NoError(t, json.Unmarshal(s, &v))
|
||||
expect.Equal(t, len(v), len(result.Entries))
|
||||
for i, m := range v {
|
||||
for k, v := range m {
|
||||
// some int64 values are converted to float64 on json.Unmarshal
|
||||
vv := reflect.ValueOf(result.Entries[i][k])
|
||||
ExpectEqual(t, reflect.ValueOf(v).Convert(vv.Type()).Interface(), vv.Interface())
|
||||
expect.Equal(t, reflect.ValueOf(v).Convert(vv.Type()).Interface(), vv.Interface())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -4,9 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
config "github.com/yusing/godoxy/internal/config/types"
|
||||
|
||||
Reference in New Issue
Block a user