Skip to content

Commit 63f9368

Browse files
authored
feat: GPU monitoring data supports persistence (#11051)
Refs #9496
1 parent 3f47a6e commit 63f9368

14 files changed

Lines changed: 651 additions & 339 deletions

File tree

agent/app/api/v2/monitor.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) {
3131
helper.SuccessWithData(c, data)
3232
}
3333

34+
// @Tags Monitor
35+
// @Summary Load monitor data
36+
// @Param request body dto.MonitorGPUSearch true "request"
37+
// @Success 200 {object} dto.dto.MonitorGPUData
38+
// @Security ApiKeyAuth
39+
// @Security Timestamp
40+
// @Router /hosts/monitor/gpu/search [post]
41+
func (b *BaseApi) LoadGPUMonitor(c *gin.Context) {
42+
var req dto.MonitorGPUSearch
43+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
44+
return
45+
}
46+
47+
data, err := monitorService.LoadGPUMonitorData(req)
48+
if err != nil {
49+
helper.InternalServer(c, err)
50+
return
51+
}
52+
helper.SuccessWithData(c, data)
53+
}
54+
3455
// @Tags Monitor
3556
// @Summary Clean monitor data
3657
// @Success 200

agent/app/dto/monitor.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type MonitorSearch struct {
1111
}
1212

1313
type MonitorData struct {
14-
Param string `json:"param" validate:"required,oneof=cpu memory load io network"`
14+
Param string `json:"param"`
1515
Date []time.Time `json:"date"`
1616
Value []interface{} `json:"value"`
1717
}
@@ -37,3 +37,36 @@ type MonitorSettingUpdate struct {
3737
Key string `json:"key" validate:"required,oneof=MonitorStatus MonitorStoreDays MonitorInterval DefaultNetwork DefaultIO"`
3838
Value string `json:"value"`
3939
}
40+
41+
type MonitorGPUSearch struct {
42+
ProductName string `json:"productName"`
43+
StartTime time.Time `json:"startTime"`
44+
EndTime time.Time `json:"endTime"`
45+
}
46+
type MonitorGPUData struct {
47+
ProductNames []string `json:"productNames"`
48+
Date []time.Time `json:"date"`
49+
GPUValue []float64 `json:"gpuValue"`
50+
TemperatureValue []int `json:"temperatureValue"`
51+
PowerValue []GPUPowerUsageHelper `json:"powerValue"`
52+
MemoryValue []GPUMemoryUsageHelper `json:"memoryValue"`
53+
SpeedValue []int `json:"speedValue"`
54+
}
55+
type GPUPowerUsageHelper struct {
56+
Total float64 `json:"total"`
57+
Used float64 `json:"used"`
58+
Percent float64 `json:"percent"`
59+
}
60+
type GPUMemoryUsageHelper struct {
61+
Total int `json:"total"`
62+
Used int `json:"used"`
63+
Percent float64 `json:"percent"`
64+
65+
GPUProcesses []GPUProcess `json:"gpuProcesses"`
66+
}
67+
type GPUProcess struct {
68+
Pid string `json:"pid"`
69+
Type string `json:"type"`
70+
ProcessName string `json:"processName"`
71+
UsedMemory string `json:"usedMemory"`
72+
}

agent/app/model/monitor.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ type MonitorNetwork struct {
3131
Up float64 `json:"up"`
3232
Down float64 `json:"down"`
3333
}
34+
35+
type MonitorGPU struct {
36+
BaseModel
37+
ProductName string `json:"productName"`
38+
GPUUtil float64 `json:"gpuUtil"`
39+
Temperature int `json:"temperature"`
40+
PowerDraw float64 `json:"powerDraw"`
41+
MaxPowerLimit float64 `json:"maxPowerLimit"`
42+
MemUsed int `json:"memUsed"`
43+
MemTotal int `json:"memTotal"`
44+
FanSpeed int `json:"fanSpeed"`
45+
Processes string `json:"processes"`
46+
}

agent/app/repo/monitor.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ import (
55

66
"github.com/1Panel-dev/1Panel/agent/app/model"
77
"github.com/1Panel-dev/1Panel/agent/global"
8+
"gorm.io/gorm"
89
)
910

1011
type MonitorRepo struct{}
1112

