diff --git a/.github/workflows/buildapp-dev.yml b/.github/workflows/buildapp-dev.yml index afba75c..f80ae2b 100644 --- a/.github/workflows/buildapp-dev.yml +++ b/.github/workflows/buildapp-dev.yml @@ -78,3 +78,5 @@ jobs: platforms: linux/amd64,linux/386,linux/arm64,linux/arm/v7,linux/arm/v6 push: true tags: thun888/mirouter-ui:dev + build-args: | + VERSION=${{ github.sha }} diff --git a/.github/workflows/buildapp.yml b/.github/workflows/buildapp.yml index d04c5e6..ce1577b 100644 --- a/.github/workflows/buildapp.yml +++ b/.github/workflows/buildapp.yml @@ -131,7 +131,7 @@ jobs: curl \ -H "Authorization: token ${{ secrets.TOKEN }}" \ -H "Content-Type: $(file -b --mime-type $file)" \ - --data-binary @"$file" \ + -F "file=@$file" \ "${{ steps.create_release.outputs.upload_url }}=$(basename $file)" fi done @@ -157,26 +157,23 @@ jobs: builddocker: runs-on: ubuntu-20.04 steps: - - - name: Checkout + - name: Checkout uses: actions/checkout@v3 - - - name: Set up QEMU + - name: Set up QEMU uses: docker/setup-qemu-action@v2 - - - name: Set up Docker Buildx + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - - name: Login to Docker Hub + - name: Login to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_PASSWORD }} - - - name: Build and push + - name: Build and push uses: docker/build-push-action@v4 with: context: . platforms: linux/amd64,linux/386,linux/arm64,linux/arm/v7,linux/arm/v6 push: true tags: thun888/mirouter-ui:latest + build-args: | + VERSION=${{ github.event.inputs.version }} diff --git a/Dockerfile b/Dockerfile index 93585f2..fd2ad7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.21.0-alpine3.18 AS builder +ARG VERSION WORKDIR /app @@ -6,7 +6,7 @@ COPY . . RUN go mod download -RUN go build -ldflags "-X 'main.Version=Docker'" -o main . +RUN go build -ldflags "-X 'main.Version=$VERSION'" -o main . FROM alpine:3.18 diff --git a/main.go b/main.go index 32b7961..6de8645 100644 --- a/main.go +++ b/main.go @@ -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": "参数错误"}) @@ -396,7 +396,7 @@ func main() { "msg": "历史数据未开启", }) } - history := database.Getdata(databasepath, routernum) + history := database.GetRouterHistory(databasepath, routernum) return c.JSON(http.StatusOK, map[string]interface{}{ "code": 0, @@ -404,6 +404,24 @@ func main() { }) }) + 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 { @@ -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 { diff --git a/modules/database/base.go b/modules/database/base.go index 612dbde..c8ab99b 100644 --- a/modules/database/base.go +++ b/modules/database/base.go @@ -16,7 +16,7 @@ import ( ) // For database -type History struct { +type RouterHistory struct { gorm.Model Ip string RouterNum int @@ -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) @@ -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, @@ -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, @@ -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 } diff --git a/otherfile/images/config.png b/otherfile/images/config.png index aa2fbb8..193898f 100644 Binary files a/otherfile/images/config.png and b/otherfile/images/config.png differ diff --git a/otherfile/images/history_index.png b/otherfile/images/history_index.png index fc68f16..f9b7ad2 100644 Binary files a/otherfile/images/history_index.png and b/otherfile/images/history_index.png differ