Skip to content

Commit

Permalink
refactor: nodes analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Feb 4, 2025
1 parent f7e3c52 commit 34c3abb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 59 deletions.
4 changes: 2 additions & 2 deletions api/nginx_log/nginx_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nginx_log

import (
"encoding/json"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/nginx_log"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -252,8 +253,7 @@ func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan ch
}

err = ws.WriteMessage(websocket.TextMessage, []byte(line.Text))

if err != nil && websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
if helper.IsUnexpectedWebsocketError(err) {
errChan <- errors.Wrap(err, "error tailNginxLog write message")
return
}
Expand Down
7 changes: 6 additions & 1 deletion app/src/views/dashboard/components/NodeAnalyticItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defineProps<{
<div class="hardware-monitor-item longer">
<div class="mb-1">
<LineChartOutlined class="mr-1" />
<span class="load-avg-describe">1min:</span>{{ ` ${item.avg_load?.load1?.toFixed(2)}` }} ·
<span class="load-avg-describe">1min:</span>{{ item.avg_load?.load1?.toFixed(2) }} ·
<span class="load-avg-describe">5min:</span>{{ item.avg_load?.load5?.toFixed(2) }} ·
<span class="load-avg-describe">15min:</span>{{ item.avg_load?.load15?.toFixed(2) }}
</div>
Expand Down Expand Up @@ -123,6 +123,11 @@ defineProps<{
.longer {
width: 180px;
}
.load-avg-describe {
margin-right: 2px;
}
@media (max-width: 400px) {
.longer {
width: 180px;
Expand Down
15 changes: 6 additions & 9 deletions app/src/views/environment/Environment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import { message } from 'ant-design-vue'
const route = useRoute()
const curd = ref()
const loadingFromSettings = ref(false)
function loadFromSettings() {
loadingFromSettings.value = true
environment.load_from_settings().then(() => {
curd.value.get_list()
message.success($gettext('Load successfully'))
}).finally(() => {
loadingFromSettings.value = false
})
}
const selectedNodeIds = ref([])
Expand All @@ -25,14 +30,6 @@ function batchUpgrade() {
const inTrash = computed(() => {
return route.query.trash === 'true'
})
// const timer = setInterval(() => {
// curd.value.get_list()
// }, 10000)
// onUnmounted(() => {
// clearInterval(timer)
// })
</script>

<template>
Expand All @@ -48,7 +45,7 @@ const inTrash = computed(() => {
:columns="envColumns"
>
<template #beforeAdd>
<AButton size="small" type="link" @click="loadFromSettings">
<AButton size="small" type="link" :loading="loadingFromSettings" @click="loadFromSettings">
{{ $gettext('Load from settings') }}
</AButton>
</template>
Expand Down
77 changes: 37 additions & 40 deletions internal/analytic/node_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package analytic
import (
"context"
"encoding/json"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gorilla/websocket"
Expand All @@ -15,16 +16,14 @@ var stopNodeRecordChan = make(chan struct{})

func RestartRetrieveNodesStatus() {
stopNodeRecordChan <- struct{}{}
time.Sleep(10 * time.Second)
time.Sleep(5 * time.Second)
go RetrieveNodesStatus()
}

func RetrieveNodesStatus() {
NodeMap = make(TNodeMap)
errChan := make(chan error)

ctx, cancel := context.WithCancel(context.Background())

defer cancel()

env := query.Environment
Expand All @@ -36,46 +35,43 @@ func RetrieveNodesStatus() {
}

for _, v := range envs {
go nodeAnalyticLive(v, errChan, ctx)
go nodeAnalyticLive(v, ctx)
}

for {
select {
case err = <-errChan:
logger.Error(err)
case <-stopNodeRecordChan:
logger.Info("RetrieveNodesStatus exited normally")
return // will execute defer cancel()
}
}
<-stopNodeRecordChan
logger.Info("RetrieveNodesStatus exited normally")
return // will execute defer cancel()
}

func nodeAnalyticLive(env *model.Environment, errChan chan error, ctx context.Context) {
func nodeAnalyticLive(env *model.Environment, ctx context.Context) {
errChan := make(chan error)
for {
err := nodeAnalyticRecord(env, ctx)
nodeAnalyticRecord(env, errChan, ctx)

if err != nil {
// set node offline
select {
case err := <-errChan:
if NodeMap[env.ID] != nil {
mutex.Lock()
NodeMap[env.ID].Status = false
mutex.Unlock()
}
logger.Error(err)
errChan <- err
// wait 5s then reconnect
time.Sleep(5 * time.Second)
case <-ctx.Done():
return
}
}
}

func nodeAnalyticRecord(env *model.Environment, ctx context.Context) (err error) {
func nodeAnalyticRecord(env *model.Environment, errChan chan error, ctx context.Context) {
mutex.Lock()
NodeMap[env.ID] = InitNode(env)
mutex.Unlock()

u, err := env.GetWebSocketURL("/api/analytic/intro")
if err != nil {
errChan <- err
return
}

Expand All @@ -90,6 +86,7 @@ func nodeAnalyticRecord(env *model.Environment, ctx context.Context) (err error)

c, _, err := dial.Dial(u, header)
if err != nil {
errChan <- err
return
}

Expand All @@ -98,30 +95,30 @@ func nodeAnalyticRecord(env *model.Environment, ctx context.Context) (err error)
var nodeStat NodeStat

go func() {
// shutdown
<-ctx.Done()
_ = c.Close()
}()
for {
_, message, err := c.ReadMessage()
if helper.IsUnexpectedWebsocketError(err) {
errChan <- err
return
}

for {
_, message, err := c.ReadMessage()
if err != nil || websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNoStatusReceived,
websocket.CloseNormalClosure) {
return err
}
err = json.Unmarshal(message, &nodeStat)
if err != nil {
errChan <- err
return
}

err = json.Unmarshal(message, &nodeStat)
// set online
nodeStat.Status = true
nodeStat.ResponseAt = time.Now()

if err != nil {
return err
mutex.Lock()
NodeMap[env.ID].NodeStat = nodeStat
mutex.Unlock()
}
}()

// set online
nodeStat.Status = true
nodeStat.ResponseAt = time.Now()

mutex.Lock()
NodeMap[env.ID].NodeStat = nodeStat
mutex.Unlock()
}
// shutdown
<-ctx.Done()
_ = c.Close()
}
3 changes: 0 additions & 3 deletions internal/analytic/node_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,18 @@ func GetNodeStat() (data NodeStat) {
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)

loadAvg, err := load.Avg()

if err != nil {
logger.Error(err)
return
}

diskStat, err := GetDiskStat()

if err != nil {
logger.Error(err)
return
}

netIO, err := net.IOCounters(false)

if err != nil {
logger.Error(err)
return
Expand Down
6 changes: 3 additions & 3 deletions internal/pty/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pty

import (
"encoding/json"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/creack/pty"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -46,8 +47,7 @@ func NewPipeLine(conn *websocket.Conn) (p *Pipeline, err error) {
func (p *Pipeline) ReadWsAndWritePty(errorChan chan error) {
for {
msgType, payload, err := p.ws.ReadMessage()
if err != nil && websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNoStatusReceived,
websocket.CloseNormalClosure) {
if helper.IsUnexpectedWebsocketError(err) {
errorChan <- errors.Wrap(err, "Error ReadWsAndWritePty unexpected close")
return
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (p *Pipeline) ReadPtyAndWriteWs(errorChan chan error) {
}
processedOutput := validString(string(buf[:n]))
err = p.ws.WriteMessage(websocket.TextMessage, []byte(processedOutput))
if err != nil && websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
if helper.IsUnexpectedWebsocketError(err) {
errorChan <- errors.Wrap(err, "Error ReadPtyAndWriteWs websocket write")
return
}
Expand Down
2 changes: 1 addition & 1 deletion model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Model struct {
ID uint64 `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at"`
DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
}

func GenerateAllModel() []any {
Expand Down

0 comments on commit 34c3abb

Please sign in to comment.