Skip to content

Commit 36c5e64

Browse files
authored
fix: Fix the monitoring data exception (#8028)
1 parent 3de37a7 commit 36c5e64

File tree

22 files changed

+131
-27
lines changed

22 files changed

+131
-27
lines changed

agent/app/dto/setting.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dto
22

33
type SettingInfo struct {
4-
SystemIP string `json:"systemIP"`
54
DockerSockPath string `json:"dockerSockPath"`
65
SystemVersion string `json:"systemVersion"`
76

agent/app/repo/monitor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewIMonitorRepo() IMonitorRepo {
2828

2929
func (u *MonitorRepo) GetBase(opts ...DBOption) ([]model.MonitorBase, error) {
3030
var data []model.MonitorBase
31-
db := global.DB
31+
db := global.MonitorDB
3232
for _, opt := range opts {
3333
db = opt(db)
3434
}
@@ -37,7 +37,7 @@ func (u *MonitorRepo) GetBase(opts ...DBOption) ([]model.MonitorBase, error) {
3737
}
3838
func (u *MonitorRepo) GetIO(opts ...DBOption) ([]model.MonitorIO, error) {
3939
var data []model.MonitorIO
40-
db := global.DB
40+
db := global.MonitorDB
4141
for _, opt := range opts {
4242
db = opt(db)
4343
}
@@ -46,7 +46,7 @@ func (u *MonitorRepo) GetIO(opts ...DBOption) ([]model.MonitorIO, error) {
4646
}
4747
func (u *MonitorRepo) GetNetwork(opts ...DBOption) ([]model.MonitorNetwork, error) {
4848
var data []model.MonitorNetwork
49-
db := global.DB
49+
db := global.MonitorDB
5050
for _, opt := range opts {
5151
db = opt(db)
5252
}

agent/app/service/monitor.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/1Panel-dev/1Panel/agent/app/repo"
1111
"github.com/1Panel-dev/1Panel/agent/buserr"
12+
"github.com/1Panel-dev/1Panel/agent/constant"
1213

1314
"github.com/1Panel-dev/1Panel/agent/app/dto"
1415
"github.com/1Panel-dev/1Panel/agent/app/model"
@@ -82,7 +83,7 @@ func (m *MonitorService) LoadMonitorData(req dto.MonitorSearch) ([]dto.MonitorDa
8283
data = append(data, itemData)
8384
}
8485
if req.Param == "all" || req.Param == "network" {
85-
bases, err := monitorRepo.GetIO(repo.WithByName(req.Info), repo.WithByCreatedAt(req.StartTime, req.EndTime))
86+
bases, err := monitorRepo.GetNetwork(repo.WithByName(req.Info), repo.WithByCreatedAt(req.StartTime, req.EndTime))
8687
if err != nil {
8788
return nil, err
8889
}
@@ -119,6 +120,33 @@ func (m *MonitorService) LoadSetting() (*dto.MonitorSetting, error) {
119120
}
120121

121122
func (m *MonitorService) UpdateSetting(key, value string) error {
123+
switch key {
124+
case "MonitorStatus":
125+
if value == constant.StatusEnable && global.MonitorCronID == 0 {
126+
interval, err := settingRepo.Get(settingRepo.WithByKey("MonitorInterval"))
127+
if err != nil {
128+
return err
129+
}
130+
if err := StartMonitor(false, interval.Value); err != nil {
131+
return err
132+
}
133+
}
134+
if value == constant.StatusDisable && global.MonitorCronID != 0 {
135+
monitorCancel()
136+
global.Cron.Remove(cron.EntryID(global.MonitorCronID))
137+
global.MonitorCronID = 0
138+
}
139+
case "MonitorInterval":
140+
status, err := settingRepo.Get(settingRepo.WithByKey("MonitorStatus"))
141+
if err != nil {
142+
return err
143+
}
144+
if status.Value == constant.StatusEnable && global.MonitorCronID != 0 {
145+
if err := StartMonitor(true, value); err != nil {
146+
return err
147+
}
148+
}
149+
}
122150
return settingRepo.Update(key, value)
123151
}
124152

agent/app/service/snapshot_create.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ func loadDbConn(snap *snapHelper, targetDir string, req dto.SnapshotCreate) erro
247247
}
248248
}
249249

250-
_ = snap.snapAgentDB.Model(&model.Setting{}).Where("key = ?", "SystemIP").Updates(map[string]interface{}{"value": ""}).Error
251250
_ = snap.snapAgentDB.Where("id = ?", snap.SnapID).Delete(&model.Snapshot{}).Error
252251

253252
return nil

agent/cron/cron.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Run() {
2828
if err := global.DB.Where("key = ?", "MonitorStatus").Find(&status).Error; err != nil {
2929
global.LOG.Errorf("load monitor status from db failed, err: %v", err)
3030
}
31-
if status.Value == "enable" {
31+
if status.Value == "Enable" {
3232
if err := global.DB.Where("key = ?", "MonitorInterval").Find(&interval).Error; err != nil {
3333
global.LOG.Errorf("load monitor interval from db failed, err: %v", err)
3434
}

agent/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func InitAgentDB() {
2525
migrations.UpdateAppTag,
2626
migrations.UpdateApp,
2727
migrations.AddOllamaModel,
28+
migrations.UpdateSettingStatus,
2829
})
2930
if err := m.Migrate(); err != nil {
3031
global.LOG.Error(err)

agent/init/migration/migrations/init.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ var InitSetting = &gormigrate.Migration{
104104
if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: global.CONF.Base.EncryptKey}).Error; err != nil {
105105
return err
106106
}
107-
if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil {
108-
return err
109-
}
110107
if err := tx.Create(&model.Setting{Key: "DockerSockPath", Value: "unix:///var/run/docker.sock"}).Error; err != nil {
111108
return err
112109
}
@@ -275,3 +272,16 @@ var AddOllamaModel = &gormigrate.Migration{
275272
return nil
276273
},
277274
}
275+
276+
var UpdateSettingStatus = &gormigrate.Migration{
277+
ID: "20250227-update-setting-status",
278+
Migrate: func(tx *gorm.DB) error {
279+
if err := tx.Model(model.Setting{}).Where("value = ?", "enable").Update("value", constant.StatusEnable).Error; err != nil {
280+
return err
281+
}
282+
if err := tx.Model(model.Setting{}).Where("value = ?", "disable").Update("value", constant.StatusDisable).Error; err != nil {
283+
return err
284+
}
285+
return nil
286+
},
287+
}

core/app/dto/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type SettingInfo struct {
1515
Theme string `json:"theme"`
1616
MenuTabs string `json:"menuTabs"`
1717
Language string `json:"language"`
18+
SystemIP string `json:"systemIP"`
1819

1920
ServerPort string `json:"serverPort"`
2021
SSL string `json:"ssl"`

core/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func Init() {
2222
migrations.RemoveLocalBackup,
2323
migrations.AddMFAInterval,
2424
migrations.UpdateXpackHideMemu,
25+
migrations.AddSystemIP,
2526
})
2627
if err := m.Migrate(); err != nil {
2728
global.LOG.Error(err)

core/init/migration/migrations/init.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,13 @@ var UpdateXpackHideMemu = &gormigrate.Migration{
318318
return nil
319319
},
320320
}
321+
322+
var AddSystemIP = &gormigrate.Migration{
323+
ID: "20250227-add-system-ip",
324+
Migrate: func(tx *gorm.DB) error {
325+
if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil {
326+
return err
327+
}
328+
return nil
329+
},
330+
}

0 commit comments

Comments
 (0)