69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestUPSPreview(t *testing.T) {
|
|
if os.Getenv("DASHBOARD_UPS_PREVIEW") != "1" {
|
|
t.Skip("manual visual fixture")
|
|
}
|
|
s, a := trafficFixture(t)
|
|
_ = a
|
|
u := upsFromValues("NUT", "cyberpower", parseColonValues(cyberPowerSample))
|
|
m := dashboardMetrics{UPS: u, CPU: CPUMetrics{CPUStatic: CPUStatic{Model: "DEMO / искусственные данные"}, Temperatures: []Temperature{}}, Timestamp: time.Now()}
|
|
s.AddEntityMetrics(m)
|
|
for i := 0; i < 30; i++ {
|
|
ts := time.Now().Add(time.Duration(i-30) * time.Minute).Unix()
|
|
v := 220 + float64(i%6)
|
|
out := 225.0
|
|
power := 150 + float64(i)
|
|
runtime := 450.0
|
|
freq := 49.8
|
|
bat := 13.4
|
|
s.AddEntityHistory("ups-voltage", "Вход", ts, &v, nil)
|
|
s.AddEntityHistory("ups-voltage", "Выход", ts, &out, nil)
|
|
s.AddEntityHistory("ups-power", "Расчётная ≈", ts, &power, nil)
|
|
s.AddEntityHistory("ups-runtime", "Автономность", ts, &runtime, nil)
|
|
s.AddEntityHistory("ups-frequency", "Частота", ts, &freq, nil)
|
|
s.AddEntityHistory("ups-battery", "Батарея", ts, &bat, nil)
|
|
p := historyPointFromMetrics(m)
|
|
p.Timestamp = ts
|
|
s.AddHistory(p)
|
|
}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
writeJSON(w, map[string]any{}, nil)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(strings.Replace(dashboardHTML, "<title>", "<title>DEMO / ", 1)))
|
|
})
|
|
mux.HandleFunc("/api/events", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
b, _ := json.Marshal(m)
|
|
fmt.Fprintf(w, "data: %s\n\n", b)
|
|
w.(http.Flusher).Flush()
|
|
<-r.Context().Done()
|
|
})
|
|
mux.HandleFunc("/api/history", func(w http.ResponseWriter, r *http.Request) {
|
|
h, _ := strconv.Atoi(r.URL.Query().Get("hours"))
|
|
v, e := s.History(h)
|
|
writeJSON(w, v, e)
|
|
})
|
|
mux.HandleFunc("/api/entity-history", func(w http.ResponseWriter, r *http.Request) {
|
|
h, _ := strconv.Atoi(r.URL.Query().Get("hours"))
|
|
v, e := s.EntityHistory(r.URL.Query().Get("kind"), h)
|
|
writeJSON(w, v, e)
|
|
})
|
|
t.Fatal(http.ListenAndServe("127.0.0.1:19106", mux))
|
|
}
|