added autorereload and API
This commit is contained in:
parent
8d6076d046
commit
4ddf124174
61
main.go
61
main.go
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -32,7 +33,7 @@ import (
|
||||||
//go:embed static
|
//go:embed static
|
||||||
var staticFiles embed.FS
|
var staticFiles embed.FS
|
||||||
|
|
||||||
const version = "0.9.0"
|
const version = "0.9.1"
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Config structs
|
// Config structs
|
||||||
|
|
@ -484,6 +485,7 @@ type PageData struct {
|
||||||
ShowGauges bool
|
ShowGauges bool
|
||||||
ShowGaugeDigital bool
|
ShowGaugeDigital bool
|
||||||
ShowTrendChart bool
|
ShowTrendChart bool
|
||||||
|
UIRevision uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type NumericStats struct {
|
type NumericStats struct {
|
||||||
|
|
@ -528,6 +530,7 @@ var (
|
||||||
alarmTracker AlarmTracker
|
alarmTracker AlarmTracker
|
||||||
uiTemplate = template.Must(template.New("ui").Parse(uiHTML))
|
uiTemplate = template.Must(template.New("ui").Parse(uiHTML))
|
||||||
cachedUI []byte
|
cachedUI []byte
|
||||||
|
uiRevision uint64 = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
@ -552,7 +555,7 @@ func getConfigSnapshot() Config {
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildCachedUI(config Config) ([]byte, error) {
|
func buildCachedUI(config Config, revision uint64) ([]byte, error) {
|
||||||
data := PageData{
|
data := PageData{
|
||||||
Title: config.UI.Title,
|
Title: config.UI.Title,
|
||||||
Subtitle: config.UI.Subtitle,
|
Subtitle: config.UI.Subtitle,
|
||||||
|
|
@ -579,6 +582,7 @@ func buildCachedUI(config Config) ([]byte, error) {
|
||||||
ShowGauges: boolValue(config.Modules.ShowGauges, true),
|
ShowGauges: boolValue(config.Modules.ShowGauges, true),
|
||||||
ShowGaugeDigital: boolValue(config.Modules.ShowGaugeDigital, false),
|
ShowGaugeDigital: boolValue(config.Modules.ShowGaugeDigital, false),
|
||||||
ShowTrendChart: boolValue(config.Modules.ShowTrendChart, true),
|
ShowTrendChart: boolValue(config.Modules.ShowTrendChart, true),
|
||||||
|
UIRevision: revision,
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
@ -590,7 +594,7 @@ func buildCachedUI(config Config) ([]byte, error) {
|
||||||
|
|
||||||
func initCachedUI() {
|
func initCachedUI() {
|
||||||
config := getConfigSnapshot()
|
config := getConfigSnapshot()
|
||||||
payload, err := buildCachedUI(config)
|
payload, err := buildCachedUI(config, atomic.LoadUint64(&uiRevision))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to pre-render UI template: %v", err)
|
log.Fatalf("failed to pre-render UI template: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -655,16 +659,20 @@ func reloadConfigSafely(configPath string) {
|
||||||
updatedCfg := oldCfg
|
updatedCfg := oldCfg
|
||||||
hotReloadSectionsLocked(&updatedCfg, newCfg)
|
hotReloadSectionsLocked(&updatedCfg, newCfg)
|
||||||
|
|
||||||
payload, err := buildCachedUI(updatedCfg)
|
if len(hotSections) > 0 {
|
||||||
if err != nil {
|
nextUIRevision := atomic.LoadUint64(&uiRevision) + 1
|
||||||
log.Printf("config reload rejected: failed to rebuild UI: %v", err)
|
payload, err := buildCachedUI(updatedCfg, nextUIRevision)
|
||||||
return
|
if err != nil {
|
||||||
}
|
log.Printf("config reload rejected: failed to rebuild UI: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
cfgMu.Lock()
|
cfgMu.Lock()
|
||||||
cfg = updatedCfg
|
cfg = updatedCfg
|
||||||
cachedUI = payload
|
cachedUI = payload
|
||||||
cfgMu.Unlock()
|
atomic.StoreUint64(&uiRevision, nextUIRevision)
|
||||||
|
cfgMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
if len(hotSections) == 0 && len(restartSections) == 0 {
|
if len(hotSections) == 0 && len(restartSections) == 0 {
|
||||||
log.Printf("config reload checked: no effective changes")
|
log.Printf("config reload checked: no effective changes")
|
||||||
|
|
@ -1764,6 +1772,14 @@ func apiData(w http.ResponseWriter, r *http.Request) {
|
||||||
_ = json.NewEncoder(w).Encode(snapshotState())
|
_ = json.NewEncoder(w).Encode(snapshotState())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiUIRevision(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Header().Set("Cache-Control", "no-store")
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]uint64{
|
||||||
|
"revision": atomic.LoadUint64(&uiRevision),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func apiHistory(w http.ResponseWriter, r *http.Request) {
|
func apiHistory(w http.ResponseWriter, r *http.Request) {
|
||||||
window, label, err := parseWindow(r.URL.Query().Get("window"))
|
window, label, err := parseWindow(r.URL.Query().Get("window"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -1903,6 +1919,7 @@ func main() {
|
||||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticRoot))))
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticRoot))))
|
||||||
mux.HandleFunc("/", serveUI)
|
mux.HandleFunc("/", serveUI)
|
||||||
mux.HandleFunc("/api/data", apiData)
|
mux.HandleFunc("/api/data", apiData)
|
||||||
|
mux.HandleFunc("/api/ui-revision", apiUIRevision)
|
||||||
mux.HandleFunc("/api/history", apiHistory)
|
mux.HandleFunc("/api/history", apiHistory)
|
||||||
mux.HandleFunc("/api/trend", apiTrend)
|
mux.HandleFunc("/api/trend", apiTrend)
|
||||||
mux.HandleFunc("/api/alarms", apiAlarms)
|
mux.HandleFunc("/api/alarms", apiAlarms)
|
||||||
|
|
@ -2689,6 +2706,7 @@ const uiHTML = `<!DOCTYPE html>
|
||||||
const SHOW_GAUGES = {{if .ShowGauges}}true{{else}}false{{end}};
|
const SHOW_GAUGES = {{if .ShowGauges}}true{{else}}false{{end}};
|
||||||
const SHOW_GAUGE_DIGITAL = {{if .ShowGaugeDigital}}true{{else}}false{{end}};
|
const SHOW_GAUGE_DIGITAL = {{if .ShowGaugeDigital}}true{{else}}false{{end}};
|
||||||
const SHOW_TREND_CHART = {{if .ShowTrendChart}}true{{else}}false{{end}};
|
const SHOW_TREND_CHART = {{if .ShowTrendChart}}true{{else}}false{{end}};
|
||||||
|
const CURRENT_UI_REVISION = {{.UIRevision}};
|
||||||
|
|
||||||
const START_ANGLE = Math.PI * 0.75;
|
const START_ANGLE = Math.PI * 0.75;
|
||||||
const END_ANGLE = Math.PI * 2.25;
|
const END_ANGLE = Math.PI * 2.25;
|
||||||
|
|
@ -2698,6 +2716,7 @@ const uiHTML = `<!DOCTYPE html>
|
||||||
let currentWindow = DEFAULT_WINDOW;
|
let currentWindow = DEFAULT_WINDOW;
|
||||||
let currentTrendWindow = DEFAULT_TREND_WINDOW;
|
let currentTrendWindow = DEFAULT_TREND_WINDOW;
|
||||||
let currentTheme = 'dark';
|
let currentTheme = 'dark';
|
||||||
|
let activeUIRevision = CURRENT_UI_REVISION;
|
||||||
let historyBusy = false;
|
let historyBusy = false;
|
||||||
let trendBusy = false;
|
let trendBusy = false;
|
||||||
let alarmsBusy = false;
|
let alarmsBusy = false;
|
||||||
|
|
@ -3329,6 +3348,22 @@ const uiHTML = `<!DOCTYPE html>
|
||||||
lineChart.update('none');
|
lineChart.update('none');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function checkUIRevision() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/ui-revision', { cache: 'no-store' });
|
||||||
|
if (!res.ok) return;
|
||||||
|
const d = await res.json();
|
||||||
|
const revision = Number(d.revision) || 0;
|
||||||
|
if (revision > activeUIRevision) {
|
||||||
|
window.location.reload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
activeUIRevision = revision;
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('UI revision check error:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchLiveData() {
|
async function fetchLiveData() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/data', { cache: 'no-store' });
|
const res = await fetch('/api/data', { cache: 'no-store' });
|
||||||
|
|
@ -3683,8 +3718,10 @@ const uiHTML = `<!DOCTYPE html>
|
||||||
fetchHistory();
|
fetchHistory();
|
||||||
fetchTrend();
|
fetchTrend();
|
||||||
fetchAlarms();
|
fetchAlarms();
|
||||||
|
checkUIRevision();
|
||||||
|
|
||||||
setInterval(fetchLiveData, POLL_MS);
|
setInterval(fetchLiveData, POLL_MS);
|
||||||
|
setInterval(checkUIRevision, 1200);
|
||||||
|
|
||||||
if (SHOW_TREND_CHART) {
|
if (SHOW_TREND_CHART) {
|
||||||
setInterval(fetchHistory, Math.max(1500, POLL_MS * 3));
|
setInterval(fetchHistory, Math.max(1500, POLL_MS * 3));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue