feat: computer data uptime total
This commit is contained in:
parent
84dfe05de8
commit
c1f8cbd2f5
3 changed files with 33 additions and 2 deletions
|
|
@ -22,20 +22,24 @@ var upgrader = websocket.Upgrader{
|
|||
}
|
||||
|
||||
func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if user is authorized
|
||||
if r.Header.Get("Authorization") != os.Getenv("WEBSOCKET_PASSWORD") {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("Unauthorized"))
|
||||
return
|
||||
}
|
||||
|
||||
// Upgrade to WebSocket connection
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
slog.Error("Error when upgrading websocket connection", slog.Any("error", err))
|
||||
slog.Error("Error when upgrading WebSocket connection", slog.Any("error", err))
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
slog.Info("WebSocket connection established")
|
||||
|
||||
// Mark computer online and record the start time for uptime tracking
|
||||
service.ComputerData.Online = true
|
||||
service.ComputerData.UptimeStart = int(time.Now().Unix())
|
||||
|
||||
|
|
@ -43,7 +47,23 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
slog.Error("WebSocket connection closed by client", slog.Any("error", err))
|
||||
|
||||
// Mark computer offline
|
||||
service.ComputerData.Online = false
|
||||
|
||||
// Calculate uptime
|
||||
sessionUptime := int(time.Now().Unix()) - service.ComputerData.UptimeStart
|
||||
|
||||
totalUptimeData := storage.GlobalDataStore.Get("uptime")
|
||||
var totalUptime float64
|
||||
if totalUptimeData != nil {
|
||||
totalUptime = totalUptimeData.(float64)
|
||||
}
|
||||
|
||||
// Add to totals
|
||||
service.ComputerData.Totals.Uptime = totalUptime + float64(sessionUptime)
|
||||
storage.GlobalDataStore.Set("uptime", service.ComputerData.Totals.Uptime)
|
||||
|
||||
service.ComputerData.UptimeStart = 0
|
||||
break
|
||||
}
|
||||
|
|
@ -64,6 +84,7 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||
var keys float64
|
||||
var clicks float64
|
||||
|
||||
// Convert values (if any) to float64
|
||||
if keysData != nil {
|
||||
keys = keysData.(float64)
|
||||
}
|
||||
|
|
@ -71,6 +92,7 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||
clicks = clicksData.(float64)
|
||||
}
|
||||
|
||||
// Add counts from current message to the totals
|
||||
storage.GlobalDataStore.Set("keys", keys+float64(clientMessage.Keys))
|
||||
storage.GlobalDataStore.Set("clicks", clicks+float64(clientMessage.Clicks))
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ type ComputerData struct {
|
|||
}
|
||||
|
||||
type ComputerTotals struct {
|
||||
Uptime float64 `json:"uptime"`
|
||||
Keys float64 `json:"keys"`
|
||||
Clicks float64 `json:"clicks"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ import (
|
|||
|
||||
var ComputerData model.ComputerData = model.ComputerData{
|
||||
Online: false,
|
||||
UptimeStart: 0,
|
||||
Totals: model.ComputerTotals{
|
||||
Uptime: 0,
|
||||
Keys: 0,
|
||||
Clicks: 0,
|
||||
},
|
||||
|
|
@ -16,12 +18,17 @@ var ComputerData model.ComputerData = model.ComputerData{
|
|||
}
|
||||
|
||||
func LoadComputerStatTotals() {
|
||||
uptimeData := storage.GlobalDataStore.Get("uptime")
|
||||
keysData := storage.GlobalDataStore.Get("keys")
|
||||
clicksData := storage.GlobalDataStore.Get("clicks")
|
||||
|
||||
var uptime float64
|
||||
var keys float64
|
||||
var clicks float64
|
||||
|
||||
if uptimeData != nil {
|
||||
uptime = uptimeData.(float64)
|
||||
}
|
||||
if keysData != nil {
|
||||
keys = keysData.(float64)
|
||||
}
|
||||
|
|
@ -30,6 +37,7 @@ func LoadComputerStatTotals() {
|
|||
}
|
||||
|
||||
ComputerData.Totals = model.ComputerTotals{
|
||||
Uptime: uptime,
|
||||
Keys: keys,
|
||||
Clicks: clicks,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue