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]build app #24

Merged
merged 12 commits into from Mar 24, 2024
2 changes: 2 additions & 0 deletions .github/workflows/buildapp-dev.yml
Expand Up @@ -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 }}
19 changes: 8 additions & 11 deletions .github/workflows/buildapp.yml
Expand Up @@ -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
Expand All @@ -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 }}
4 changes: 2 additions & 2 deletions Dockerfile
@@ -1,12 +1,12 @@
FROM golang:1.21.0-alpine3.18 AS builder
ARG VERSION

WORKDIR /app

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

Expand Down
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.