Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OPT]float process #25

Merged
merged 15 commits into from
Mar 24, 2024
Merged
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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