-
Notifications
You must be signed in to change notification settings - Fork 41
/
seek_stats.go
64 lines (53 loc) · 1.62 KB
/
seek_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package api
import (
"github.com/gin-gonic/gin"
"github.com/joschahenningsen/TUM-Live/dao"
log "github.com/sirupsen/logrus"
"net/http"
)
// progressRoutes contains a DaoWrapper object and all route functions dangle from it.
type seekStatsRoutes struct {
dao.DaoWrapper
}
// configSeekStatsRouter sets up the router and initializes a progress buffer
// that is used to minimize writes to the database.
func configSeekStatsRouter(router *gin.Engine, daoWrapper dao.DaoWrapper) {
routes := seekStatsRoutes{daoWrapper}
router.POST("/api/seekReport/:streamID", routes.reportSeek)
router.GET("/api/seekReport/:streamID", routes.getSeek)
}
type reportSeekRequest struct {
Position float64 `json:"position"`
}
// reportSeek adds entry for a user performed seek, to generate a heatmap later on
func (r seekStatsRoutes) reportSeek(c *gin.Context) {
var req reportSeekRequest
if err := c.BindJSON(&req); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
if err := r.VideoSeekDao.Add(c.Param("streamID"), req.Position); err != nil {
log.WithError(err).Error("Could not add seek hit")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
}
// getSeek get seeks for a video
func (r seekStatsRoutes) getSeek(c *gin.Context) {
chunks, err := r.VideoSeekDao.Get(c.Param("streamID"))
if err != nil {
log.WithError(err).Error("Could not get seek hits")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var values []gin.H
for _, chunk := range chunks {
values = append(values, gin.H{
"index": chunk.ChunkIndex,
"value": chunk.Hits,
})
}
c.JSON(http.StatusOK, gin.H{
"values": values,
})
}