2024-07-02 07:44:37 +03:00
|
|
|
package handlers
|
2022-03-17 03:34:44 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/metrics"
|
2022-03-25 09:06:47 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2024-07-03 06:37:58 +03:00
|
|
|
"github.com/owncast/owncast/webserver/handlers/generated"
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils "github.com/owncast/owncast/webserver/utils"
|
2022-03-17 03:34:44 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReportPlaybackMetrics will accept playback metrics from a client and save
|
|
|
|
// them for future video health reporting.
|
|
|
|
func ReportPlaybackMetrics(w http.ResponseWriter, r *http.Request) {
|
2024-07-03 06:37:58 +03:00
|
|
|
if r.Method != http.MethodPost {
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, r.Method+" not supported")
|
2022-03-17 03:34:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
2024-07-03 06:37:58 +03:00
|
|
|
var request generated.ReportPlaybackMetricsJSONRequestBody
|
2022-03-17 03:34:44 +03:00
|
|
|
if err := decoder.Decode(&request); err != nil {
|
|
|
|
log.Errorln("error decoding playback metrics payload:", err)
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, err.Error())
|
2022-03-17 03:34:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-25 09:06:47 +03:00
|
|
|
clientID := utils.GenerateClientIDFromRequest(r)
|
|
|
|
|
2024-07-03 06:37:58 +03:00
|
|
|
metrics.RegisterPlaybackErrorCount(clientID, *request.Errors)
|
2024-10-26 22:22:19 +03:00
|
|
|
if request.Bandwidth != nil && *request.Bandwidth != 0.0 {
|
2024-07-03 06:37:58 +03:00
|
|
|
metrics.RegisterPlayerBandwidth(clientID, *request.Bandwidth)
|
2022-03-30 03:33:32 +03:00
|
|
|
}
|
|
|
|
|
2024-10-26 22:22:19 +03:00
|
|
|
if request.Latency != nil && *request.Latency != 0.0 {
|
2024-07-03 06:37:58 +03:00
|
|
|
metrics.RegisterPlayerLatency(clientID, *request.Latency)
|
2022-03-30 03:33:32 +03:00
|
|
|
}
|
|
|
|
|
2024-10-26 22:22:19 +03:00
|
|
|
if request.DownloadDuration != nil && *request.DownloadDuration != 0.0 {
|
2024-07-03 06:37:58 +03:00
|
|
|
metrics.RegisterPlayerSegmentDownloadDuration(clientID, *request.DownloadDuration)
|
2022-03-30 03:33:32 +03:00
|
|
|
}
|
|
|
|
|
2024-07-03 06:37:58 +03:00
|
|
|
metrics.RegisterQualityVariantChangesCount(clientID, *request.QualityVariantChanges)
|
2022-03-17 03:34:44 +03:00
|
|
|
}
|