accelerate export speed

This commit is contained in:
邱博亞 2022-08-31 13:54:58 +08:00
parent 12d35e0645
commit edec4b9a3b
1 changed files with 33 additions and 15 deletions

View File

@ -171,10 +171,22 @@ wb.add_worksheet(name: "EventNewsModule") do |sheet|
{"$match" => {"taggable_id"=>{"$in"=>@event_news.pluck(:id)}}}, {"$match" => {"taggable_id"=>{"$in"=>@event_news.pluck(:id)}}},
{"$group" => {"_id"=>"$taggable_id","children"=>{"$push"=>"$tag_id"}}} {"$group" => {"_id"=>"$taggable_id","children"=>{"$push"=>"$tag_id"}}}
]).map{|v| [v['_id'],v['children']]}.to_h ]).map{|v| [v['_id'],v['children']]}.to_h
event_carousel_image_map = EventCarouselImage.collection.aggregate([
{"$sort" => {"created_at" => 1}},
{"$group" => {"_id"=>"$event_news_id","children"=>{"$push"=>"$$ROOT"}}}
]).map{|v| [v['_id'],v['children']]}.to_h
event_news_files_map = EventNewsFile.collection.aggregate([
{"$sort" => {"created_at" => 1}},
{"$group" => {"_id"=>"$event_news_id","children"=>{"$push"=>"$$ROOT"}}}
]).map{|v| [v['_id'],v['children']]}.to_h
event_news_links_map = EventNewsLink.collection.aggregate([
{"$sort" => {"created_at" => 1}},
{"$group" => {"_id"=>"$event_news_id","children"=>{"$push"=>"$$ROOT"}}}
]).map{|v| [v['_id'],v['children']]}.to_h
@event_news.each do |anns| @event_news.each do |anns|
row = [] row = []
row << categories_ids_map[anns.category_id] row << categories_ids_map[anns.category_id]
t = Array(tag_anns_map[anns.id]).map do |tag_id| t = tag_anns_map[anns.id].to_a.map do |tag_id|
tag_ids_map[tag_id] tag_ids_map[tag_id]
end end
row << t.join(",") row << t.join(",")
@ -200,38 +212,44 @@ wb.add_worksheet(name: "EventNewsModule") do |sheet|
row << anns.notes_translations["en"] row << anns.notes_translations["en"]
row << anns.notes_translations["zh_tw"] row << anns.notes_translations["zh_tw"]
links = anns.event_news_links.asc(:created_at) links = event_news_links_map[anns.id].to_a
t = links.collect{|l|l.url} t = links.collect{|l|l['url']}
row << t.join(";") row << t.join(";")
t = links.collect{|l|l.title_translations["en"]} t = links.collect{|l|l['title']["en"] rescue ''}
row << t.join(";") row << t.join(";")
t = links.collect{|l|l.title_translations["zh_tw"]} t = links.collect{|l|l['title']["zh_tw"] rescue ''}
row << t.join(";") row << t.join(";")
files = anns.event_news_files.asc(:created_at) files = event_news_files_map[anns.id].to_a
t = files.collect{|f|("http://" + request.host_with_port + f.file.url rescue nil)} t = files.collect{|f| f['file'] ? nil : ("http://" + request.host_with_port + "/uploads/event_news_file/file/#{f['_id']}/#{f['file']}" rescue nil)}
t.delete(nil) t.delete(nil)
row << t.join(";") row << t.join(";")
t = files.collect{|l|l.description_translations["en"]} t = files.collect{|l| l['file'] ? nil : l['description']["en"] rescue ''}
t.delete(nil)
row << t.join(";") row << t.join(";")
t = files.collect{|l|l.description_translations["zh_tw"]} t = files.collect{|l| l['file'] ? nil : l['description']["zh_tw"] rescue ''}
t.delete(nil)
row << t.join(";") row << t.join(";")
t = files.collect{|l|l.title_translations["en"]} t = files.collect{|l| l['file'] ? nil : l['title']["en"] rescue ''}
t.delete(nil)
row << t.join(";") row << t.join(";")
t = files.collect{|l|l.title_translations["zh_tw"]} t = files.collect{|l| l['file'] ? nil : l['title']["zh_tw"] rescue ''}
t.delete(nil)
row << t.join(";") row << t.join(";")
row << anns.place_translations["en"] row << anns.place_translations["en"]
row << anns.place_translations["zh_tw"] row << anns.place_translations["zh_tw"]
row << (anns.event_end_date.strftime("%Y/%m/%d %H:%M") rescue "") row << (anns.event_end_date.strftime("%Y/%m/%d %H:%M") rescue "")
carousel_images = anns.event_carousel_images.asc(:created_at) carousel_images = event_carousel_image_map[anns.id].to_a
t = carousel_images.collect{|f|("http://" + request.host_with_port + f.file.url rescue nil)} t = carousel_images.collect{|f| f['file'] ? nil : ("http://" + request.host_with_port + "/uploads/event_carousel_image/file/#{f['_id']}/#{f['file']}" rescue nil)}
t.delete(nil) t.delete(nil)
row << t.join(";") row << t.join(";")
t = carousel_images.collect{|l|l.description_translations["en"]} t = carousel_images.collect{|l| l['file'] ? nil : l['description']["en"] rescue ''}
t.delete(nil)
row << t.join(";") row << t.join(";")
t = carousel_images.collect{|l|l.description_translations["zh_tw"]} t = carousel_images.collect{|l| l['file'] ? nil : l['description']["zh_tw"] rescue ''}
t.delete(nil)
row << t.join(";") row << t.join(";")
sheet.add_row row sheet.add_row row
end end