From c61b10cb948d493b22de0aff93b94cbf8b85b028 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Sun, 30 Jun 2013 15:56:00 -0700 Subject: [PATCH] reimplement Tag.trending to use old danbooru 1 behavior --- app/models/tag.rb | 17 +++++++++++------ test/unit/tag_test.rb | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 3a78a2794..85997cbdd 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -121,20 +121,25 @@ class Tag < ActiveRecord::Base Cache.get("popular-tags", 1.hour) do CurrentUser.scoped(User.admins.first, "127.0.0.1") do n = 1 - results = [] + counts = {} - while results.empty? && n < 256 - query = n.days.ago.strftime("date:>%Y-%m-%d") - results = RelatedTagCalculator.calculate_from_sample_to_array(query) + while counts.empty? && n < 256 + tag_strings = Post.select_values_sql("select tag_string from posts where created_at >= ? order by md5 limit 100", n.days.ago) + tag_strings.each do |tag_string| + tag_string.scan(/\S+/).each do |tag| + counts[tag] ||= 0 + counts[tag] += 1 + end + end n *= 2 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_name, recent_count.to_f / tag.post_count.to_f] end - results.sort_by! {|x| -x[1]}.map(&:first) + counts.sort_by {|x| -x[1]}.map(&:first) end end end diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index d6e9add27..c595d20ae 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -14,6 +14,22 @@ class TagTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil 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 setup do MEMCACHE.flush_all