Skip to content

Commit

Permalink
Merge pull request #975 from rubygems/revert-961-stats-page
Browse files Browse the repository at this point in the history
Revert "fetch top gems from redis instead of postgres"
  • Loading branch information
dwradcliffe committed Jun 9, 2015
2 parents ee36a61 + 453548d commit e4e248d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 54 deletions.
21 changes: 2 additions & 19 deletions app/models/rubygem.rb
Expand Up @@ -62,8 +62,8 @@ def self.latest(limit=5)
with_one_version.order(created_at: :desc).limit(limit)
end

def self.downloaded(limit=10)
most_downloaded_by_redis(limit) || most_downloaded_by_db(limit)
def self.downloaded(limit=5)
with_versions.by_downloads.limit(limit)
end

def self.letter(letter)
Expand All @@ -82,23 +82,6 @@ def self.monthly_short_dates
monthly_dates.map { |date| date.strftime("%m/%d") }
end

def self.most_downloaded_by_db(limit=10)
with_versions.by_downloads.limit(limit)
end

def self.most_downloaded_by_redis(limit=10)
counts = {}
self.pluck(:name).in_groups_of(100).each do |group|
group.each do |name|
counts[name] = Download.for_rubygem(name)
end
end
counts = counts.sort_by{|x| -x.last.to_i }.first(limit).collect(&:first)
self.where(name: counts).by_downloads
rescue
nil
end

def self.versions_key(name)
"r:#{name}"
end
Expand Down
36 changes: 1 addition & 35 deletions test/unit/rubygem_test.rb
Expand Up @@ -496,7 +496,7 @@ class RubygemTest < ActiveSupport::TestCase
end

should "only latest downloaded versions" do
assert_equal [@thin, @rake, @json, @thor, @rack], Rubygem.downloaded(5)
assert_equal [@thin, @rake, @json, @thor, @rack], Rubygem.downloaded
assert_equal [@thin, @rake, @json, @thor, @rack, @dust], Rubygem.downloaded(6)
end
end
Expand Down Expand Up @@ -683,39 +683,5 @@ class RubygemTest < ActiveSupport::TestCase
assert_equal(("01".."30").map { |date| "10/#{date}" }, Rubygem.monthly_short_dates)
end
end

context "give most downloaded gems" do
setup do
@thin = create(:rubygem, :name => 'thin', :downloads => 76)
@rake = create(:rubygem, :name => 'rake', :downloads => 45)
@json = create(:rubygem, :name => 'json', :downloads => 32)
@thor = create(:rubygem, :name => 'thor', :downloads => 20)
@rack = create(:rubygem, :name => 'rack', :downloads => 13)
@dust = create(:rubygem, :name => 'dust', :downloads => 0)
@haml = create(:rubygem, :name => 'haml', :downloads => 0)
@chronic = create(:rubygem, :name => 'chronic', :downloads => 0)

@gems = [@thin, @rake, @json, @thor, @rack, @dust, @haml, @chronic]
@gems.each do |g|
create(:version, :rubygem => g)
g[:downloads].times { Download.incr(g.name, @version.full_name) }
end
end

should "by postgres" do
assert_equal [@thin, @rake, @json, @thor, @rack], Rubygem.most_downloaded_by_db(5)
end

should "by redis" do
assert_equal [@thin, @rake, @json, @thor, @rack], Rubygem.most_downloaded_by_redis(5)
end

should "differentiate if the ranking is not the same for redis and postgres" do
version = @thin.versions.last
version.indexed = false
version.save
assert_not_equal Rubygem.most_downloaded_by_db(5), Rubygem.most_downloaded_by_redis(5)
end
end
end
end

0 comments on commit e4e248d

Please sign in to comment.