fix tag trending
This commit is contained in:
@@ -117,10 +117,14 @@ class Tag < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
module StatisticsMethods
|
module StatisticsMethods
|
||||||
|
def trending_count_limit
|
||||||
|
20
|
||||||
|
end
|
||||||
|
|
||||||
def trending
|
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
|
CurrentUser.scoped(User.admins.first, "127.0.0.1") do
|
||||||
n = 1
|
n = 3
|
||||||
counts = {}
|
counts = {}
|
||||||
|
|
||||||
while counts.empty? && n < 256
|
while counts.empty? && n < 256
|
||||||
@@ -134,12 +138,13 @@ class Tag < ActiveRecord::Base
|
|||||||
n *= 2
|
n *= 2
|
||||||
end
|
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 = 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
|
||||||
|
|
||||||
counts.sort_by {|x| -x[1]}.map(&:first)
|
counts.sort_by {|x| -x[1]}.slice(0, 20).map(&:first)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ module Danbooru
|
|||||||
class Configuration
|
class Configuration
|
||||||
# The version of this Danbooru.
|
# The version of this Danbooru.
|
||||||
def version
|
def version
|
||||||
"2.22.0"
|
"2.22.1"
|
||||||
end
|
end
|
||||||
|
|
||||||
# The name of this Danbooru.
|
# The name of this Danbooru.
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ class TagTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
context ".trending" do
|
context ".trending" do
|
||||||
setup do
|
setup do
|
||||||
|
Tag.stubs(:trending_count_limit).returns(0)
|
||||||
|
|
||||||
Timecop.travel(1.week.ago) do
|
Timecop.travel(1.week.ago) do
|
||||||
FactoryGirl.create(:post, :tag_string => "aaa")
|
FactoryGirl.create(:post, :tag_string => "aaa")
|
||||||
FactoryGirl.create(:post, :tag_string => "bbb")
|
FactoryGirl.create(:post, :tag_string => "bbb")
|
||||||
|
|||||||
Reference in New Issue
Block a user