Skip to content

Commit

Permalink
load stream progresses in one query (#1136)
Browse files Browse the repository at this point in the history
* load stream progresses in one query

* fixes and test fixes
  • Loading branch information
joschahenningsen authored Aug 23, 2023
1 parent 8519c75 commit c57bef9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
34 changes: 17 additions & 17 deletions api/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"errors"
"github.com/getsentry/sentry-go"
"github.com/joschahenningsen/TUM-Live/dao"
"github.com/joschahenningsen/TUM-Live/model"
"github.com/joschahenningsen/TUM-Live/tools"
Expand Down Expand Up @@ -204,25 +203,26 @@ func (r progressRoutes) getProgressBatch(c *gin.Context) {
ids = append(ids, uint(id))
}

streamProgresses := make([]model.StreamProgress, len(ids))
progressResults := make([]model.StreamProgress, len(ids))
streamProgresses, err := r.LoadProgress(tumLiveContext.User.ID, ids)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not load progress",
Err: err,
})
return
}
for i, id := range ids {
p, err := r.LoadProgress(tumLiveContext.User.ID, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
streamProgresses[i] = model.StreamProgress{StreamID: id}
} else {
sentry.CaptureException(err)
_ = c.Error(tools.RequestError{
Err: err,
Status: http.StatusInternalServerError,
CustomMessage: "can't retrieve streamProgresses for user",
})
return
progressResults[i] = model.StreamProgress{StreamID: id}
for _, progress := range streamProgresses {
if progress.StreamID == id {
progressResults[i].Progress = progress.Progress
progressResults[i].Watched = progress.Watched
break
}
} else {
streamProgresses[i] = p
}
}

c.JSON(http.StatusOK, streamProgresses)
c.JSON(http.StatusOK, progressResults)
}
8 changes: 4 additions & 4 deletions api/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestUserProgress(t *testing.T) {
progressMock.
EXPECT().
LoadProgress(testutils.TUMLiveContextStudent.User.ID, gomock.Any()).
Return(model.StreamProgress{}, errors.New(""))
Return([]model.StreamProgress{}, errors.New(""))
return progressMock
}(),
}
Expand All @@ -175,7 +175,7 @@ func TestUserProgress(t *testing.T) {
progressMock.
EXPECT().
LoadProgress(testutils.TUMLiveContextStudent.User.ID, gomock.Any()).
Return(model.StreamProgress{}, gorm.ErrRecordNotFound)
Return([]model.StreamProgress{}, gorm.ErrRecordNotFound)
return progressMock
}(),
}
Expand All @@ -194,7 +194,7 @@ func TestUserProgress(t *testing.T) {
progressMock.
EXPECT().
LoadProgress(testutils.TUMLiveContextStudent.User.ID, gomock.Any()).
Return(model.StreamProgress{StreamID: 16, Watched: true, Progress: 0.5}, nil).
Return([]model.StreamProgress{{StreamID: 16, Watched: true, Progress: 0.5}}, nil).
AnyTimes()
return progressMock
}(),
Expand All @@ -214,7 +214,7 @@ func TestUserProgress(t *testing.T) {
progressMock.
EXPECT().
LoadProgress(testutils.TUMLiveContextStudent.User.ID, gomock.Any()).
Return(model.StreamProgress{StreamID: 16, Watched: true, Progress: 0.5}, nil)
Return([]model.StreamProgress{{StreamID: 16, Watched: true, Progress: 0.5}}, nil)
return progressMock
}(),
}
Expand Down
6 changes: 3 additions & 3 deletions dao/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var Progress = NewProgressDao()
type ProgressDao interface {
SaveProgresses(progresses []model.StreamProgress) error
GetProgressesForUser(userID uint) ([]model.StreamProgress, error)
LoadProgress(userID uint, streamID uint) (streamProgress model.StreamProgress, err error)
LoadProgress(userID uint, streamIDs []uint) (streamProgress []model.StreamProgress, err error)
SaveWatchedState(progress *model.StreamProgress) error
}

Expand Down Expand Up @@ -47,7 +47,7 @@ func (d progressDao) SaveWatchedState(progress *model.StreamProgress) error {
}

// LoadProgress retrieves the current StreamProgress from the database for a given user and stream.
func (d progressDao) LoadProgress(userID uint, streamID uint) (streamProgress model.StreamProgress, err error) {
err = DB.First(&streamProgress, "user_id = ? AND stream_id = ?", userID, streamID).Error
func (d progressDao) LoadProgress(userID uint, streamIDs []uint) (streamProgress []model.StreamProgress, err error) {
err = DB.Find(&streamProgress, "user_id = ? AND stream_id IN ?", userID, streamIDs).Error
return streamProgress, err
}
10 changes: 5 additions & 5 deletions mock_dao/progress.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions web/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func (r mainRoutes) WatchPage(c *gin.Context) {
// Check for fetching progress
if tumLiveContext.User != nil && tumLiveContext.Stream.Recording {

progress, err := dao.Progress.LoadProgress(tumLiveContext.User.ID, tumLiveContext.Stream.ID)
progress, err := dao.Progress.LoadProgress(tumLiveContext.User.ID, []uint{tumLiveContext.Stream.ID})
if err != nil {
data.Progress = model.StreamProgress{Progress: 0}
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.WithError(err).Warn("Couldn't fetch progress from the database.")
}
} else {
data.Progress = progress
} else if len(progress) > 0 {
data.Progress = progress[0]
}
}
if c.Query("restart") == "1" {
Expand Down

0 comments on commit c57bef9

Please sign in to comment.