From e84fa8f8c4d10ce297b1262b66a0896ac7b53f90 Mon Sep 17 00:00:00 2001 From: thun888 <2238342947@qq.com> Date: Thu, 4 Apr 2024 22:47:57 +0800 Subject: [PATCH] v1.3.3 (#29) * fix * [ImgBot] Optimize images *Total -- 435.32kb -> 276.76kb (36.42%) /otherfile/images/config.png -- 20.77kb -> 11.67kb (43.81%) /otherfile/images/history_index.png -- 414.56kb -> 265.09kb (36.05%) Signed-off-by: ImgBotApp * [Opt][Incomplete]Optimize history processing * [FIX][Build APP]Rclone * [ADD]Support Devices History Display * [OPT][FIX][BUILDAPP] * [OPT]Float process * [FIX]build app * fix Upload Release Assets * fix Upload Release Assets * [FIX] device history * FIX * [Fix]Fixes high memory usage --------- Signed-off-by: ImgBotApp Co-authored-by: ImgBotApp --- main.go | 11 +++++++++- modules/database/base.go | 45 ++++++++++++++++++++++++++++++++++------ modules/download/base.go | 18 +++++++++++----- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index dd795f6..c3b54bc 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ import ( "main/modules/netdata" "main/modules/tp" "net/http" + + // _ "net/http/pprof" "os" "os/exec" "os/signal" @@ -70,6 +72,9 @@ func init() { routerNames = make(map[int]string) hardwares = make(map[int]string) routerunits = make(map[int]bool) + // go func() { + // logrus.Println(http.ListenAndServe(":6060", nil)) + // }() } func GetCpuPercent() float64 { percent, _ := cpu.Percent(time.Second, false) @@ -471,13 +476,17 @@ func main() { // var contentRewrite = middleware.Rewrite(map[string]string{"/*": "/static/$1"}) // e.GET("/*", contentHandler, contentRewrite) - if tiny == false { + if !tiny { directory := "static" if basedirectory != "" { directory = filepath.Join(basedirectory, "static") } logrus.Debug("静态资源目录为:" + directory) e.Static("/", directory) + } else if tiny { + e.GET("/*", func(c echo.Context) error { + return c.JSON(http.StatusNotFound, map[string]interface{}{"code": 404, "msg": "已开启tiny模式"}) + }) } gettoken(dev) diff --git a/modules/database/base.go b/modules/database/base.go index 2d6c1f6..3e178ef 100644 --- a/modules/database/base.go +++ b/modules/database/base.go @@ -73,6 +73,11 @@ func CheckDatabase(databasePath string) { err = db.AutoMigrate(&DevicesHistory{}) checkErr(err) // Perform CRUD operations on the history table using db.Create, db.First, db.Update, db.Delete methods + defer func() { + sqlDB, err := db.DB() + checkErr(err) + sqlDB.Close() + }() } // Savetodb saves device statistics to the database. @@ -85,10 +90,22 @@ func CheckDatabase(databasePath string) { func Savetodb(databasePath string, dev []config.Dev, tokens map[int]string, maxsaved int) { db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{}) checkErr(err) + var ( + cpu float64 + cpu_tp int + mem float64 + upSpeed float64 + downSpeed float64 + upTotal float64 + downTotal float64 + deviceNum int + devs []interface{} + mac string + ) for i, d := range dev { ip := d.IP routerNum := i - cpu, cpu_tp, mem, upSpeed, downSpeed, upTotal, downTotal, deviceNum, devs := getRouterStats(i, tokens, ip) + cpu, cpu_tp, mem, upSpeed, downSpeed, upTotal, downTotal, deviceNum, devs = getRouterStats(i, tokens, ip) var count int64 db.Model(&RouterHistory{}).Where("router_num = ?", routerNum).Count(&count) if count >= int64(maxsaved) { @@ -108,6 +125,7 @@ func Savetodb(databasePath string, dev []config.Dev, tokens map[int]string, maxs DownTotal: downTotal, DeviceNum: deviceNum, }) + for _, dev := range devs { devMap := dev.(map[string]interface{}) @@ -117,11 +135,11 @@ func Savetodb(databasePath string, dev []config.Dev, tokens map[int]string, maxs var info DeviceInfo err = json.Unmarshal(data, &info) checkErr(err) - mac := info.Mac - upSpeed := float64(info.UpSpeed) - downSpeed := float64(info.DownSpeed) - upTotal := float64(info.Upload) - downTotal := float64(info.Download) + mac = info.Mac + upSpeed = float64(info.UpSpeed) + downSpeed = float64(info.DownSpeed) + upTotal = float64(info.Upload) + downTotal = float64(info.Download) db.Create(&DevicesHistory{ Mac: mac, UpSpeed: upSpeed, @@ -150,6 +168,11 @@ func Savetodb(databasePath string, dev []config.Dev, tokens map[int]string, maxs }) } + defer func() { + sqlDB, err := db.DB() + checkErr(err) + sqlDB.Close() + }() } func GetRouterHistory(databasePath string, routernum int, fixupfloat bool) []RouterHistory { @@ -171,6 +194,11 @@ func GetRouterHistory(databasePath string, routernum int, fixupfloat bool) []Rou } } + defer func() { + sqlDB, err := db.DB() + checkErr(err) + sqlDB.Close() + }() return history } @@ -190,6 +218,11 @@ func GetDeviceHistory(databasePath string, deviceMac string, fixupfloat bool) [] } } + defer func() { + sqlDB, err := db.DB() + checkErr(err) + sqlDB.Close() + }() return history } diff --git a/modules/download/base.go b/modules/download/base.go index 5df3ad2..7983382 100644 --- a/modules/download/base.go +++ b/modules/download/base.go @@ -48,7 +48,7 @@ func DownloadStatic(basedirectory string, force bool) error { checkErr(err) logrus.Info("静态资源已存在,版本号为" + string(forntendVersion)) - resp, err := http.Get("https://mrui-api.hzchu.top/checkupdate") + resp, err := http.Get("https://mrui-api.hzchu.top/v2/api/checkupdate") if err != nil { logrus.Info("无法获取更新信息,跳过检查") @@ -60,14 +60,21 @@ func DownloadStatic(basedirectory string, force bool) error { checkErr(err) var result map[string]interface{} json.Unmarshal(body, &result) + front := result["front"].(map[string]interface{}) + frontversion := front["version"] + frontchangelog := front["changelog"] - if result["backversion"] != string(Version) { - message := fmt.Sprintf("后端程序发现新版本(%v),请及时更新", result["backversion"]) + backend := result["backend"].(map[string]interface{}) + backendversion := backend["version"] + backendchangelog := front["changelog"] + + if backendversion != string(Version) { + message := fmt.Sprintf("后端程序发现新版本(%v),请及时更新。更新日志:%v", backendversion, backendchangelog) logrus.Info(message) } - if result["frontversion"] != string(forntendVersion) { - message := fmt.Sprintf("前端文件发现新版本(%v),正在重新下载", result["frontversion"]) + if frontversion != string(forntendVersion) { + message := fmt.Sprintf("前端文件发现新版本(%v),在前端页面中进行更新。更新日志:%v", frontversion, frontchangelog) logrus.Info(message) os.RemoveAll(directory) downloadfile(directory) @@ -120,6 +127,7 @@ func unzip(src, dest string) error { return err } _, err = io.Copy(outFile, rc) + checkErr(err) outFile.Close() }