Skip to content

Commit

Permalink
fix: Refresh heatmap thumbnail cache when a heatmap is updated (#1395)
Browse files Browse the repository at this point in the history
Co-authored-by: Alderamin <[email protected]>
  • Loading branch information
Alderamin49ly and Alderamin authored Oct 17, 2023
1 parent fbe8488 commit fd0c499
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
9 changes: 9 additions & 0 deletions pkg/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,15 @@ func Migrate() {
return tx.AutoMigrate(Scene{}).Error
},
},
{
ID: "0069-file-refresh-heatmap-cache",
Migrate: func(tx *gorm.DB) error {
type File struct {
RefreshHeatmapCache bool `json:"refresh_heatmap_cache" gorm:"default:false"`
}
return tx.AutoMigrate(File{}).Error
},
},

// ===============================================================================================
// Put DB Schema migrations above this line and migrations that rely on the updated schema below
Expand Down
7 changes: 4 additions & 3 deletions pkg/models/model_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type File struct {
VideoDuration float64 `json:"duration" xbvrbackup:"duration"`
VideoProjection string `json:"projection" xbvrbackup:"projection"`

HasHeatmap bool `json:"has_heatmap" xbvrbackup:"-"`
IsSelectedScript bool `json:"is_selected_script" xbvrbackup:"is_selected_script"`
IsExported bool `json:"is_exported" xbvrbackup:"-"`
HasHeatmap bool `json:"has_heatmap" xbvrbackup:"-"`
IsSelectedScript bool `json:"is_selected_script" xbvrbackup:"is_selected_script"`
IsExported bool `json:"is_exported" xbvrbackup:"-"`
RefreshHeatmapCache bool `json:"refresh_heatmap_cache" xbvrbackup:"-"`
}

func (f *File) GetPath() string {
Expand Down
54 changes: 36 additions & 18 deletions pkg/server/heatmapproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ func NewHeatmapThumbnailProxy(imageproxy *imageproxy.Proxy, cache imageproxy.Cac
return proxy
}

func getScriptFileIds(urlpart string) ([]uint, error) {
func getScriptFiles(urlpart string) ([]models.File, error) {
sceneId, err := strconv.Atoi(urlpart)
ids := make([]uint, 0)
files := make([]models.File, 0)
if err != nil {
return ids, err
return files, err
}

var scene models.Scene
err = scene.GetIfExistByPK(uint(sceneId))
if err != nil {
return ids, err
return files, err
}

scriptfiles, err := scene.GetScriptFilesSorted(config.Config.Interfaces.Players.ScriptSortSeq)
if err != nil || len(scriptfiles) < 1 {
return ids, fmt.Errorf("scene %d has no script files", sceneId)
return files, fmt.Errorf("scene %d has no script files", sceneId)
}

for i := range scriptfiles {
ids = append(ids, scriptfiles[i].ID)
files = append(files, scriptfiles[i])
}
return ids, nil
return files, nil
}

func getHeatmapImageForScene(fileId uint) (image.Image, error) {
Expand Down Expand Up @@ -149,27 +149,39 @@ func (p *HeatmapThumbnailProxy) ServeHTTP(w http.ResponseWriter, r *http.Request
}

imageURL := parts[2]
fileIds, err := getScriptFileIds(parts[1])
files, err := getScriptFiles(parts[1])
if err != nil {
p.serveImageproxyResponse(w, r, imageURL)
return
}

cacheKey := fmt.Sprintf("%d:%s", fileIds[0], imageURL)
cachedContent, ok := p.Cache.Get(cacheKey)
if ok {
w.Header().Add("Content-Type", "image/jpeg")
w.Header().Add("Content-Length", fmt.Sprint(len(cachedContent)))
if _, err := io.Copy(w, bytes.NewReader(cachedContent)); err != nil {
log.Printf("Failed to send out response: %v", err)
loadFromCache := true

for i := range files {
if files[i].RefreshHeatmapCache {
loadFromCache = false
break
}
}

cacheKey := fmt.Sprintf("%d:%s", files[0].ID, imageURL)

if loadFromCache {
cachedContent, ok := p.Cache.Get(cacheKey)
if ok {
w.Header().Add("Content-Type", "image/jpeg")
w.Header().Add("Content-Length", fmt.Sprint(len(cachedContent)))
if _, err := io.Copy(w, bytes.NewReader(cachedContent)); err != nil {
log.Printf("Failed to send out response: %v", err)
}
return
}
return
}

heatmapImages := make([]image.Image, 0)

for i := range fileIds {
heatmapImage, err := getHeatmapImageForScene(fileIds[i])
for i := range files {
heatmapImage, err := getHeatmapImageForScene(files[i].ID)
if err == nil {
heatmapImages = append(heatmapImages, heatmapImage)
if len(heatmapImages) == maximumHeatmaps {
Expand All @@ -183,6 +195,12 @@ func (p *HeatmapThumbnailProxy) ServeHTTP(w http.ResponseWriter, r *http.Request
return
}

for i := range files {
file := files[i]
file.RefreshHeatmapCache = false
file.Save()
}

heatmapsHeight := len(heatmapImages) * (heatmapHeight + heatmapMargin)
proxyURL := fmt.Sprintf("/%dx%d,jpeg/%s", thumbnailWidth, thumbnailHeight-heatmapsHeight, imageURL)
r2 := new(http.Request)
Expand Down
1 change: 1 addition & 0 deletions pkg/tasks/heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func GenerateHeatmaps(tlog *logrus.Entry) {
)
if err == nil {
file.HasHeatmap = true
file.RefreshHeatmapCache = true
file.Save()
} else {
log.Warn(err)
Expand Down

0 comments on commit fd0c499

Please sign in to comment.