Skip to content

Commit

Permalink
[ADD]Support Devices History Display (#23)
Browse files Browse the repository at this point in the history
* 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 <ImgBotHelp@gmail.com>

* [Opt][Incomplete]Optimize history processing

* [FIX][Build APP]Rclone

* [ADD]Support Devices History Display

---------

Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com>
Co-authored-by: ImgBotApp <ImgBotHelp@gmail.com>
  • Loading branch information
thun888 and ImgBotApp committed Mar 24, 2024
1 parent e0e2cb5 commit 9a2b735
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
29 changes: 23 additions & 6 deletions main.go
Expand Up @@ -385,7 +385,7 @@ func main() {
// })
e.GET("/_api/getconfig", getconfig)

e.GET("/_api/gethistory", func(c echo.Context) error {
e.GET("/_api/getrouterhistory", func(c echo.Context) error {
routernum, err := strconv.Atoi(c.QueryParam("routernum"))
if err != nil {
return c.JSON(http.StatusOK, map[string]interface{}{"code": 1100, "msg": "参数错误"})
Expand All @@ -396,14 +396,32 @@ func main() {
"msg": "历史数据未开启",
})
}
history := database.Getdata(databasepath, routernum)
history := database.GetRouterHistory(databasepath, routernum)

return c.JSON(http.StatusOK, map[string]interface{}{
"code": 0,
"history": history,
})
})

e.GET("/_api/getdevicehistory", func(c echo.Context) error {
deviceMac := c.QueryParam("devicemac")
if deviceMac == "" {
return c.JSON(http.StatusOK, map[string]interface{}{"code": 1100, "msg": "参数错误"})
}
if !historyEnable {
return c.JSON(http.StatusOK, map[string]interface{}{
"code": 1101,
"msg": "历史数据未开启",
})
}
history := database.GetDeviceHistory(databasepath, deviceMac)

return c.JSON(http.StatusOK, map[string]interface{}{
"code": 0,
"history": history,
})
})
e.GET("/_api/flushstatic", func(c echo.Context) error {
err := download.DownloadStatic(basedirectory, true)
if err != nil {
Expand All @@ -420,12 +438,11 @@ func main() {
})

e.GET("/_api/refresh", func(c echo.Context) error {
go func() {
gettoken(dev)
}()
gettoken(dev)
logrus.Debugln("执行完成")
return c.JSON(http.StatusOK, map[string]interface{}{
"code": 0,
"msg": "已开始刷新",
"msg": "执行完成",
})
})
e.GET("/_api/quit", func(c echo.Context) error {
Expand Down
51 changes: 36 additions & 15 deletions modules/database/base.go
Expand Up @@ -16,7 +16,7 @@ import (
)

// For database
type History struct {
type RouterHistory struct {
gorm.Model
Ip string
RouterNum int
Expand Down Expand Up @@ -59,16 +59,16 @@ type Dev struct {
// CheckDatabase checks the SQLite database file at the given path.
//
// Parameters:
// - databasepath: a string variable that holds the path to the SQLite database file.
// - databasePath: a string variable that holds the path to the SQLite database file.
//
// Returns: None.
func CheckDatabase(databasepath string) {
// databasepath is a variable that holds the path to the SQLite database file
db, err := gorm.Open(sqlite.Open(databasepath), &gorm.Config{})
func CheckDatabase(databasePath string) {
// databasePath is a variable that holds the path to the SQLite database file
db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
checkErr(err)

// Check if the history table exists, if not, create it
err = db.AutoMigrate(&History{})
err = db.AutoMigrate(&RouterHistory{})
checkErr(err)
err = db.AutoMigrate(&DevicesHistory{})
checkErr(err)
Expand All @@ -78,25 +78,25 @@ func CheckDatabase(databasepath string) {
// Savetodb saves device statistics to the database.
//
// Parameters:
// - databasepath: the path to the database.
// - databasePath: the path to the database.
// - dev: an array of device configurations.
// - tokens: a map of token IDs to strings.
// - maxsaved: the maximum number of records to delete.
func Savetodb(databasepath string, dev []config.Dev, tokens map[int]string, maxsaved int) {
db, err := gorm.Open(sqlite.Open(databasepath), &gorm.Config{})
func Savetodb(databasePath string, dev []config.Dev, tokens map[int]string, maxsaved int) {
db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
checkErr(err)
for i, d := range dev {
ip := d.IP
routerNum := i
cpu, cpu_tp, mem, upSpeed, downSpeed, upTotal, downTotal, deviceNum, devs := getRouterStats(i, tokens, ip)
var count int64
db.Model(&History{}).Where("router_num = ?", routerNum).Count(&count)
db.Model(&RouterHistory{}).Where("router_num = ?", routerNum).Count(&count)
if count >= int64(maxsaved) {
logrus.Debug("删除历史数据")
db.Exec("DELETE FROM histories WHERE router_num = ? AND created_at = (SELECT MIN(created_at) FROM histories WHERE router_num = ? );", routerNum, routerNum)

}
db.Create(&History{
db.Create(&RouterHistory{
Ip: ip,
RouterNum: routerNum,
Cpu: cpu,
Expand Down Expand Up @@ -136,7 +136,7 @@ func Savetodb(databasepath string, dev []config.Dev, tokens map[int]string, maxs

}
}
db.Create(&History{
db.Create(&RouterHistory{
Ip: ip,
RouterNum: routerNum,
Cpu: cpu,
Expand All @@ -152,14 +152,35 @@ func Savetodb(databasepath string, dev []config.Dev, tokens map[int]string, maxs
}
}

func Getdata(databasepath string, routernum int) []History {
db, err := gorm.Open(sqlite.Open(databasepath), &gorm.Config{})
func GetRouterHistory(databasePath string, routernum int) []RouterHistory {
db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
checkErr(err)
var history []History
var history []RouterHistory
db.Where("router_num = ?", routernum).Find(&history)
// 处理浮点数精度问题
for i := range history {
history[i].Cpu = round(history[i].Cpu, .5, 2)
history[i].Mem = round(history[i].Mem, .5, 2)
history[i].UpSpeed = round(history[i].UpSpeed, .5, 2)
history[i].DownSpeed = round(history[i].DownSpeed, .5, 2)
history[i].UpTotal = round(history[i].UpTotal, .5, 2)
history[i].DownTotal = round(history[i].DownTotal, .5, 2)

}
return history
}

func GetDeviceHistory(databasePath string, deviceMac string) []DevicesHistory {
db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
checkErr(err)
var history []DevicesHistory
db.Where("mac = ?", deviceMac).Find(&history)
// 处理浮点数精度问题
for i := range history {
history[i].UpSpeed = round(history[i].UpSpeed, .5, 2)
history[i].DownSpeed = round(history[i].DownSpeed, .5, 2)
history[i].UpTotal = round(history[i].UpTotal, .5, 2)
history[i].DownTotal = round(history[i].DownTotal, .5, 2)
}
return history
}
Expand Down
Binary file modified otherfile/images/config.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified otherfile/images/history_index.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9a2b735

Please sign in to comment.