From bca689b6ca813aed03bb83b1620ed90ca4eeac37 Mon Sep 17 00:00:00 2001 From: Scott Francis Date: Mon, 18 Nov 2024 14:58:16 -0500 Subject: [PATCH] Show currently playing stream on dashboard --- debug.go | 4 ++ release.go | 4 ++ server.go | 68 +++++++++++++++++++++++++------- web/static/js/script.js | 25 ++++++++++++ web/templates/base.html | 4 +- web/templates/home.html | 6 +-- web/templates/stream_counts.html | 2 - web/templates/stream_info.html | 19 +++++++++ 8 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 web/static/js/script.js delete mode 100644 web/templates/stream_counts.html create mode 100644 web/templates/stream_info.html diff --git a/debug.go b/debug.go index ef9dc2e..309609a 100644 --- a/debug.go +++ b/debug.go @@ -7,3 +7,7 @@ import "github.com/gin-gonic/gin" func SetGinMode() { gin.SetMode(gin.DebugMode) } + +func IsDebugMode() bool { + return true +} diff --git a/release.go b/release.go index 19384c0..3bd37fa 100644 --- a/release.go +++ b/release.go @@ -7,3 +7,7 @@ import "github.com/gin-gonic/gin" func SetGinMode() { gin.SetMode(gin.ReleaseMode) } + +func IsDebugMode() bool { + return false +} diff --git a/server.go b/server.go index 0ab854d..f0ba286 100644 --- a/server.go +++ b/server.go @@ -316,12 +316,6 @@ func (s *Server) streamTracker(c *gin.Context) { } } -func (s *Server) getActiveStreamCount() int { - s.lock.Lock() - defer s.lock.Unlock() - return len(s.streams) -} - func (s *Server) getActiveStreams() []*streamInfo { s.lock.Lock() defer s.lock.Unlock() @@ -368,22 +362,68 @@ func (s *Server) debug() gin.HandlerFunc { } } -func (s *Server) getStreamCountData() gin.H { - return gin.H{ - "ActiveStreams": s.getActiveStreamCount(), - "TotalStreams": atomic.LoadInt64(&s.totalStreams), +func (s *Server) headContent() template.HTML { + if IsDebugMode() { + return template.HTML(` + + + `) + } else { + return template.HTML(fmt.Sprintf(` + `, s.version)) } } func (s *Server) homePage() gin.HandlerFunc { + streamInfoData := s.getStreamInfoData() + streamInfoData["HeadContent"] = s.headContent() + if IsDebugMode() { + streamInfoData["Version"] = "debug" + } else { + streamInfoData["Version"] = s.version + } return func(c *gin.Context) { - c.HTML(http.StatusOK, "base.html", s.getStreamCountData()) + c.HTML(http.StatusOK, "base.html", streamInfoData) + } +} + +func (s *Server) getStreamInfoData() gin.H { + return gin.H{ + "ActiveStreams": s.getActiveStreams(), + "TotalStreams": atomic.LoadInt64(&s.totalStreams), + "Now": time.Now(), } } -func (s *Server) getStreamCounts() gin.HandlerFunc { +func (s *Server) getStreamInfo() gin.HandlerFunc { return func(c *gin.Context) { - c.HTML(http.StatusOK, "stream_counts.html", s.getStreamCountData()) + c.HTML(http.StatusOK, "stream_info.html", s.getStreamInfoData()) } } @@ -400,7 +440,7 @@ func (s *Server) Start(provider *Provider) chan error { s.router.GET(fmt.Sprintf("%s:channelId", channelURIPrefix), s.streamChannel()) s.router.PUT("/refresh", s.refresh()) s.router.GET("/debug", s.debug()) - s.router.GET("/stream-counts", s.getStreamCounts()) + s.router.GET("/stream-info", s.getStreamInfo()) s.router.StaticFS("/static", static.AssetFile()) s.server = &http.Server{ diff --git a/web/static/js/script.js b/web/static/js/script.js new file mode 100644 index 0000000..5d6c400 --- /dev/null +++ b/web/static/js/script.js @@ -0,0 +1,25 @@ +function updateDurations() { + const entries = document.querySelectorAll('[data-start-time]'); + const now = Math.floor(Date.now() / 1000); + + entries.forEach(entry => { + const startTime = parseInt(entry.dataset.startTime); + const durationSeconds = now - startTime; + + const hours = Math.floor(durationSeconds / 3600); + const minutes = Math.floor((durationSeconds % 3600) / 60); + const seconds = durationSeconds % 60; + + let durationStr = ''; + if (hours > 0) durationStr += `${hours}h `; + if (minutes > 0) durationStr += `${minutes}m `; + durationStr += `${seconds}s`; + + const startTimeStr = new Date(startTime * 1000).toLocaleTimeString(); + entry.title = `Started: ${startTimeStr} (Duration: ${durationStr})`; + }); +} + +// Update durations immediately and every second +updateDurations(); +setInterval(updateDurations, 1000); diff --git a/web/templates/base.html b/web/templates/base.html index 8dae9e9..dbce18c 100644 --- a/web/templates/base.html +++ b/web/templates/base.html @@ -4,8 +4,7 @@ ProxyTV - - + {{ .HeadContent }}
@@ -40,5 +39,6 @@ } }); + \ No newline at end of file diff --git a/web/templates/home.html b/web/templates/home.html index 2f71326..1e7081c 100644 --- a/web/templates/home.html +++ b/web/templates/home.html @@ -2,9 +2,9 @@

ProxyTV Dashboard

-

Server Status

-
- {{ template "stream_counts.html" . }} +

Server Status

+
+ {{ template "stream_info.html" . }}
diff --git a/web/templates/stream_counts.html b/web/templates/stream_counts.html deleted file mode 100644 index 6f375e8..0000000 --- a/web/templates/stream_counts.html +++ /dev/null @@ -1,2 +0,0 @@ -

Active Streams: {{ .ActiveStreams }}

-

Total Streams: {{ .TotalStreams }}

diff --git a/web/templates/stream_info.html b/web/templates/stream_info.html new file mode 100644 index 0000000..441d245 --- /dev/null +++ b/web/templates/stream_info.html @@ -0,0 +1,19 @@ +
+

Active Streams: {{len .ActiveStreams}} / Total Streams: {{.TotalStreams}}

+ +
+ {{range .ActiveStreams}} +
+ {{if .LogoURL}} + {{.Name}} + {{end}} +
+
{{.Name}}
+
{{.ClientIP}}
+
+
+ {{else}} +
No active streams
+ {{end}} +
+