reimplement Tag.trending to use old danbooru 1 behavior

This commit is contained in:
r888888888
2013-06-30 15:56:00 -07:00
parent 42e1d4a1e1
commit c61b10cb94
2 changed files with 27 additions and 6 deletions

View File

@@ -121,20 +121,25 @@ class Tag < ActiveRecord::Base
Cache.get("popular-tags", 1.hour) do Cache.get("popular-tags", 1.hour) do
CurrentUser.scoped(User.admins.first, "127.0.0.1") do CurrentUser.scoped(User.admins.first, "127.0.0.1") do
n = 1 n = 1
results = [] counts = {}
while results.empty? && n < 256 while counts.empty? && n < 256
query = n.days.ago.strftime("date:>%Y-%m-%d") tag_strings = Post.select_values_sql("select tag_string from posts where created_at >= ? order by md5 limit 100", n.days.ago)
results = RelatedTagCalculator.calculate_from_sample_to_array(query) tag_strings.each do |tag_string|
tag_string.scan(/\S+/).each do |tag|
counts[tag] ||= 0
counts[tag] += 1
end
end
n *= 2 n *= 2
end end
results.map! do |tag_name, recent_count| counts = counts.to_a.map do |tag_name, recent_count|
tag = Tag.find_or_create_by_name(tag_name) tag = Tag.find_or_create_by_name(tag_name)
[tag_name, recent_count.to_f / tag.post_count.to_f] [tag_name, recent_count.to_f / tag.post_count.to_f]
end end
results.sort_by! {|x| -x[1]}.map(&:first) counts.sort_by {|x| -x[1]}.map(&:first)
end end
end end
end end

View File

@@ -14,6 +14,22 @@ class TagTest < ActiveSupport::TestCase
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
end end
context ".trending" do
setup do
Timecop.travel(1.week.ago) do
Post.create(:tag_string => "aaa")
Post.create(:tag_string => "bbb")
end
Post.create(:tag_string => "bbb")
Post.create(:tag_string => "ccc")
end
should "order the results by the total post count" do
assert_equal([], Tag.trending)
end
end
context "A tag category fetcher" do context "A tag category fetcher" do
setup do setup do
MEMCACHE.flush_all MEMCACHE.flush_all