mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 17:22:20 +00:00
Get stats by a bad but functionnal way
This commit is contained in:
parent
6ca354f44f
commit
b2104a0cb7
@ -4,22 +4,24 @@ package web
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/markbates/pkger"
|
||||||
|
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
|
||||||
|
"gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine"
|
||||||
|
"gitlab.crans.org/nounous/ghostream/stream/webrtc"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"github.com/markbates/pkger"
|
|
||||||
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
|
|
||||||
"gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine"
|
|
||||||
"gitlab.crans.org/nounous/ghostream/stream/webrtc"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Precompile regex
|
// Precompile regex
|
||||||
validPath = regexp.MustCompile("^/[a-z0-9@_-]*$")
|
validPath = regexp.MustCompile("^/[a-z0-9@_-]*$")
|
||||||
|
|
||||||
|
connectedClients = make(map[string]map[string]int64)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handle site index and viewer pages
|
// Handle site index and viewer pages
|
||||||
@ -90,14 +92,33 @@ func staticHandler() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Retrieve stream name from URL
|
||||||
name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0]
|
name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0]
|
||||||
userCount := 0
|
userCount := 0
|
||||||
|
|
||||||
|
// Clients have a unique generated identifier per session, that expires in 40 seconds.
|
||||||
|
// Each time the client connects to this page, the identifier is renewed.
|
||||||
|
// Yeah, that's not a good way to have stats, but it works...
|
||||||
|
if connectedClients[name] == nil {
|
||||||
|
connectedClients[name] = make(map[string]int64)
|
||||||
|
}
|
||||||
|
currentTime := time.Now().Unix()
|
||||||
|
if _, ok := r.URL.Query()["uid"]; ok {
|
||||||
|
uid := r.URL.Query()["uid"][0]
|
||||||
|
connectedClients[name][uid] = currentTime
|
||||||
|
}
|
||||||
|
for uid, oldTime := range connectedClients[name] {
|
||||||
|
if currentTime-oldTime > 40 {
|
||||||
|
delete(connectedClients, uid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get requested stream
|
// Get requested stream
|
||||||
stream, err := streams.Get(name)
|
stream, err := streams.Get(name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
userCount = stream.ClientCount()
|
userCount = stream.ClientCount()
|
||||||
userCount += webrtc.GetNumberConnectedSessions(name)
|
userCount += webrtc.GetNumberConnectedSessions(name)
|
||||||
|
userCount += len(connectedClients[name])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display connected users statistics
|
// Display connected users statistics
|
||||||
|
@ -9,6 +9,7 @@ export class ViewerCounter {
|
|||||||
constructor(element, streamName) {
|
constructor(element, streamName) {
|
||||||
this.element = element;
|
this.element = element;
|
||||||
this.url = "/_stats/" + streamName;
|
this.url = "/_stats/" + streamName;
|
||||||
|
this.uid = Math.floor(1e19 * Math.random()).toString(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,7 +22,7 @@ export class ViewerCounter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
refreshViewersCounter() {
|
refreshViewersCounter() {
|
||||||
fetch(this.url)
|
fetch(this.url + "?uid=" + this.uid)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then((data) => this.element.innerText = data.ConnectedViewers)
|
.then((data) => this.element.innerText = data.ConnectedViewers)
|
||||||
.catch(console.log);
|
.catch(console.log);
|
||||||
|
Loading…
Reference in New Issue
Block a user