From 7e6670b2eb6cd880e3e78a18e611d4a0557a5000 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 1 Jul 2013 01:16:28 -0700 Subject: [PATCH] fix tag trending --- app/models/tag.rb | 13 +++++++++---- config/danbooru_default_config.rb | 2 +- test/unit/tag_test.rb | 2 ++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 85997cbdd..e48591318 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -117,10 +117,14 @@ class Tag < ActiveRecord::Base end module StatisticsMethods + def trending_count_limit + 20 + end + def trending - Cache.get("popular-tags", 1.hour) do + Cache.get("popular-tags-v2", 1.hour) do CurrentUser.scoped(User.admins.first, "127.0.0.1") do - n = 1 + n = 3 counts = {} while counts.empty? && n < 256 @@ -134,12 +138,13 @@ class Tag < ActiveRecord::Base n *= 2 end - counts = counts.to_a.map do |tag_name, recent_count| + counts = counts.to_a.select {|x| x[1] > trending_count_limit} + counts = counts.map do |tag_name, recent_count| tag = Tag.find_or_create_by_name(tag_name) [tag_name, recent_count.to_f / tag.post_count.to_f] end - counts.sort_by {|x| -x[1]}.map(&:first) + counts.sort_by {|x| -x[1]}.slice(0, 20).map(&:first) end end end diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 567138ad4..dcc731242 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -4,7 +4,7 @@ module Danbooru class Configuration # The version of this Danbooru. def version - "2.22.0" + "2.22.1" end # The name of this Danbooru. diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index 9e82f8288..51a759233 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -16,6 +16,8 @@ class TagTest < ActiveSupport::TestCase context ".trending" do setup do + Tag.stubs(:trending_count_limit).returns(0) + Timecop.travel(1.week.ago) do FactoryGirl.create(:post, :tag_string => "aaa") FactoryGirl.create(:post, :tag_string => "bbb")