From edec4b9a3b9f940825a1afaa43d1ef14afa12046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Wed, 31 Aug 2022 13:54:58 +0800 Subject: [PATCH] accelerate export speed --- .../admin/event_news/export_excel.xlsx.axlsx | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/app/views/admin/event_news/export_excel.xlsx.axlsx b/app/views/admin/event_news/export_excel.xlsx.axlsx index ff991de..2cdfb6a 100644 --- a/app/views/admin/event_news/export_excel.xlsx.axlsx +++ b/app/views/admin/event_news/export_excel.xlsx.axlsx @@ -171,10 +171,22 @@ wb.add_worksheet(name: "EventNewsModule") do |sheet| {"$match" => {"taggable_id"=>{"$in"=>@event_news.pluck(:id)}}}, {"$group" => {"_id"=>"$taggable_id","children"=>{"$push"=>"$tag_id"}}} ]).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| row = [] 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] end row << t.join(",") @@ -200,38 +212,44 @@ wb.add_worksheet(name: "EventNewsModule") do |sheet| row << anns.notes_translations["en"] row << anns.notes_translations["zh_tw"] - links = anns.event_news_links.asc(:created_at) - t = links.collect{|l|l.url} + links = event_news_links_map[anns.id].to_a + t = links.collect{|l|l['url']} row << t.join(";") - t = links.collect{|l|l.title_translations["en"]} + t = links.collect{|l|l['title']["en"] rescue ''} row << t.join(";") - t = links.collect{|l|l.title_translations["zh_tw"]} + t = links.collect{|l|l['title']["zh_tw"] rescue ''} row << t.join(";") - files = anns.event_news_files.asc(:created_at) - t = files.collect{|f|("http://" + request.host_with_port + f.file.url rescue nil)} + files = event_news_files_map[anns.id].to_a + 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) 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(";") - 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(";") - 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(";") - 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 << anns.place_translations["en"] row << anns.place_translations["zh_tw"] row << (anns.event_end_date.strftime("%Y/%m/%d %H:%M") rescue "") - carousel_images = anns.event_carousel_images.asc(:created_at) - t = carousel_images.collect{|f|("http://" + request.host_with_port + f.file.url rescue nil)} + carousel_images = event_carousel_image_map[anns.id].to_a + 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) 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(";") - 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(";") sheet.add_row row end