-
Notifications
You must be signed in to change notification settings - Fork 41
/
progress.go
171 lines (156 loc) · 4.63 KB
/
progress.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package api
import (
"github.com/joschahenningsen/TUM-Live/dao"
"github.com/joschahenningsen/TUM-Live/model"
"github.com/joschahenningsen/TUM-Live/tools"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
var progressBuff *progressBuffer
// progressBuffer contains progresses to be written to the database
type progressBuffer struct {
lock sync.Mutex
progresses []model.StreamProgress
interval time.Duration
}
func newProgressBuffer() *progressBuffer {
return &progressBuffer{
lock: sync.Mutex{},
progresses: []model.StreamProgress{},
interval: time.Second * 5,
}
}
// add new progress to the list to be flushed eventually
func (b *progressBuffer) add(progress model.StreamProgress) {
b.lock.Lock()
defer b.lock.Unlock()
b.progresses = append(b.progresses, progress)
}
// flush writes the collected progresses to the database
func (b *progressBuffer) flush() error {
b.lock.Lock()
defer b.lock.Unlock()
if len(b.progresses) == 0 {
return nil
}
err := dao.Progress.SaveProgresses(b.progresses)
b.progresses = []model.StreamProgress{}
return err
}
// run flushes the progress buffer every interval
func (b *progressBuffer) run() {
for {
time.Sleep(b.interval)
err := b.flush()
if err != nil {
log.WithError(err).Error("Error flushing progress buffer")
}
}
}
// configProgressRouter sets up the router and initializes a progress buffer
// that is used to minimize writes to the database.
func configProgressRouter(router *gin.Engine, daoWrapper dao.DaoWrapper) {
routes := progressRoutes{daoWrapper}
progressBuff = newProgressBuffer()
go progressBuff.run()
router.POST("/api/progressReport", routes.saveProgress)
router.POST("/api/watched", routes.markWatched)
}
// progressRoutes contains a DaoWrapper object and all route functions dangle from it.
type progressRoutes struct {
dao.DaoWrapper
}
// progressRequest corresponds the request that is sent by the video player when it reports its progress for VODs
type progressRequest struct {
StreamID uint `json:"streamID"`
Progress float64 `json:"progress"` // A fraction that represents currentTime / totalTime for a given video
// Note: To be able to save the progress, we also need the userID, but it`s already contained in the Gin context
}
// saveProgress saves progress to a buffer that is flushed at a fixed interval.
func (r progressRoutes) saveProgress(c *gin.Context) {
var request progressRequest
err := c.BindJSON(&request)
if err != nil {
log.WithError(err).Warn("Could not bind JSON.")
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "can not bind body",
Err: err,
})
return
}
foundContext, exists := c.Get("TUMLiveContext")
if !exists {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "context should exist but doesn't",
})
return
}
tumLiveContext := foundContext.(tools.TUMLiveContext)
if tumLiveContext.User == nil {
_ = c.Error(tools.RequestError{
Status: http.StatusForbidden,
CustomMessage: "not logged in",
})
return
}
progressBuff.add(model.StreamProgress{
Progress: request.Progress,
StreamID: request.StreamID,
UserID: tumLiveContext.User.ID,
})
}
// watchedRequest corresponds the request that is sent when a user marked the video as watched on the watch page.
type watchedRequest struct {
StreamID uint `json:"streamID"`
Watched bool `json:"watched"`
}
// markWatched marks a VoD as watched in the database.
func (r progressRoutes) markWatched(c *gin.Context) {
var request watchedRequest
err := c.BindJSON(&request)
if err != nil {
log.WithError(err).Error("Could not bind JSON.")
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "can not bind body",
Err: err,
})
return
}
foundContext, exists := c.Get("TUMLiveContext")
if !exists {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "context should exist but doesn't",
})
return
}
tumLiveContext := foundContext.(tools.TUMLiveContext)
if tumLiveContext.User == nil {
_ = c.Error(tools.RequestError{
Status: http.StatusForbidden,
CustomMessage: "not logged in",
})
return
}
prog := model.StreamProgress{
UserID: tumLiveContext.User.ID,
StreamID: request.StreamID,
Watched: request.Watched,
}
err = r.ProgressDao.SaveWatchedState(&prog)
if err != nil {
log.WithError(err).Error("can not mark VoD as watched.")
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not mark VoD as watched.",
Err: err,
})
return
}
}