1213
type IMonitorRepo interface {
1314
GetBase(opts ...DBOption) ([]model.MonitorBase, error)
15+
GetGPU(opts ...DBOption) ([]model.MonitorGPU, error)
1416
GetIO(opts ...DBOption) ([]model.MonitorIO, error)
1517
GetNetwork(opts ...DBOption) ([]model.MonitorNetwork, error)
1618

1719
CreateMonitorBase(model model.MonitorBase) error
20+
BatchCreateMonitorGPU(list []model.MonitorGPU) error
1821
BatchCreateMonitorIO(ioList []model.MonitorIO) error
1922
BatchCreateMonitorNet(ioList []model.MonitorNetwork) error
2023
DelMonitorBase(timeForDelete time.Time) error
24+
DelMonitorGPU(timeForDelete time.Time) error
2125
DelMonitorIO(timeForDelete time.Time) error
2226
DelMonitorNet(timeForDelete time.Time) error
27+
28+
WithByProductName(name string) DBOption
2329
}
2430

2531
func NewIMonitorRepo() IMonitorRepo {
@@ -53,10 +59,22 @@ func (u *MonitorRepo) GetNetwork(opts ...DBOption) ([]model.MonitorNetwork, erro
5359
err := db.Find(&data).Error
5460
return data, err
5561
}
62+
func (u *MonitorRepo) GetGPU(opts ...DBOption) ([]model.MonitorGPU, error) {
63+
var data []model.MonitorGPU
64+
db := global.GPUMonitorDB
65+
for _, opt := range opts {
66+
db = opt(db)
67+
}
68+
err := db.Find(&data).Error
69+
return data, err
70+
}
5671

5772
func (u *MonitorRepo) CreateMonitorBase(model model.MonitorBase) error {
5873
return global.MonitorDB.Create(&model).Error
5974
}
75+
func (s *MonitorRepo) BatchCreateMonitorGPU(list []model.MonitorGPU) error {
76+
return global.GPUMonitorDB.CreateInBatches(&list, len(list)).Error
77+
}
6078
func (u *MonitorRepo) BatchCreateMonitorIO(ioList []model.MonitorIO) error {
6179
return global.MonitorDB.CreateInBatches(ioList, len(ioList)).Error
6280
}
@@ -72,3 +90,12 @@ func (u *MonitorRepo) DelMonitorIO(timeForDelete time.Time) error {
7290
func (u *MonitorRepo) DelMonitorNet(timeForDelete time.Time) error {
7391
return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error
7492
}
93+
func (s *MonitorRepo) DelMonitorGPU(timeForDelete time.Time) error {
94+
return global.GPUMonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorGPU{}).Error
95+
}
96+
97+
func (s *MonitorRepo) WithByProductName(name string) DBOption {
98+
return func(g *gorm.DB) *gorm.DB {
99+
return g.Where("product_name = ?", name)
100+
}
101+
}

agent/app/repo/setting.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package repo
22

33
import (
44
"errors"
5-
"time"
65

76
"github.com/1Panel-dev/1Panel/agent/app/model"
87
"github.com/1Panel-dev/1Panel/agent/global"
@@ -19,12 +18,6 @@ type ISettingRepo interface {
1918
Update(key, value string) error
2019
WithByKey(key string) DBOption
2120

22-
CreateMonitorBase(model model.MonitorBase) error
23-
BatchCreateMonitorIO(ioList []model.MonitorIO) error
24-
BatchCreateMonitorNet(ioList []model.MonitorNetwork) error
25-
DelMonitorBase(timeForDelete time.Time) error
26-
DelMonitorIO(timeForDelete time.Time) error
27-
DelMonitorNet(timeForDelete time.Time) error
2821
UpdateOrCreate(key, value string) error
2922

3023
GetDescription(opts ...DBOption) (model.CommonDescription, error)
@@ -85,25 +78,6 @@ func (s *SettingRepo) Update(key, value string) error {
8578
return global.DB.Model(&model.Setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error
8679
}
8780

88-
func (s *SettingRepo) CreateMonitorBase(model model.MonitorBase) error {
89-
return global.MonitorDB.Create(&model).Error
90-
}
91-
func (s *SettingRepo) BatchCreateMonitorIO(ioList []model.MonitorIO) error {
92-
return global.MonitorDB.CreateInBatches(ioList, len(ioList)).Error
93-
}
94-
func (s *SettingRepo) BatchCreateMonitorNet(ioList []model.MonitorNetwork) error {
95-
return global.MonitorDB.CreateInBatches(ioList, len(ioList)).Error
96-
}
97-
func (s *SettingRepo) DelMonitorBase(timeForDelete time.Time) error {
98-
return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorBase{}).Error
99-
}
100-
func (s *SettingRepo) DelMonitorIO(timeForDelete time.Time) error {
101-
return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorIO{}).Error
102-
}
103-
func (s *SettingRepo) DelMonitorNet(timeForDelete time.Time) error {
104-
return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error
105-
}
106-
10781
func (s *SettingRepo) UpdateOrCreate(key, value string) error {
10882
var setting model.Setting
10983
result := global.DB.Where("key = ?", key).First(&setting)

0 commit comments

Comments
 (0)