feat: computer data uptime

also fix last commit
This commit is contained in:
trafficlunar 2025-07-20 21:38:37 +01:00
parent 02740548de
commit 84dfe05de8
3 changed files with 13 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
@ -36,12 +37,14 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
slog.Info("WebSocket connection established") slog.Info("WebSocket connection established")
service.ComputerData.Online = true service.ComputerData.Online = true
service.ComputerData.UptimeStart = int(time.Now().Unix())
for { for {
_, message, err := conn.ReadMessage() _, message, err := conn.ReadMessage()
if err != nil { if err != nil {
slog.Error("WebSocket connection closed by client", slog.Any("error", err)) slog.Error("WebSocket connection closed by client", slog.Any("error", err))
service.ComputerData.Online = false service.ComputerData.Online = false
service.ComputerData.UptimeStart = 0
break break
} }

View file

@ -10,9 +10,10 @@ type ComputerWebSocketMessage struct {
} }
type ComputerData struct { type ComputerData struct {
Online bool `json:"online"` Online bool `json:"online"`
Totals ComputerTotals `json:"totals"` UptimeStart int `json:"uptimeStart"`
Graph []ComputerGraphData `json:"graph"` Totals ComputerTotals `json:"totals"`
Graph []ComputerGraphData `json:"graph"`
} }
type ComputerTotals struct { type ComputerTotals struct {

View file

@ -52,12 +52,17 @@ func NewRouter() {
// Prometheus // Prometheus
r.Handle("/metrics", promhttp.Handler()) r.Handle("/metrics", promhttp.Handler())
var commit = "unknown"
if os.Getenv("SOURCE_COMMIT") != "" {
commit = os.Getenv("SOURCE_COMMIT")[:7] // shorten to 7 characters
}
r.Get("/", func(w http.ResponseWriter, r *http.Request) { r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{ json.NewEncoder(w).Encode(map[string]string{
"name": "trafficlunar's api", "name": "trafficlunar's api",
"url": "https://github.com/trafficlunar/api", "url": "https://github.com/trafficlunar/api",
"commit": os.Getenv("SOURCE_COMMIT")[:7], // shorten to 7 characters "commit": commit,
}) })
}) })
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {