mirror of
https://github.com/nezhahq/nezha.git
synced 2025-02-02 01:28:13 -05:00
feat: add online user count
Some checks are pending
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Waiting to run
Run Tests / tests (ubuntu) (push) Waiting to run
Run Tests / tests (windows) (push) Waiting to run
Some checks are pending
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Waiting to run
Run Tests / tests (ubuntu) (push) Waiting to run
Run Tests / tests (windows) (push) Waiting to run
This commit is contained in:
parent
3227e30b3e
commit
f212144310
@ -107,6 +107,8 @@ func serverStream(c *gin.Context) (any, error) {
|
|||||||
return nil, newWsError("%v", err)
|
return nil, newWsError("%v", err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
singleton.OnlineUsers.Add(1)
|
||||||
|
defer singleton.OnlineUsers.Add(^uint64(0))
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
stat, err := getServerStat(c, count == 0)
|
stat, err := getServerStat(c, count == 0)
|
||||||
@ -164,6 +166,7 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) {
|
|||||||
|
|
||||||
return utils.Json.Marshal(model.StreamServerData{
|
return utils.Json.Marshal(model.StreamServerData{
|
||||||
Now: time.Now().Unix() * 1000,
|
Now: time.Now().Unix() * 1000,
|
||||||
|
Online: singleton.OnlineUsers.Load(),
|
||||||
Servers: servers,
|
Servers: servers,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
26
cmd/dashboard/controller/ws_test.go
Normal file
26
cmd/dashboard/controller/ws_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWs(t *testing.T) {
|
||||||
|
onlineUsers := new(atomic.Uint64)
|
||||||
|
onlineUsers.Add(1)
|
||||||
|
if onlineUsers.Load() != 1 {
|
||||||
|
t.Error("onlineUsers.Add(1) failed")
|
||||||
|
}
|
||||||
|
onlineUsers.Add(1)
|
||||||
|
if onlineUsers.Load() != 2 {
|
||||||
|
t.Error("onlineUsers.Add(1) failed")
|
||||||
|
}
|
||||||
|
onlineUsers.Add(^uint64(0))
|
||||||
|
if onlineUsers.Load() != 1 {
|
||||||
|
t.Error("onlineUsers.Add(^uint64(0)) failed")
|
||||||
|
}
|
||||||
|
onlineUsers.Add(^uint64(0))
|
||||||
|
if onlineUsers.Load() != 0 {
|
||||||
|
t.Error("onlineUsers.Add(^uint64(0)) failed")
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,7 @@ type StreamServer struct {
|
|||||||
|
|
||||||
type StreamServerData struct {
|
type StreamServerData struct {
|
||||||
Now int64 `json:"now,omitempty"`
|
Now int64 `json:"now,omitempty"`
|
||||||
|
Online uint64 `json:"online,omitempty"`
|
||||||
Servers []StreamServer `json:"servers,omitempty"`
|
Servers []StreamServer `json:"servers,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package singleton
|
|||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"log"
|
"log"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
@ -23,6 +24,7 @@ var (
|
|||||||
Loc *time.Location
|
Loc *time.Location
|
||||||
FrontendTemplates []model.FrontendTemplate
|
FrontendTemplates []model.FrontendTemplate
|
||||||
DashboardBootTime = uint64(time.Now().Unix())
|
DashboardBootTime = uint64(time.Now().Unix())
|
||||||
|
OnlineUsers = new(atomic.Uint64)
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed frontend-templates.yaml
|
//go:embed frontend-templates.yaml
|
||||||
|
Loading…
Reference in New Issue
Block a user