2020-10-02 09:06:14 +02:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-03-07 04:43:57 +01:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-10-02 09:06:14 +02:00
|
|
|
|
2022-03-07 02:31:47 +01:00
|
|
|
"github.com/owncast/owncast/controllers"
|
|
|
|
"github.com/owncast/owncast/core"
|
|
|
|
"github.com/owncast/owncast/core/user"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/metrics"
|
2022-03-07 02:31:47 +01:00
|
|
|
"github.com/owncast/owncast/models"
|
2020-11-15 03:39:53 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-02 09:06:14 +02:00
|
|
|
)
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// GetViewersOverTime will return the number of viewers at points in time.
|
2020-10-02 09:06:14 +02:00
|
|
|
func GetViewersOverTime(w http.ResponseWriter, r *http.Request) {
|
2022-03-07 04:43:57 +01:00
|
|
|
windowStartAtStr := r.URL.Query().Get("windowStart")
|
|
|
|
windowStartAtUnix, err := strconv.Atoi(windowStartAtStr)
|
|
|
|
if err != nil {
|
|
|
|
controllers.WriteSimpleResponse(w, false, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
windowStartAt := time.Unix(int64(windowStartAtUnix), 0)
|
|
|
|
windowEnd := time.Now()
|
|
|
|
|
|
|
|
viewersOverTime := metrics.GetViewersOverTime(windowStartAt, windowEnd)
|
2020-10-02 09:06:14 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-03-07 04:43:57 +01:00
|
|
|
err = json.NewEncoder(w).Encode(viewersOverTime)
|
2020-11-15 03:39:53 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-10-02 09:06:14 +02:00
|
|
|
}
|
2022-03-07 02:31:47 +01:00
|
|
|
|
|
|
|
// GetActiveViewers returns currently connected clients.
|
|
|
|
func GetActiveViewers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c := core.GetActiveViewers()
|
2022-10-13 01:37:26 +02:00
|
|
|
viewers := make([]models.Viewer, 0, len(c))
|
2022-03-07 02:31:47 +01:00
|
|
|
for _, v := range c {
|
|
|
|
viewers = append(viewers, *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(viewers); err != nil {
|
|
|
|
controllers.InternalErrorHandler(w, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExternalGetActiveViewers returns currently connected clients.
|
|
|
|
func ExternalGetActiveViewers(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
|
|
|
|
GetConnectedChatClients(w, r)
|
|
|
|
}
|