Browse Source
* Add support for ending the inbound stream. Closes #191 * Add a simple success response to API requests * Add viewers over time API * Move controllers to admin directorypull/216/head
8 changed files with 66 additions and 12 deletions
@ -1,4 +1,4 @@
@@ -1,4 +1,4 @@
|
||||
package controllers |
||||
package admin |
||||
|
||||
import ( |
||||
"encoding/json" |
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
package admin |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"net/http" |
||||
|
||||
"github.com/gabek/owncast/metrics" |
||||
) |
||||
|
||||
// GetViewersOverTime will return the number of viewers at points in time
|
||||
func GetViewersOverTime(w http.ResponseWriter, r *http.Request) { |
||||
viewersOverTime := metrics.Metrics.Viewers |
||||
w.Header().Set("Content-Type", "application/json") |
||||
json.NewEncoder(w).Encode(viewersOverTime) |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
package metrics |
||||
|
||||
import "time" |
||||
|
||||
type timestampedValue struct { |
||||
Time time.Time `json:"time"` |
||||
Value int `json:"value"` |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
package metrics |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/gabek/owncast/core" |
||||
) |
||||
|
||||
// How often we poll for updates
|
||||
const viewerMetricsPollingInterval = 5 * time.Minute |
||||
|
||||
func startViewerCollectionMetrics() { |
||||
collectViewerCount() |
||||
|
||||
for range time.Tick(viewerMetricsPollingInterval) { |
||||
collectViewerCount() |
||||
} |
||||
} |
||||
|
||||
func collectViewerCount() { |
||||
if len(Metrics.Viewers) > maxCollectionValues { |
||||
Metrics.Viewers = Metrics.Viewers[1:] |
||||
} |
||||
|
||||
count := core.GetStatus().ViewerCount |
||||
value := timestampedValue{ |
||||
Value: count, |
||||
Time: time.Now(), |
||||
} |
||||
Metrics.Viewers = append(Metrics.Viewers, value) |
||||
} |
Loading…
Reference in new issue