2020-10-02 10:06:14 +03:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-03-07 06:43:57 +03:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-10-02 10:06:14 +03:00
|
|
|
|
2022-03-07 04:31:47 +03:00
|
|
|
"github.com/owncast/owncast/controllers"
|
|
|
|
"github.com/owncast/owncast/core"
|
|
|
|
"github.com/owncast/owncast/core/user"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/metrics"
|
2022-03-07 04:31:47 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2020-11-15 05:39:53 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-02 10:06:14 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// GetViewersOverTime will return the number of viewers at points in time.
|
2020-10-02 10:06:14 +03:00
|
|
|
func GetViewersOverTime(w http.ResponseWriter, r *http.Request) {
|
2022-03-07 06:43:57 +03: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 10:06:14 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-03-07 06:43:57 +03:00
|
|
|
err = json.NewEncoder(w).Encode(viewersOverTime)
|
2020-11-15 05:39:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-10-02 10:06:14 +03:00
|
|
|
}
|
2022-03-07 04:31:47 +03:00
|
|
|
|
|
|
|
// GetActiveViewers returns currently connected clients.
|
|
|
|
func GetActiveViewers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c := core.GetActiveViewers()
|
2022-10-13 02:37:26 +03:00
|
|
|
viewers := make([]models.Viewer, 0, len(c))
|
2022-03-07 04:31:47 +03: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)
|
|
|
|
}
|