fix: 代码优化

This commit is contained in:
TsMask
2025-07-15 15:16:44 +08:00
parent 9f6a7f3bb7
commit 150551acce
12 changed files with 202 additions and 130 deletions

View File

@@ -2,6 +2,7 @@ package service
import (
"fmt"
"sync"
"testing"
"time"
@@ -14,7 +15,7 @@ import (
func TestInfo(t *testing.T) {
s := MonitorInfo{}
s.load(0.5) // 0.5 半分钟
s.Load(5 * time.Second) // 0.5 半分钟
fmt.Println(s)
select {}
@@ -28,34 +29,22 @@ type MonitorInfo struct {
}
// load 执行资源获取
func (m *MonitorInfo) load(interval float64) {
var itemBase MonitorBase
itemBase.CreateTime = time.Now().UnixMilli()
loadInfo, _ := load.Avg()
itemBase.CPULoad1 = loadInfo.Load1
itemBase.CPULoad5 = loadInfo.Load5
itemBase.CPULoad15 = loadInfo.Load15
totalPercent, _ := cpu.Percent(3*time.Second, false)
if len(totalPercent) > 0 {
itemBase.CPU = totalPercent[0]
}
cpuCount, _ := cpu.Counts(false)
cpuAvg := (float64(cpuCount*2) * 0.75) * 100
itemBase.LoadUsage = 0
if cpuAvg > 0 {
itemBase.LoadUsage = loadInfo.Load1 / cpuAvg
}
memoryInfo, _ := mem.VirtualMemory()
itemBase.Memory = memoryInfo.UsedPercent
m.MonitorBase = itemBase
// 求平均
m.MonitorIO = loadDiskIO(interval)
m.MonitorNetwork = loadNetIO(interval)
func (m *MonitorInfo) Load(duration time.Duration) {
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
m.MonitorBase = loadCPUMem(duration)
}()
go func() {
defer wg.Done()
m.MonitorIO = loadDiskIO(duration)
}()
go func() {
defer wg.Done()
m.MonitorNetwork = loadNetIO(duration)
}()
wg.Wait()
}
// MonitorBase 监控_基本信息 monitor_base
@@ -73,25 +62,49 @@ type MonitorBase struct {
type MonitorIO struct {
CreateTime int64 `json:"createTime"` // 创建时间
Name string `json:"name"` // 磁盘名
Read int64 `json:"read"` // 读取K
Write int64 `json:"write"` // 写入K
Count int64 `json:"count"` // 次数
Time int64 `json:"time"` // 耗时
Read uint64 `json:"read"` // 读取 Bytes
Write uint64 `json:"write"` // 写入 Bytes
Count uint64 `json:"count"` // 次数
Time uint64 `json:"time"` // 耗时
}
// MonitorNetwork 监控_网络IO monitor_network
type MonitorNetwork struct {
CreateTime int64 `json:"createTime"` // 创建时间
Name string `json:"name"` // 网卡名
Up float64 `json:"up"` // 上行
Down float64 `json:"down"` // 下行
CreateTime int64 `json:"createTime"` // 创建时间
Name string `json:"name"` // 网卡名
Up uint64 `json:"up"` // 上行 bytes
Down uint64 `json:"down"` // 下行 bytes
}
// loadCPUMem CPU内存使用率interval表示采集的平均值分钟
func loadCPUMem(duration time.Duration) MonitorBase {
var itemBase MonitorBase
itemBase.CreateTime = time.Now().UnixMilli()
loadInfo, _ := load.Avg()
itemBase.CPULoad1 = loadInfo.Load1
itemBase.CPULoad5 = loadInfo.Load5
itemBase.CPULoad15 = loadInfo.Load15
totalPercent, _ := cpu.Percent(duration, false)
if len(totalPercent) > 0 {
itemBase.CPU = totalPercent[0]
}
if cpuCount, _ := cpu.Counts(false); cpuCount > 0 {
itemBase.LoadUsage = loadInfo.Load1 / float64(cpuCount)
} else {
itemBase.LoadUsage = 0
}
memoryInfo, _ := mem.VirtualMemory()
itemBase.Memory = memoryInfo.UsedPercent
return itemBase
}
// loadDiskIO 磁盘读写interval表示采集的平均值分钟
func loadDiskIO(interval float64) []MonitorIO {
func loadDiskIO(duration time.Duration) []MonitorIO {
ioStat, _ := disk.IOCounters()
time.Sleep(time.Duration(interval) * time.Minute)
time.Sleep(duration)
ioStat2, _ := disk.IOCounters()
var ioList []MonitorIO
@@ -104,32 +117,24 @@ func loadDiskIO(interval float64) []MonitorIO {
itemIO.Name = io1.Name
if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes {
itemIO.Read = int64(float64(io2.ReadBytes-io1.ReadBytes) / interval / 60)
itemIO.Read = io2.ReadBytes - io1.ReadBytes
}
if io2.WriteBytes != 0 && io1.WriteBytes != 0 && io2.WriteBytes > io1.WriteBytes {
itemIO.Write = int64(float64(io2.WriteBytes-io1.WriteBytes) / interval / 60)
itemIO.Write = io2.WriteBytes - io1.WriteBytes
}
if io2.ReadCount != 0 && io1.ReadCount != 0 && io2.ReadCount > io1.ReadCount {
itemIO.Count = int64(float64(io2.ReadCount-io1.ReadCount) / interval / 60)
itemIO.Count = io2.ReadCount - io1.ReadCount
}
writeCount := int64(0)
if io2.WriteCount != 0 && io1.WriteCount != 0 && io2.WriteCount > io1.WriteCount {
writeCount = int64(float64(io2.WriteCount-io1.WriteCount) / interval * 60)
}
if writeCount > itemIO.Count {
itemIO.Count = writeCount
itemIO.Count += io2.WriteCount - io1.WriteCount
}
if io2.ReadTime != 0 && io1.ReadTime != 0 && io2.ReadTime > io1.ReadTime {
itemIO.Time = int64(float64(io2.ReadTime-io1.ReadTime) / interval / 60)
itemIO.Time = io2.ReadTime - io1.ReadTime
}
writeTime := int64(0)
if io2.WriteTime != 0 && io1.WriteTime != 0 && io2.WriteTime > io1.WriteTime {
writeTime = int64(float64(io2.WriteTime-io1.WriteTime) / interval / 60)
}
if writeTime > itemIO.Time {
itemIO.Time = writeTime
itemIO.Time += io2.WriteTime - io1.WriteTime
}
ioList = append(ioList, itemIO)
break
@@ -140,7 +145,7 @@ func loadDiskIO(interval float64) []MonitorIO {
}
// loadNetIO 网络接口包括虚拟接口interval表示采集的平均值分钟
func loadNetIO(interval float64) []MonitorNetwork {
func loadNetIO(duration time.Duration) []MonitorNetwork {
// 获取当前时刻
netStat, _ := net.IOCounters(true)
netStatAll, _ := net.IOCounters(false)
@@ -148,7 +153,7 @@ func loadNetIO(interval float64) []MonitorNetwork {
netStatList = append(netStatList, netStat...)
netStatList = append(netStatList, netStatAll...)
time.Sleep(time.Duration(interval) * time.Minute)
time.Sleep(duration)
// 获取结束时刻
netStat2, _ := net.IOCounters(true)
@@ -168,10 +173,10 @@ func loadNetIO(interval float64) []MonitorNetwork {
// 如果结束时刻发送字节数和当前时刻发送字节数都不为零,并且结束时刻发送字节数大于当前时刻发送字节数
if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent {
itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / interval / 60
itemNet.Up = net2.BytesSent - net1.BytesSent
}
if net2.BytesRecv != 0 && net1.BytesRecv != 0 && net2.BytesRecv > net1.BytesRecv {
itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / interval / 60
itemNet.Down = net2.BytesRecv - net1.BytesRecv
}
netList = append(netList, itemNet)
break

View File

@@ -199,9 +199,10 @@ func (s SysJob) ExportData(rows []model.SysJob, fileName string) (string, error)
}
}
misfirePolicy := "放弃执行"
if row.MisfirePolicy == "1" {
switch row.MisfirePolicy {
case "1":
misfirePolicy = "立即执行"
} else if row.MisfirePolicy == "2" {
case "2":
misfirePolicy = "执行一次"
}
concurrent := "禁止"