accelerate feed

This commit is contained in:
邱博亞 2021-11-09 18:11:11 +08:00
parent 98e6860af2
commit ed065e5cb3
3 changed files with 166 additions and 116 deletions

View File

@ -15,131 +15,181 @@ class SiteFeedAnnc
def get_annc(annc_uid) def get_annc(annc_uid)
Array(self[:all_contents_for_feed]).select{|v| v['id']==annc_uid}[0] rescue {} Array(self[:all_contents_for_feed]).select{|v| v['id']==annc_uid}[0] rescue {}
end end
def all_contents_for_feed(site_source=nil,locale=I18n.locale.to_s,is_widget=false) before_save do
if self.top_list_changed? || self.hot_list_changed? || self.hidden_annc_changed? || self.category_title_changed?
self[:all_contents_for_feed] = self.cache_annc
end
end
def cache_annc(force_refresh=false)
feed = SiteFeed.find(self.feed_id)
anns = feed.get_annc(force_refresh)
cat = self.category_title cat = self.category_title
Array(self[:all_contents_for_feed]).collect do |v| locales = Site.first.in_use_locales rescue I18n.available_locales
tmp = v cache_data = {}
if hidden_annc.exclude?(v['id']) && !tmp["title_translations"][locale].blank? locales.each do |locale|
if self.channel_key == "announcement" locale = locale.to_s
if (!tmp["postdate"].blank? && tmp["postdate"].to_time>Time.now) data_for_locale = I18n.with_locale(locale) do
next anns.collect do |a|
end tmp = a.deep_dup
tmp['statuses'] = [] tmp[:is_hidden] = self.hidden_annc.include?(tmp['id'])
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id']) if self.channel_key == "announcement"
tmp[:is_top] = false tmp["postdate"] = tmp["postdate"].blank? ? nil : tmp["postdate"].to_time
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => I18n.t(:top),
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => I18n.t(:hot),
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
if !tmp["source-site-title"].blank?
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
end
files = tmp["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (bf["title_translations"][locale].blank? ? File.basename(bf["url"]) : bf["title_translations"][locale] rescue '') }} rescue []
links = tmp["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
tmp["bulletin_links"] = links
tmp["bulletin_files"] = files
tmp["title"] = tmp["title_translations"][locale]
tmp["subtitle"] = tmp["subtitle_translations"][locale]
tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
tmp["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil
tmp["target"] = "_self"
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
tmp["img_description"] = tmp["image_description_translations"][locale]
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
else else
tmp[:is_top] = true tmp['statuses'] = []
tmp['statuses'] << { if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
"status" => I18n.t(:top), tmp[:is_top] = false
"status-class" => "status-top" else
} tmp[:is_top] = true
end tmp['statuses'] << {
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id']) "status" => I18n.t(:top),
tmp[:is_hot] = false "status-class" => "status-top"
else }
tmp[:is_hot] = true end
tmp['statuses'] << { if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
"status" => I18n.t(:hot), tmp[:is_hot] = false
"status-class" => "status-hot" else
} tmp[:is_hot] = true
end tmp['statuses'] << {
tmp["category"] = cat "status" => I18n.t(:hot),
tmp["source-site"] = self.remote_site_url "status-class" => "status-hot"
tmp["source-site-title"] = (self[:channel_title][locale] rescue "") }
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h" end
next if !site_source.nil? && site_source != tmp["source-site-title"] tmp["category"] = cat
if !tmp["source-site-title"].blank? tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
tmp['statuses'] << { tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>", "status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source" "status-class" => "status-source"
} }
end tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
files = tmp["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (bf["title_translations"][locale].blank? ? File.basename(bf["url"]) : bf["title_translations"][locale] rescue '') }} rescue [] tmp["target"] = "_self"
links = tmp["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue [] tmp["more"] = I18n.t(:more_plus)
tmp["bulletin_links"] = links tmp["view_count"] = ""
tmp["bulletin_files"] = files new_tmp = {}
tmp["title"] = tmp["title_translations"][locale] tmp.each do |key,value|
tmp["subtitle"] = tmp["subtitle_translations"][locale] if key.include? "_translations"
tmp["source-site-link"] = tmp["source-site"] new_tmp[key.sub("_translations","")] = value[locale].to_s rescue ""
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>" elsif key.include?("date") || key.include?("Date")
if !tmp["external_link"].blank? new_tmp[key] = DateTime.parse(value) rescue nil
tmp["link_to_show"] = tmp["external_link"]
else
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
end
tmp["target"] = "_self"
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
tmp["img_description"] = tmp["image_description_translations"][locale]
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
else
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => I18n.t(:top),
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => I18n.t(:hot),
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
next if !site_source.nil? && site_source != tmp["source-site-title"]
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
tmp["target"] = "_self"
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
new_tmp = {}
tmp.each do |key,value|
if key.include? "_translations"
new_tmp[key.sub("_translations","")] = value[locale].to_s rescue ""
elsif key.include?("date") || key.include?("Date")
new_tmp[key] = DateTime.parse(value) rescue nil
else
if value.class == Hash
value.each do |sub_k,sub_v|
if sub_k.include? "_translations"
new_tmp[key][sub_k.sub("_translations","")] = sub_v[locale].to_s rescue ""
else
new_tmp[key][sub_k] = sub_v
end
end
else else
new_tmp[key] = value if value.class == Hash
value.each do |sub_k,sub_v|
if sub_k.include? "_translations"
new_tmp[key][sub_k.sub("_translations","")] = sub_v[locale].to_s rescue ""
else
new_tmp[key][sub_k] = sub_v
end
end
else
new_tmp[key] = value
end
end end
end end
tmp = BSON::Document.new(new_tmp)
end end
tmp = BSON::Document.new(new_tmp) tmp
end
end
cache_data[locale.to_s] = data_for_locale
end
cache_data
end
def self.get_feed_cache(channel_key,merge_with_category=nil,site_source=nil,locale=I18n.locale.to_s,is_widget=false,max_len=nil)
match_cond = {"channel_key"=>channel_key}
if !merge_with_category.blank? && merge_with_category.exclude?('all')
match_cond["merge_with_category"] = {"$in"=>merge_with_category}
end
if site_source
match_cond["channel_title.#{locale}"] = site_source
end
pipeline = [
{"$match"=>match_cond},
{"$project"=>{"data"=>"$all_contents_for_feed.#{locale}"}},
{"$unwind"=>"$data"},
{"$match"=>{"data.is_hidden"=>{"$ne"=>true},
"data.postdate"=>{"$lte"=>Time.now},
"data.title" => {"$gt"=>""}
}
},
{"$sort"=>{"data.is_top"=>-1,"data.postdate"=>-1}}
]
if max_len
pipeline << {"$limit"=> max_len}
end
self.collection.aggregate(pipeline).collect do |data|
tmp = data['data']
if tmp["link_to_show"].nil?
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
end
tmp
end
end
def all_contents_for_feed(site_source=nil,locale=I18n.locale.to_s,is_widget=false)
if !site_source.nil? && site_source != self[:channel_title][locale]
return []
end
time_now = Time.now
Array(self[:all_contents_for_feed][locale.to_s]).collect do |v|
tmp = v
next if tmp["is_hidden"] || (!tmp["postdate"].nil? && tmp["postdate"]<Time.now) || tmp['title'].blank?
if tmp["link_to_show"].nil?
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end end
else
tmp = nil
end end
tmp tmp
end.compact end.compact

View File

@ -12,12 +12,12 @@ module FeedModel
tmp = SiteFeedAnnc.new(feed_id: feed.id) tmp = SiteFeedAnnc.new(feed_id: feed.id)
end end
tmp[:feed_name] = feed[:feed_name] tmp[:feed_name] = feed[:feed_name]
tmp.all_contents_for_feed = feed.get_annc(true)
tmp.merge_with_category = feed.merge_with_category tmp.merge_with_category = feed.merge_with_category
tmp.channel_key = feed.channel_key tmp.channel_key = feed.channel_key
tmp.category_title = feed.category[:title] rescue {} tmp.category_title = feed.category[:title] rescue {}
tmp.remote_site_url = feed.remote_site_url tmp.remote_site_url = feed.remote_site_url
tmp.channel_title = feed.channel_title_for_cache tmp.channel_title = feed.channel_title_for_cache
tmp.all_contents_for_feed = tmp.cache_annc(true)
tmp.save tmp.save
end end
def do_before_save def do_before_save

View File

@ -12,13 +12,13 @@ module Feeds
if tmp.nil? if tmp.nil?
tmp = SiteFeedAnnc.new(feed_id: site_feed.id) tmp = SiteFeedAnnc.new(feed_id: site_feed.id)
end end
tmp.all_contents_for_feed = site_feed.get_annc
tmp[:feed_name] = site_feed[:feed_name] tmp[:feed_name] = site_feed[:feed_name]
tmp.merge_with_category = site_feed.merge_with_category tmp.merge_with_category = site_feed.merge_with_category
tmp.channel_key = site_feed.channel_key tmp.channel_key = site_feed.channel_key
tmp.category_title = site_feed.category[:title] rescue {} tmp.category_title = site_feed.category[:title] rescue {}
tmp.remote_site_url = site_feed.remote_site_url tmp.remote_site_url = site_feed.remote_site_url
tmp.channel_title = site_feed.channel_title_for_cache tmp.channel_title = site_feed.channel_title_for_cache
tmp.all_contents_for_feed = tmp.cache_annc
tmp.save tmp.save
elsif !tmp.nil? elsif !tmp.nil?
tmp.destroy tmp.destroy