Skip to content

Commit 6fa5bec

Browse files
author
Datanoise
committed
feat: improve dashboard accuracy with high-precision metrics and server uptime tracking
1 parent 8343be4 commit 6fa5bec

2 files changed

Lines changed: 68 additions & 32 deletions

File tree

server/server.go

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Server struct {
3636
RelayM *relay.RelayManager
3737
tmpl *template.Template
3838
httpServers []*http.Server
39+
startTime time.Time
3940
}
4041

4142
func NewServer(cfg *config.Config) *Server {
@@ -52,10 +53,11 @@ func NewServer(cfg *config.Config) *Server {
5253

5354
r := relay.NewRelay(cfg.LowLatencyMode, hm)
5455
return &Server{
55-
Config: cfg,
56-
Relay: r,
57-
RelayM: relay.NewRelayManager(r),
58-
tmpl: tmpl,
56+
Config: cfg,
57+
Relay: r,
58+
RelayM: relay.NewRelayManager(r),
59+
tmpl: tmpl,
60+
startTime: time.Now(),
5961
}
6062
}
6163

@@ -1160,23 +1162,43 @@ func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
11601162
totalDropped += st.BytesDropped
11611163
}
11621164

1163-
payload, _ := json.Marshal(map[string]interface{}{
1164-
"bytes_in": bi,
1165-
"bytes_out": bo,
1166-
"total_listeners": tl,
1167-
"total_sources": len(info),
1168-
"total_relays": tr,
1169-
"total_streamers": ts,
1170-
"streams": info,
1171-
"relays": relays,
1172-
"visible_mounts": s.Config.VisibleMounts,
1173-
"sys_ram": m.Sys / 1024 / 1024,
1174-
"heap_alloc": m.HeapAlloc / 1024 / 1024,
1175-
"stack_sys": m.StackSys / 1024 / 1024,
1176-
"num_gc": m.NumGC,
1177-
"goroutines": runtime.NumGoroutine(),
1178-
"total_dropped": totalDropped,
1179-
})
1165+
payload, _ := json.Marshal(map[string]interface{}{
1166+
1167+
"bytes_in": bi,
1168+
1169+
"bytes_out": bo,
1170+
1171+
"total_listeners": tl,
1172+
1173+
"total_sources": len(info),
1174+
1175+
"total_relays": tr,
1176+
1177+
"total_streamers": ts,
1178+
1179+
"streams": info,
1180+
1181+
"relays": relays,
1182+
1183+
"visible_mounts": s.Config.VisibleMounts,
1184+
1185+
"sys_ram": m.Sys,
1186+
1187+
"heap_alloc": m.HeapAlloc,
1188+
1189+
"stack_sys": m.StackSys,
1190+
1191+
"num_gc": m.NumGC,
1192+
1193+
"goroutines": runtime.NumGoroutine(),
1194+
1195+
"total_dropped": totalDropped,
1196+
1197+
"server_uptime": time.Since(s.startTime).Round(time.Second).String(),
1198+
1199+
})
1200+
1201+
11801202
if _, err := fmt.Fprintf(w, "data: %s\n\n", payload); err != nil {
11811203
return err
11821204
}

server/templates/admin.html

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
<div class="label" style="font-size: 0.7rem; color: var(--text-dim);">TOTAL LOSS</div>
117117
<div class="value" style="font-size: 1.5rem; color: var(--danger);"><span id="debug-loss">0.0</span> MB</div>
118118
</div>
119+
<div class="card" style="padding: 1rem; text-align: center; border-color: var(--primary);">
120+
<div class="label" style="font-size: 0.7rem; color: var(--text-dim);">SERVER UPTIME</div>
121+
<div class="value" style="font-size: 1.5rem; color: var(--primary);" id="debug-uptime">0s</div>
122+
</div>
119123
</div>
120124
</section>
121125
<div id="tab-overview" class="tab-content active">
@@ -222,9 +226,19 @@ <h2>Playback History</h2>
222226
var debugRam = document.getElementById('debug-ram'); var debugGoroutines = document.getElementById('debug-goroutines');
223227
var debugHeap = document.getElementById('debug-heap'); var debugStack = document.getElementById('debug-stack');
224228
var debugGC = document.getElementById('debug-gc'); var debugLoss = document.getElementById('debug-loss');
229+
var debugUptime = document.getElementById('debug-uptime');
225230
var isDebug = window.location.search.indexOf('debug') !== -1;
226231
if (isDebug) document.getElementById('debug-stats').style.display = 'block';
227232

233+
function formatBytes(bytes, decimals = 2) {
234+
if (!+bytes) return '0 Bytes';
235+
const k = 1024;
236+
const dm = decimals < 0 ? 0 : decimals;
237+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
238+
const i = Math.floor(Math.log(bytes) / Math.log(k));
239+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
240+
}
241+
228242
function resize() { var rect = canvas.getBoundingClientRect(); canvas.width = rect.width * 2; canvas.height = rect.height * 2; ctx.scale(2, 2); }
229243
window.addEventListener('resize', resize); resize();
230244
var historyLen = 60; var dataIn = new Array(historyLen).fill(0); var dataOut = new Array(historyLen).fill(0);
@@ -250,17 +264,17 @@ <h2>Playback History</h2>
250264
inRateEl.innerText = rIn.toFixed(1); outRateEl.innerText = rOut.toFixed(1); dataIn.shift(); dataIn.push(rIn); dataOut.shift(); dataOut.push(rOut); drawChart();
251265
gListeners.innerText = data.total_listeners; gSources.innerText = data.total_sources;
252266
gStreamers.innerText = data.total_streamers; gRelays.innerText = data.total_relays;
253-
gMbIn.innerText = (data.bytes_in / (1024 * 1024)).toFixed(1); gMbOut.innerText = (data.bytes_out / (1024 * 1024)).toFixed(1);
254-
if (debugRam) {
255-
debugRam.innerText = data.sys_ram;
256-
debugHeap.innerText = data.heap_alloc;
257-
debugStack.innerText = data.stack_sys;
258-
debugGC.innerText = data.num_gc;
259-
debugGoroutines.innerText = data.goroutines;
260-
debugLoss.innerText = (data.total_dropped / (1024 * 1024)).toFixed(2);
261-
}
262-
263-
streamsBody.innerHTML = ''; if (data.streams.length > 0) {
267+
gMbIn.innerText = (data.bytes_in / (1024 * 1024)).toFixed(2); gMbOut.innerText = (data.bytes_out / (1024 * 1024)).toFixed(2);
268+
if (debugRam) {
269+
debugRam.innerText = formatBytes(data.sys_ram);
270+
debugHeap.innerText = formatBytes(data.heap_alloc);
271+
debugStack.innerText = formatBytes(data.stack_sys);
272+
debugGC.innerText = data.num_gc;
273+
debugGoroutines.innerText = data.goroutines;
274+
debugLoss.innerText = formatBytes(data.total_dropped);
275+
debugUptime.innerText = data.server_uptime;
276+
}
277+
streamsBody.innerHTML = ''; if (data.streams.length > 0) {
264278

265279
data.streams.forEach(function(s) {
266280
var prev = streamHistory[s.mount] || { in: s.bytes_in, out: s.bytes_out }; var curIn = (s.bytes_in - prev.in) / 1024, curOut = (s.bytes_out - prev.out) / 1024; streamHistory[s.mount] = { in: s.bytes_in, out: s.bytes_out };

0 commit comments

Comments
 (0)