Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor: refactor app and api #211

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html", "toml"]
include_ext = ["go", "tpl", "tmpl", "html", "toml", "po"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "app/node_modules", "upload", "docs", "resources", .ts", ".vue", ".tsx", ".idea"]
exclude_dir = ["assets", "tmp", "vendor", "app/node_modules", "upload", "docs", "resources", ".idea"]
# Watch these directories if you specified.
include_dir = ["app/src/language"]
include_dir = []
# Exclude files.
exclude_file = []
# Exclude specific regular expressions.
Expand Down Expand Up @@ -51,3 +51,6 @@ runner = "green"
[misc]
# Delete tmp directory on exit
clean_on_exit = true

[screen]
keep_scroll = true
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ jobs:
pnpm install
working-directory: app

- name: Check frontend code style
run: |
pnpm run lint
working-directory: app

- name: Check frontend types
run: |
pnpm run typecheck
working-directory: app

- name: Build
run: |
npx browserslist@latest --update-db
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.DS_Store
.idea
database.db
tmp
node_modules
Expand Down
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/nginx-ui.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 33 additions & 101 deletions api/analytic/analytic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package analytic

import (
"fmt"
analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
Expand All @@ -17,22 +17,6 @@ import (
"github.com/gorilla/websocket"
)

type CPUStat struct {
User float64 `json:"user"`
System float64 `json:"system"`
Idle float64 `json:"idle"`
Total float64 `json:"total"`
}

type Stat struct {
Uptime uint64 `json:"uptime"`
LoadAvg *load.AvgStat `json:"loadavg"`
CPU CPUStat `json:"cpu"`
Memory analytic2.MemStat `json:"memory"`
Disk analytic2.DiskStat `json:"disk"`
Network net.IOCountersStat `json:"network"`
}

func Analytic(c *gin.Context) {
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
Expand All @@ -51,7 +35,7 @@ func Analytic(c *gin.Context) {
var stat Stat

for {
stat.Memory, err = analytic2.GetMemoryStat()
stat.Memory, err = analytic.GetMemoryStat()

if err != nil {
logger.Error(err)
Expand All @@ -76,7 +60,7 @@ func Analytic(c *gin.Context) {

stat.LoadAvg, _ = load.Avg()

stat.Disk, err = analytic2.GetDiskStat()
stat.Disk, err = analytic.GetDiskStat()

if err != nil {
logger.Error(err)
Expand All @@ -103,20 +87,24 @@ func Analytic(c *gin.Context) {
}

func GetAnalyticInit(c *gin.Context) {
cpuInfo, _ := cpu.Info()
network, _ := net.IOCounters(false)
memory, err := analytic2.GetMemoryStat()
cpuInfo, err := cpu.Info()
if err != nil {
logger.Error(err)
}

network, err := net.IOCounters(false)
if err != nil {
logger.Error(err)
return
}

diskStat, err := analytic2.GetDiskStat()
memory, err := analytic.GetMemoryStat()
if err != nil {
logger.Error(err)
}

diskStat, err := analytic.GetDiskStat()
if err != nil {
logger.Error(err)
return
}

var _net net.IOCountersStat
Expand All @@ -132,86 +120,30 @@ func GetAnalyticInit(c *gin.Context) {
hostInfo.Platform = "CentOS"
}

loadAvg, _ := load.Avg()

c.JSON(http.StatusOK, gin.H{
"host": hostInfo,
"cpu": gin.H{
"info": cpuInfo,
"user": analytic2.CpuUserRecord,
"total": analytic2.CpuTotalRecord,
},
"network": gin.H{
"init": _net,
"bytesRecv": analytic2.NetRecvRecord,
"bytesSent": analytic2.NetSentRecord,
},
"disk_io": gin.H{
"writes": analytic2.DiskWriteRecord,
"reads": analytic2.DiskReadRecord,
},
"memory": memory,
"disk": diskStat,
"loadavg": loadAvg,
})
}
loadAvg, err := load.Avg()

func GetNodeStat(c *gin.Context) {
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// upgrade http to websocket
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
logger.Error(err)
return
}

defer ws.Close()

for {
// write
err = ws.WriteJSON(analytic2.GetNodeStat())
if err != nil || websocket.IsUnexpectedCloseError(err,
websocket.CloseGoingAway,
websocket.CloseNoStatusReceived,
websocket.CloseNormalClosure) {
logger.Error(err)
break
}

time.Sleep(10 * time.Second)
}
}

func GetNodesAnalytic(c *gin.Context) {
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
c.JSON(http.StatusOK, InitResp{
Host: hostInfo,
CPU: CPURecords{
Info: cpuInfo,
User: analytic.CpuUserRecord,
Total: analytic.CpuTotalRecord,
},
}
// upgrade http to websocket
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
logger.Error(err)
return
}

defer ws.Close()

for {
// write
err = ws.WriteJSON(analytic2.NodeMap)
if err != nil || websocket.IsUnexpectedCloseError(err,
websocket.CloseGoingAway,
websocket.CloseNoStatusReceived,
websocket.CloseNormalClosure) {
logger.Error(err)
break
}

time.Sleep(10 * time.Second)
}
Network: NetworkRecords{
Init: _net,
BytesRecv: analytic.NetRecvRecord,
BytesSent: analytic.NetSentRecord,
},
DiskIO: DiskIORecords{
Writes: analytic.DiskWriteRecord,
Reads: analytic.DiskReadRecord,
},
Memory: memory,
Disk: diskStat,
LoadAvg: loadAvg,
})
}
70 changes: 70 additions & 0 deletions api/analytic/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package analytic

import (
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"net/http"
"time"
)

func GetNodeStat(c *gin.Context) {
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// upgrade http to websocket
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
logger.Error(err)
return
}

defer ws.Close()

for {
// write
err = ws.WriteJSON(analytic.GetNodeStat())
if err != nil || websocket.IsUnexpectedCloseError(err,
websocket.CloseGoingAway,
websocket.CloseNoStatusReceived,
websocket.CloseNormalClosure) {
logger.Error(err)
break
}

time.Sleep(10 * time.Second)
}
}

func GetNodesAnalytic(c *gin.Context) {
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// upgrade http to websocket
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
logger.Error(err)
return
}

defer ws.Close()

for {
// write
err = ws.WriteJSON(analytic.NodeMap)
if err != nil || websocket.IsUnexpectedCloseError(err,
websocket.CloseGoingAway,
websocket.CloseNoStatusReceived,
websocket.CloseNormalClosure) {
logger.Error(err)
break
}

time.Sleep(10 * time.Second)
}
}
Loading