fix error and speed up the widget sample speed

This commit is contained in:
chiu 2020-03-15 12:06:49 +08:00
parent fbbf911193
commit 3d46f147f0
1 changed files with 3 additions and 20 deletions

View File

@ -77,28 +77,11 @@ class GalleriesController < ApplicationController
end
def widget
tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags
albums = Album.filter_by_widget_categories.filter_by_tags(tags)
album_ids = Album.filter_by_widget_categories.filter_by_tags(tags).pluck(:id)
params = OrbitHelper.params
counts = OrbitHelper.widget_data_count
images = []
total_images = 0
if !albums.blank?
albums.each do |album|
total_images = total_images + album.album_images.count
end
counts = counts > total_images ? total_images : counts
abc = AlbumImage.count
counts = counts > abc ? abc : counts
while images.count < counts
albums.each do |album|
img = album.album_images.sample
if !images.include?(img) && img != nil
images << img
end
end
end
end
pipeline = [{'$match' => {album_id:{"$in"=>album_ids}}},{'$sort'=>{'_id'=>-1}},{ '$limit' => counts *5 }]
images = AlbumImage.collection.aggregate(pipeline).sample(counts)
images = images.collect do |a|
alt_text = (a.description.nil? || a.description == "" ? "gallery image" : a.description)
{