2017-05-11 13:04:43 +00:00
|
|
|
// +build !no_ws
|
|
|
|
|
2017-09-13 15:09:13 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora WebSocket Subsystem
|
2019-04-29 06:11:26 +00:00
|
|
|
* Copyright Azareal 2017 - 2020
|
2017-09-13 15:09:13 +00:00
|
|
|
*
|
|
|
|
*/
|
2018-03-08 03:59:47 +00:00
|
|
|
package common
|
2017-05-11 13:04:43 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
import (
|
2017-06-10 07:58:15 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-09-03 04:50:31 +00:00
|
|
|
"fmt"
|
2017-06-10 07:58:15 +00:00
|
|
|
"net/http"
|
2017-09-03 04:50:31 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2019-01-21 12:27:59 +00:00
|
|
|
"strings"
|
2017-09-03 04:50:31 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2019-04-29 06:11:26 +00:00
|
|
|
p "github.com/Azareal/Gosora/common/phrases"
|
2017-07-17 10:23:42 +00:00
|
|
|
"github.com/Azareal/gopsutil/cpu"
|
|
|
|
"github.com/Azareal/gopsutil/mem"
|
2017-09-03 04:50:31 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2017-06-10 07:58:15 +00:00
|
|
|
)
|
2017-05-11 13:04:43 +00:00
|
|
|
|
2017-09-13 15:09:13 +00:00
|
|
|
// TODO: Disable WebSockets on high load? Add a Control Panel interface for disabling it?
|
2018-03-08 03:59:47 +00:00
|
|
|
var EnableWebsockets = true // Put this in caps for consistency with the other constants?
|
2017-09-13 15:09:13 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
var wsUpgrader = websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}
|
|
|
|
var errWsNouser = errors.New("This user isn't connected via WebSockets")
|
2017-05-11 13:04:43 +00:00
|
|
|
|
|
|
|
func init() {
|
2018-09-08 05:12:48 +00:00
|
|
|
adminStatsWatchers = make(map[*websocket.Conn]*WSUser)
|
2019-04-27 06:32:26 +00:00
|
|
|
topicListWatchers = make(map[*WSUser]struct{})
|
|
|
|
topicWatchers = make(map[int]map[*WSUser]struct{})
|
2018-06-24 13:49:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
//easyjson:json
|
2018-06-24 13:49:29 +00:00
|
|
|
type WsTopicList struct {
|
2019-02-10 05:52:26 +00:00
|
|
|
Topics []*WsTopicsRow
|
|
|
|
LastPage int // Not for WebSockets, but for the JSON endpoint for /topics/ to keep the paginator functional
|
2019-05-09 06:58:55 +00:00
|
|
|
LastUpdate int64
|
2018-06-24 13:49:29 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
// TODO: How should we handle errors for this?
|
2018-03-08 03:59:47 +00:00
|
|
|
// TODO: Move this out of common?
|
|
|
|
func RouteWebsockets(w http.ResponseWriter, r *http.Request, user User) RouteError {
|
2018-09-08 05:12:48 +00:00
|
|
|
// TODO: Spit out a 500 instead of nil?
|
2017-09-03 04:50:31 +00:00
|
|
|
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
2017-05-11 13:04:43 +00:00
|
|
|
if err != nil {
|
2019-02-28 07:28:17 +00:00
|
|
|
return LocalError("unable to upgrade", w, r, user)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2019-02-28 07:28:17 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
2018-09-08 05:12:48 +00:00
|
|
|
wsUser, err := WsHub.AddConn(user, conn)
|
|
|
|
if err != nil {
|
2017-10-30 09:57:08 +00:00
|
|
|
return nil
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
//conn.SetReadLimit(/* put the max request size from earlier here? */)
|
|
|
|
//conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
2018-09-08 05:12:48 +00:00
|
|
|
var currentPage string
|
2017-05-11 13:04:43 +00:00
|
|
|
for {
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if user.ID == 0 {
|
2018-03-08 03:59:47 +00:00
|
|
|
WsHub.GuestLock.Lock()
|
|
|
|
delete(WsHub.OnlineGuests, wsUser)
|
|
|
|
WsHub.GuestLock.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
} else {
|
2018-06-24 13:49:29 +00:00
|
|
|
// TODO: Make sure the admin is removed from the admin stats list in the case that an error happens
|
2018-09-08 05:12:48 +00:00
|
|
|
WsHub.RemoveConn(wsUser, conn)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-09-12 09:50:57 +00:00
|
|
|
if conn == nil {
|
|
|
|
panic("conn must not be nil")
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2019-05-09 06:58:55 +00:00
|
|
|
for _, msg := range bytes.Split(message, []byte("\r")) {
|
2018-08-22 01:32:07 +00:00
|
|
|
//StoppedServer("Profile end") // A bit of code for me to profile the software
|
2017-09-03 04:50:31 +00:00
|
|
|
if bytes.HasPrefix(msg, []byte("page ")) {
|
|
|
|
msgblocks := bytes.SplitN(msg, []byte(" "), 2)
|
2017-05-11 13:04:43 +00:00
|
|
|
if len(msgblocks) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2018-09-08 05:12:48 +00:00
|
|
|
if !bytes.Equal(msgblocks[1], []byte(currentPage)) {
|
|
|
|
wsLeavePage(wsUser, conn, currentPage)
|
|
|
|
currentPage = string(msgblocks[1])
|
|
|
|
wsPageResponses(wsUser, conn, currentPage)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2019-03-21 22:59:41 +00:00
|
|
|
} else if bytes.HasPrefix(msg, []byte("resume ")) {
|
2019-05-09 06:58:55 +00:00
|
|
|
msgblocks := bytes.SplitN(msg, []byte(" "), 3)
|
|
|
|
if len(msgblocks) < 3 {
|
2019-03-21 22:59:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-05-09 06:58:55 +00:00
|
|
|
//log.Print("resuming on " + string(msgblocks[1]) + " at " + string(msgblocks[2]))
|
|
|
|
|
|
|
|
if !bytes.Equal(msgblocks[1], []byte(currentPage)) {
|
|
|
|
wsLeavePage(wsUser, conn, currentPage) // Avoid clients abusing late resumes
|
|
|
|
currentPage = string(msgblocks[1])
|
|
|
|
// TODO: Synchronise this better?
|
|
|
|
resume, err := strconv.ParseInt(string(msgblocks[2]), 10, 64)
|
|
|
|
wsPageResponses(wsUser, conn, currentPage)
|
|
|
|
if err != nil {
|
|
|
|
wsPageResume(wsUser, conn, currentPage, resume)
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
/*if bytes.Equal(message,[]byte(`start-view`)) {
|
|
|
|
} else if bytes.Equal(message,[]byte(`end-view`)) {
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 09:50:57 +00:00
|
|
|
DebugLog("Closing connection for user " + strconv.Itoa(user.ID))
|
2017-10-30 09:57:08 +00:00
|
|
|
return nil
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
// TODO: Copied from routes package for use in wsPageResponse, find a more elegant solution.
|
|
|
|
func ParseSEOURL(urlBit string) (slug string, id int, err error) {
|
|
|
|
halves := strings.Split(urlBit, ".")
|
|
|
|
if len(halves) < 2 {
|
|
|
|
halves = append(halves, halves[0])
|
|
|
|
}
|
|
|
|
tid, err := strconv.Atoi(halves[1])
|
|
|
|
return halves[0], tid, err
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
// TODO: Use a map instead of a switch to make this more modular?
|
2018-09-08 05:12:48 +00:00
|
|
|
func wsPageResponses(wsUser *WSUser, conn *websocket.Conn, page string) {
|
|
|
|
if page == "/" {
|
|
|
|
page = Config.DefaultPath
|
2018-06-25 06:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 09:50:57 +00:00
|
|
|
DebugLog("Entering page " + page)
|
2019-01-21 12:27:59 +00:00
|
|
|
switch {
|
2018-06-24 13:49:29 +00:00
|
|
|
// Live Topic List is an experimental feature
|
|
|
|
// TODO: Optimise this to reduce the amount of contention
|
2019-01-21 12:27:59 +00:00
|
|
|
case page == "/topics/":
|
2018-06-24 13:49:29 +00:00
|
|
|
topicListMutex.Lock()
|
2019-04-27 06:32:26 +00:00
|
|
|
topicListWatchers[wsUser] = struct{}{}
|
2018-06-24 13:49:29 +00:00
|
|
|
topicListMutex.Unlock()
|
2019-01-22 12:06:55 +00:00
|
|
|
// TODO: Evict from page when permissions change? Or check user perms every-time before sending data?
|
2019-01-21 12:27:59 +00:00
|
|
|
case strings.HasPrefix(page, "/topic/"):
|
|
|
|
//fmt.Println("entering topic prefix websockets zone")
|
2019-05-03 21:49:21 +00:00
|
|
|
if wsUser.User.ID == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
_, tid, err := ParseSEOURL(page)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
topic, err := Topics.Get(tid)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !Forums.Exists(topic.ParentID) {
|
|
|
|
return
|
|
|
|
}
|
2020-02-05 02:29:35 +00:00
|
|
|
usercpy := BlankUser()
|
2019-01-22 12:06:55 +00:00
|
|
|
*usercpy = *wsUser.User
|
|
|
|
usercpy.Init()
|
2019-01-21 12:27:59 +00:00
|
|
|
|
|
|
|
/*skip, rerr := header.Hooks.VhookSkippable("ws_topic_check_pre_perms", w, r, usercpy, &fid, &header)
|
|
|
|
if skip || rerr != nil {
|
|
|
|
return
|
|
|
|
}*/
|
|
|
|
|
|
|
|
fperms, err := FPStore.Get(topic.ParentID, usercpy.Group)
|
|
|
|
if err == ErrNoRows {
|
|
|
|
fperms = BlankForumPerms()
|
|
|
|
} else if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cascadeForumPerms(fperms, usercpy)
|
|
|
|
if !usercpy.Perms.ViewTopic {
|
|
|
|
return
|
|
|
|
}
|
2019-01-22 12:06:55 +00:00
|
|
|
|
|
|
|
topicMutex.Lock()
|
|
|
|
_, ok := topicWatchers[topic.ID]
|
|
|
|
if !ok {
|
2019-04-27 06:32:26 +00:00
|
|
|
topicWatchers[topic.ID] = make(map[*WSUser]struct{})
|
2019-01-22 12:06:55 +00:00
|
|
|
}
|
2019-04-27 06:32:26 +00:00
|
|
|
topicWatchers[topic.ID][wsUser] = struct{}{}
|
2019-01-22 12:06:55 +00:00
|
|
|
topicMutex.Unlock()
|
2019-01-21 12:27:59 +00:00
|
|
|
case page == "/panel/":
|
2018-09-08 05:12:48 +00:00
|
|
|
if !wsUser.User.IsSuperMod {
|
|
|
|
return
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
// Listen for changes and inform the admins...
|
|
|
|
adminStatsMutex.Lock()
|
|
|
|
watchers := len(adminStatsWatchers)
|
2018-09-08 05:12:48 +00:00
|
|
|
adminStatsWatchers[conn] = wsUser
|
2017-09-03 04:50:31 +00:00
|
|
|
if watchers == 0 {
|
|
|
|
go adminStatsTicker()
|
|
|
|
}
|
|
|
|
adminStatsMutex.Unlock()
|
2018-09-08 05:12:48 +00:00
|
|
|
default:
|
|
|
|
return
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2018-09-12 09:50:57 +00:00
|
|
|
err := wsUser.SetPageForSocket(conn, page)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 06:58:55 +00:00
|
|
|
// TODO: Use a map instead of a switch to make this more modular?
|
|
|
|
// TODO: Implement this
|
|
|
|
func wsPageResume(wsUser *WSUser, conn *websocket.Conn, page string, resume int64) {
|
|
|
|
if page == "/" {
|
|
|
|
page = Config.DefaultPath
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// TODO: Synchronise this bit of resume with tick updating lastTopicList?
|
|
|
|
case page == "/topics/":
|
|
|
|
/*if resume >= hub.lastTick.Unix() {
|
|
|
|
conn.Write([]byte("resume tooslow"))
|
|
|
|
} else {
|
|
|
|
conn.Write([]byte("resume success"))
|
|
|
|
}*/
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
// TODO: Use a map instead of a switch to make this more modular?
|
2018-09-08 05:12:48 +00:00
|
|
|
func wsLeavePage(wsUser *WSUser, conn *websocket.Conn, page string) {
|
|
|
|
if page == "/" {
|
|
|
|
page = Config.DefaultPath
|
2019-01-22 12:06:55 +00:00
|
|
|
} else if page != "" {
|
2019-01-21 12:27:59 +00:00
|
|
|
DebugLog("Leaving page " + page)
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case page == "/topics/":
|
2018-09-08 05:12:48 +00:00
|
|
|
wsUser.FinalizePage("/topics/", func() {
|
|
|
|
topicListMutex.Lock()
|
|
|
|
delete(topicListWatchers, wsUser)
|
|
|
|
topicListMutex.Unlock()
|
|
|
|
})
|
2019-01-21 12:27:59 +00:00
|
|
|
case strings.HasPrefix(page, "/topic/"):
|
|
|
|
//fmt.Println("leaving topic prefix websockets zone")
|
2019-05-03 21:49:21 +00:00
|
|
|
if wsUser.User.ID == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-01-22 12:06:55 +00:00
|
|
|
wsUser.FinalizePage(page, func() {
|
|
|
|
_, tid, err := ParseSEOURL(page)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
topicMutex.Lock()
|
|
|
|
defer topicMutex.Unlock()
|
|
|
|
topic, ok := topicWatchers[tid]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, ok = topic[wsUser]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(topic, wsUser)
|
|
|
|
if len(topic) == 0 {
|
|
|
|
delete(topicWatchers, tid)
|
|
|
|
}
|
|
|
|
})
|
2019-01-21 12:27:59 +00:00
|
|
|
case page == "/panel/":
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.Lock()
|
2018-09-08 05:12:48 +00:00
|
|
|
delete(adminStatsWatchers, conn)
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2018-09-12 09:50:57 +00:00
|
|
|
err := wsUser.SetPageForSocket(conn, "")
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
// TODO: Abstract this
|
|
|
|
// TODO: Use odd-even sharding
|
2019-04-27 06:32:26 +00:00
|
|
|
var topicListWatchers map[*WSUser]struct{}
|
2018-06-24 13:49:29 +00:00
|
|
|
var topicListMutex sync.RWMutex
|
2019-04-27 06:32:26 +00:00
|
|
|
var topicWatchers map[int]map[*WSUser]struct{} // map[tid]watchers
|
2019-01-22 12:06:55 +00:00
|
|
|
var topicMutex sync.RWMutex
|
2018-09-08 05:12:48 +00:00
|
|
|
var adminStatsWatchers map[*websocket.Conn]*WSUser
|
2017-09-03 04:50:31 +00:00
|
|
|
var adminStatsMutex sync.RWMutex
|
|
|
|
|
|
|
|
func adminStatsTicker() {
|
2017-05-11 13:04:43 +00:00
|
|
|
time.Sleep(time.Second)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
lastUonline := -1
|
|
|
|
lastGonline := -1
|
|
|
|
lastTotonline := -1
|
|
|
|
lastCPUPerc := -1
|
2017-09-10 16:57:22 +00:00
|
|
|
var lastAvailableRAM int64 = -1
|
2018-09-08 05:12:48 +00:00
|
|
|
var noStatUpdates, noRAMUpdates bool
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
var onlineColour, onlineGuestsColour, onlineUsersColour, cpustr, cpuColour, ramstr, ramColour string
|
|
|
|
var cpuerr, ramerr error
|
|
|
|
var memres *mem.VirtualMemoryStat
|
2017-09-10 16:57:22 +00:00
|
|
|
var cpuPerc []float64
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
var totunit, uunit, gunit string
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2020-02-05 02:29:35 +00:00
|
|
|
lessThanSwitch := func(number, lowerBound, midBound int) string {
|
2017-12-30 05:47:46 +00:00
|
|
|
switch {
|
|
|
|
case number < lowerBound:
|
|
|
|
return "stat_green"
|
|
|
|
case number < midBound:
|
|
|
|
return "stat_orange"
|
|
|
|
}
|
|
|
|
return "stat_red"
|
|
|
|
}
|
2020-02-05 02:29:35 +00:00
|
|
|
greaterThanSwitch := func(number, lowerBound, midBound int) string {
|
2017-12-30 05:47:46 +00:00
|
|
|
switch {
|
|
|
|
case number > midBound:
|
|
|
|
return "stat_green"
|
|
|
|
case number > lowerBound:
|
|
|
|
return "stat_orange"
|
|
|
|
}
|
|
|
|
return "stat_red"
|
|
|
|
}
|
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
AdminStatLoop:
|
|
|
|
for {
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.RLock()
|
2017-09-10 16:57:22 +00:00
|
|
|
watchCount := len(adminStatsWatchers)
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.RUnlock()
|
2017-09-10 16:57:22 +00:00
|
|
|
if watchCount == 0 {
|
2017-05-11 13:04:43 +00:00
|
|
|
break AdminStatLoop
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
cpuPerc, cpuerr = cpu.Percent(time.Second, true)
|
2017-05-11 13:04:43 +00:00
|
|
|
memres, ramerr = mem.VirtualMemory()
|
2018-03-08 03:59:47 +00:00
|
|
|
uonline := WsHub.UserCount()
|
|
|
|
gonline := WsHub.GuestCount()
|
2017-05-11 13:04:43 +00:00
|
|
|
totonline := uonline + gonline
|
2017-12-30 05:47:46 +00:00
|
|
|
reqCount := 0
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
// It's far more likely that the CPU Usage will change than the other stats, so we'll optimise them separately...
|
2017-09-10 16:57:22 +00:00
|
|
|
noStatUpdates = (uonline == lastUonline && gonline == lastGonline && totonline == lastTotonline)
|
|
|
|
noRAMUpdates = (lastAvailableRAM == int64(memres.Available))
|
|
|
|
if int(cpuPerc[0]) == lastCPUPerc && noStatUpdates && noRAMUpdates {
|
2017-05-11 13:04:43 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
if !noStatUpdates {
|
2017-12-30 05:47:46 +00:00
|
|
|
onlineColour = greaterThanSwitch(totonline, 3, 10)
|
|
|
|
onlineGuestsColour = greaterThanSwitch(gonline, 1, 10)
|
|
|
|
onlineUsersColour = greaterThanSwitch(uonline, 1, 5)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
totonline, totunit = ConvertFriendlyUnit(totonline)
|
|
|
|
uonline, uunit = ConvertFriendlyUnit(uonline)
|
|
|
|
gonline, gunit = ConvertFriendlyUnit(gonline)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
if cpuerr != nil {
|
|
|
|
cpustr = "Unknown"
|
|
|
|
} else {
|
2017-09-10 16:57:22 +00:00
|
|
|
calcperc := int(cpuPerc[0]) / runtime.NumCPU()
|
2017-05-11 13:04:43 +00:00
|
|
|
cpustr = strconv.Itoa(calcperc)
|
2017-11-23 05:37:08 +00:00
|
|
|
switch {
|
|
|
|
case calcperc < 30:
|
2017-05-11 13:04:43 +00:00
|
|
|
cpuColour = "stat_green"
|
2017-11-23 05:37:08 +00:00
|
|
|
case calcperc < 75:
|
2017-05-11 13:04:43 +00:00
|
|
|
cpuColour = "stat_orange"
|
2017-11-23 05:37:08 +00:00
|
|
|
default:
|
2017-05-11 13:04:43 +00:00
|
|
|
cpuColour = "stat_red"
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
if !noRAMUpdates {
|
2017-05-12 13:25:12 +00:00
|
|
|
if ramerr != nil {
|
|
|
|
ramstr = "Unknown"
|
2017-05-11 13:04:43 +00:00
|
|
|
} else {
|
2018-03-08 03:59:47 +00:00
|
|
|
totalCount, totalUnit := ConvertByteUnit(float64(memres.Total))
|
|
|
|
usedCount := ConvertByteInUnit(float64(memres.Total-memres.Available), totalUnit)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
// Round totals with .9s up, it's how most people see it anyway. Floats are notoriously imprecise, so do it off 0.85
|
|
|
|
var totstr string
|
2017-09-10 16:57:22 +00:00
|
|
|
if (totalCount - float64(int(totalCount))) > 0.85 {
|
|
|
|
usedCount += 1.0 - (totalCount - float64(int(totalCount)))
|
|
|
|
totstr = strconv.Itoa(int(totalCount) + 1)
|
2017-05-12 13:25:12 +00:00
|
|
|
} else {
|
2017-09-10 16:57:22 +00:00
|
|
|
totstr = fmt.Sprintf("%.1f", totalCount)
|
2017-05-12 13:25:12 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
if usedCount > totalCount {
|
|
|
|
usedCount = totalCount
|
2017-05-12 13:25:12 +00:00
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
ramstr = fmt.Sprintf("%.1f", usedCount) + " / " + totstr + totalUnit
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
ramperc := ((memres.Total - memres.Available) * 100) / memres.Total
|
2017-12-30 05:47:46 +00:00
|
|
|
ramColour = lessThanSwitch(int(ramperc), 50, 75)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
// Acquire a write lock for now, so we can handle the delete() case below and the read one simultaneously
|
|
|
|
// TODO: Stop taking a write lock here if it isn't necessary
|
|
|
|
adminStatsMutex.Lock()
|
2018-09-08 05:12:48 +00:00
|
|
|
for conn := range adminStatsWatchers {
|
|
|
|
w, err := conn.NextWriter(websocket.TextMessage)
|
2017-05-11 13:04:43 +00:00
|
|
|
if err != nil {
|
2018-09-08 05:12:48 +00:00
|
|
|
delete(adminStatsWatchers, conn)
|
2017-05-11 13:04:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-06-24 13:49:29 +00:00
|
|
|
// TODO: Use JSON for this to make things more portable and easier to convert to MessagePack, if need be?
|
2020-02-05 02:29:35 +00:00
|
|
|
write := func(msg string) {
|
|
|
|
w.Write([]byte(msg + "\r"))
|
|
|
|
}
|
|
|
|
push := func(id, msg string) {
|
|
|
|
write("set #" + id + " <span>"+msg+"</span>")
|
|
|
|
}
|
|
|
|
pushc := func(id, classes string) {
|
|
|
|
write("set-class #" + id + " " + classes)
|
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
if !noStatUpdates {
|
2020-02-05 02:29:35 +00:00
|
|
|
push("dash-totonline",p.GetTmplPhrasef("panel_dashboard_online", totonline, totunit))
|
|
|
|
push("dash-gonline",p.GetTmplPhrasef("panel_dashboard_guests_online", gonline, gunit))
|
|
|
|
push("dash-uonline",p.GetTmplPhrasef("panel_dashboard_users_online", uonline, uunit))
|
|
|
|
push("dash-reqs",strconv.Itoa(reqCount) + " reqs / second")
|
|
|
|
pushc("dash-totonline","grid_item grid_stat " + onlineColour)
|
|
|
|
pushc("dash-gonline","grid_item grid_stat " + onlineGuestsColour)
|
|
|
|
pushc("dash-uonline","grid_item grid_stat " + onlineUsersColour)
|
|
|
|
//pushc("dash-reqs","grid_item grid_stat grid_end_group")
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2020-02-05 02:29:35 +00:00
|
|
|
push("dash-cpu",p.GetTmplPhrasef("panel_dashboard_cpu",cpustr) + "%")
|
|
|
|
pushc("dash-cpu","grid_item grid_istat " + cpuColour)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
if !noRAMUpdates {
|
2020-02-05 02:29:35 +00:00
|
|
|
push("dash-ram", p.GetTmplPhrasef("panel_dashboard_ram",ramstr))
|
|
|
|
pushc("dash-ram","grid_item grid_istat " + ramColour)
|
2017-05-12 13:25:12 +00:00
|
|
|
}
|
2017-05-11 13:04:43 +00:00
|
|
|
w.Close()
|
|
|
|
}
|
2018-06-24 13:49:29 +00:00
|
|
|
adminStatsMutex.Unlock()
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
lastUonline = uonline
|
|
|
|
lastGonline = gonline
|
|
|
|
lastTotonline = totonline
|
|
|
|
lastCPUPerc = int(cpuPerc[0])
|
|
|
|
lastAvailableRAM = int64(memres.Available)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|