From 6991c773da5ab2c7b087e12487edacfbfaa0bbe4 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 8 Apr 2024 13:48:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=9B=91=E6=8E=A7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=8B=AC=E7=AB=8B=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=20(#4424)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/monitor.go | 6 +- backend/app/repo/setting.go | 12 ++-- backend/global/global.go | 15 ++--- backend/init/db/db.go | 36 +++++++++++- backend/init/migration/migrate.go | 1 + backend/init/migration/migrations/v_1_10.go | 65 ++++++++++++++++++++- 6 files changed, 117 insertions(+), 18 deletions(-) diff --git a/backend/app/api/v1/monitor.go b/backend/app/api/v1/monitor.go index 69929ff9b..14be057b0 100644 --- a/backend/app/api/v1/monitor.go +++ b/backend/app/api/v1/monitor.go @@ -28,7 +28,7 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) { var backdatas []dto.MonitorData if req.Param == "all" || req.Param == "cpu" || req.Param == "memory" || req.Param == "load" { var bases []model.MonitorBase - if err := global.DB. + if err := global.MonitorDB. Where("created_at > ? AND created_at < ?", req.StartTime, req.EndTime). Find(&bases).Error; err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -45,7 +45,7 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) { } if req.Param == "all" || req.Param == "io" { var bases []model.MonitorIO - if err := global.DB. + if err := global.MonitorDB. Where("created_at > ? AND created_at < ?", req.StartTime, req.EndTime). Find(&bases).Error; err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -62,7 +62,7 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) { } if req.Param == "all" || req.Param == "network" { var bases []model.MonitorNetwork - if err := global.DB. + if err := global.MonitorDB. Where("name = ? AND created_at > ? AND created_at < ?", req.Info, req.StartTime, req.EndTime). Find(&bases).Error; err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) diff --git a/backend/app/repo/setting.go b/backend/app/repo/setting.go index ea07b7adc..c5453189f 100644 --- a/backend/app/repo/setting.go +++ b/backend/app/repo/setting.go @@ -68,20 +68,20 @@ func (u *SettingRepo) Update(key, value string) error { } func (u *SettingRepo) CreateMonitorBase(model model.MonitorBase) error { - return global.DB.Create(&model).Error + return global.MonitorDB.Create(&model).Error } func (u *SettingRepo) BatchCreateMonitorIO(ioList []model.MonitorIO) error { - return global.DB.CreateInBatches(ioList, len(ioList)).Error + return global.MonitorDB.CreateInBatches(ioList, len(ioList)).Error } func (u *SettingRepo) BatchCreateMonitorNet(ioList []model.MonitorNetwork) error { - return global.DB.CreateInBatches(ioList, len(ioList)).Error + return global.MonitorDB.CreateInBatches(ioList, len(ioList)).Error } func (u *SettingRepo) DelMonitorBase(timeForDelete time.Time) error { - return global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorBase{}).Error + return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorBase{}).Error } func (u *SettingRepo) DelMonitorIO(timeForDelete time.Time) error { - return global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorIO{}).Error + return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorIO{}).Error } func (u *SettingRepo) DelMonitorNet(timeForDelete time.Time) error { - return global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error + return global.MonitorDB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error } diff --git a/backend/global/global.go b/backend/global/global.go index b41231af3..964f40f27 100644 --- a/backend/global/global.go +++ b/backend/global/global.go @@ -13,13 +13,14 @@ import ( ) var ( - DB *gorm.DB - LOG *logrus.Logger - CONF configs.ServerConfig - VALID *validator.Validate - SESSION *psession.PSession - CACHE *badger_db.Cache - Viper *viper.Viper + DB *gorm.DB + MonitorDB *gorm.DB + LOG *logrus.Logger + CONF configs.ServerConfig + VALID *validator.Validate + SESSION *psession.PSession + CACHE *badger_db.Cache + Viper *viper.Viper Cron *cron.Cron MonitorCronID cron.EntryID diff --git a/backend/init/db/db.go b/backend/init/db/db.go index 534168d3c..76df4755e 100644 --- a/backend/init/db/db.go +++ b/backend/init/db/db.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path" "time" "github.com/1Panel-dev/1Panel/backend/global" @@ -34,6 +35,7 @@ func Init() { Colorful: false, }, ) + initMonitorDB(newLogger) db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{ DisableForeignKeyConstraintWhenMigrating: true, @@ -45,7 +47,7 @@ func Init() { _ = db.Exec("PRAGMA journal_mode = WAL;") sqlDB, dbError := db.DB() if dbError != nil { - panic(err) + panic(dbError) } sqlDB.SetConnMaxIdleTime(10) sqlDB.SetMaxOpenConns(100) @@ -54,3 +56,35 @@ func Init() { global.DB = db global.LOG.Info("init db successfully") } + +func initMonitorDB(newLogger logger.Interface) { + if _, err := os.Stat(global.CONF.System.DbPath); err != nil { + if err := os.MkdirAll(global.CONF.System.DbPath, os.ModePerm); err != nil { + panic(fmt.Errorf("init db dir failed, err: %v", err)) + } + } + fullPath := path.Join(global.CONF.System.DbPath, "monitor.db") + if _, err := os.Stat(fullPath); err != nil { + if _, err := os.Create(fullPath); err != nil { + panic(fmt.Errorf("init db file failed, err: %v", err)) + } + } + + db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{ + DisableForeignKeyConstraintWhenMigrating: true, + Logger: newLogger, + }) + if err != nil { + panic(err) + } + sqlDB, dbError := db.DB() + if dbError != nil { + panic(dbError) + } + sqlDB.SetConnMaxIdleTime(10) + sqlDB.SetMaxOpenConns(100) + sqlDB.SetConnMaxLifetime(time.Hour) + + global.MonitorDB = db + global.LOG.Info("init monitor db successfully") +} diff --git a/backend/init/migration/migrate.go b/backend/init/migration/migrate.go index 198ac74b5..e9f1e5fcf 100644 --- a/backend/init/migration/migrate.go +++ b/backend/init/migration/migrate.go @@ -77,6 +77,7 @@ func Init() { migrations.AddDatabaseIsDelete, migrations.AddXpackHideMenu, migrations.AddCronjobCommand, + migrations.NewMonitorDB, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/backend/init/migration/migrations/v_1_10.go b/backend/init/migration/migrations/v_1_10.go index 5f2fa98f4..1ddec233c 100644 --- a/backend/init/migration/migrations/v_1_10.go +++ b/backend/init/migration/migrations/v_1_10.go @@ -2,6 +2,7 @@ package migrations import ( "github.com/1Panel-dev/1Panel/backend/app/model" + "github.com/1Panel-dev/1Panel/backend/global" "github.com/go-gormigrate/gormigrate/v2" "gorm.io/gorm" ) @@ -44,4 +45,66 @@ var AddCronjobCommand = &gormigrate.Migration{ } return nil }, -} \ No newline at end of file +} + +var NewMonitorDB = &gormigrate.Migration{ + ID: "20240408-new-monitor-db", + Migrate: func(tx *gorm.DB) error { + var ( + bases []model.MonitorBase + ios []model.MonitorIO + networks []model.MonitorNetwork + ) + if err := tx.Find(&bases).Error; err != nil { + return err + } + if err := tx.Find(&ios).Error; err != nil { + return err + } + if err := tx.Find(&networks).Error; err != nil { + return err + } + if err := global.MonitorDB.AutoMigrate(&model.MonitorBase{}, &model.MonitorIO{}, &model.MonitorNetwork{}); err != nil { + return err + } + + _ = global.MonitorDB.Exec("DELETE FROM monitor_bases").Error + _ = global.MonitorDB.Exec("DELETE FROM monitor_ios").Error + _ = global.MonitorDB.Exec("DELETE FROM monitor_networks").Error + + for i := 0; i <= len(bases)/200; i++ { + var itemData []model.MonitorBase + if 200*(i+1) <= len(bases) { + itemData = bases[200*i : 200*(i+1)] + } else { + itemData = bases[200*i:] + } + if err := global.MonitorDB.Create(&itemData).Error; err != nil { + return err + } + } + for i := 0; i <= len(ios)/200; i++ { + var itemData []model.MonitorIO + if 200*(i+1) <= len(ios) { + itemData = ios[200*i : 200*(i+1)] + } else { + itemData = ios[200*i:] + } + if err := global.MonitorDB.Create(&itemData).Error; err != nil { + return err + } + } + for i := 0; i <= len(networks)/200; i++ { + var itemData []model.MonitorNetwork + if 200*(i+1) <= len(networks) { + itemData = networks[200*i : 200*(i+1)] + } else { + itemData = networks[200*i:] + } + if err := global.MonitorDB.Create(&itemData).Error; err != nil { + return err + } + } + return nil + }, +}