diff --git a/Dockerfile b/Dockerfile index fd2ad7d..4d77f92 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ ARG VERSION +FROM golang:1.21.0-alpine3.18 AS builder + WORKDIR /app COPY . . diff --git a/main.go b/main.go index 6de8645..83366e4 100644 --- a/main.go +++ b/main.go @@ -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 { @@ -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, @@ -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 { @@ -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, diff --git a/modules/database/base.go b/modules/database/base.go index c8ab99b..0561a66 100644 --- a/modules/database/base.go +++ b/modules/database/base.go @@ -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 }