Skip to content

Commit

Permalink
[OPT]float process (#25)
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

* [OPT][FIX][BUILDAPP]

* [OPT]Float process

* [FIX]build app

---------

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 002fc53 commit 95f8f62
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
@@ -1,5 +1,7 @@
ARG VERSION

FROM golang:1.21.0-alpine3.18 AS builder

WORKDIR /app

COPY . .
Expand Down
20 changes: 16 additions & 4 deletions main.go
Expand Up @@ -387,7 +387,12 @@ func main() {

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

return c.JSON(http.StatusOK, map[string]interface{}{
"code": 0,
Expand All @@ -406,7 +411,13 @@ func main() {

e.GET("/_api/getdevicehistory", func(c echo.Context) error {
deviceMac := c.QueryParam("devicemac")
if deviceMac == "" {
fixupfloat := c.QueryParam("fixupfloat")
if fixupfloat == "" {
fixupfloat = "false"
}
fixupfloat_bool, err := strconv.ParseBool(fixupfloat)

if deviceMac == "" || len(deviceMac) != 17 || err != nil {
return c.JSON(http.StatusOK, map[string]interface{}{"code": 1100, "msg": "参数错误"})
}
if !historyEnable {
Expand All @@ -415,7 +426,8 @@ func main() {
"msg": "历史数据未开启",
})
}
history := database.GetDeviceHistory(databasepath, deviceMac)
history := database.GetDeviceHistory(databasepath, deviceMac, fixupfloat_bool)


return c.JSON(http.StatusOK, map[string]interface{}{
"code": 0,
Expand Down
36 changes: 22 additions & 14 deletions modules/database/base.go
Expand Up @@ -152,35 +152,43 @@ func Savetodb(databasePath string, dev []config.Dev, tokens map[int]string, maxs
}
}

func GetRouterHistory(databasePath string, routernum int) []RouterHistory {
func GetRouterHistory(databasePath string, routernum int, fixupfloat bool) []RouterHistory {

db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
checkErr(err)
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)
if fixupfloat {
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 {
func GetDeviceHistory(databasePath string, deviceMac string, fixupfloat bool) []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)
if fixupfloat {
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

0 comments on commit 95f8f62

Please sign in to comment.