From 2c8af031648d110148dfe672c4fc3063f1743374 Mon Sep 17 00:00:00 2001 From: James Mills Date: Wed, 25 Mar 2020 16:01:29 +1000 Subject: Refactored the way views are stored with a mgiration off the old data (to be removed) --- app/app.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'app/app.go') diff --git a/app/app.go b/app/app.go index 03937fc..71ef0d7 100644 --- a/app/app.go +++ b/app/app.go @@ -292,21 +292,33 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) { return } - views, err := a.Store.GetViews(prefix, id) + views, err := a.Store.GetViews(id) if err != nil { - err := fmt.Errorf("error retrieving views for %s %s: %w", prefix, id, err) + err := fmt.Errorf("error retrieving views for %s: %w", id, err) log.Warn(err) } playing.Views = views + playlist := a.Library.Playlist() + + // TODO: Optimize this? Bitcask has no concept of MultiGet / MGET + for _, video := range playlist { + views, err := a.Store.GetViews(video.ID) + if err != nil { + err := fmt.Errorf("error retrieving views for %s: %w", video.ID, err) + log.Warn(err) + } + video.Views = views + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") ctx := &struct { Playing *media.Video Playlist media.Playlist }{ Playing: playing, - Playlist: a.Library.Playlist(), + Playlist: playlist, } a.render("index", w, ctx) } @@ -325,8 +337,13 @@ func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) { return } - if err := a.Store.IncView(prefix, id); err != nil { - err := fmt.Errorf("error updating view for %s %s: %w", prefix, id, err) + if err := a.Store.Migrate(prefix, id); err != nil { + err := fmt.Errorf("error migrating store data: %w", err) + log.Warn(err) + } + + if err := a.Store.IncViews(id); err != nil { + err := fmt.Errorf("error updating view for %s: %w", id, err) log.Warn(err) } -- cgit v1.2.3