Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rubygem download ranking #3812

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/stylesheets/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
.l-mb-4 {
margin-bottom: 1rem; }

.l-mb-6 {
margin-bottom: 1.5rem; }

.l-mb-8 {
margin-bottom: 2rem; }

Expand Down
1 change: 0 additions & 1 deletion app/assets/stylesheets/modules/gem.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
margin-bottom: 6px; }

.gem__downloads {
margin-bottom: 20px;
display: block;
font-weight: 800;
font-size: 24px; }
Expand Down
3 changes: 1 addition & 2 deletions app/assets/stylesheets/type.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@

.t-text--s {
font-weight: 500;
font-size: 12px;
text-transform: uppercase; }
font-size: 12px; }

.t-hidden {
position: absolute !important;
Expand Down
22 changes: 22 additions & 0 deletions app/models/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ def downloads
gem_download&.count || 0
end

def gem_rank
gem_ranking = update_gems_ranking

return gem_ranking[id] if gem_ranking.key?(id)

Rails.cache.delete("gems_ranking")
update_gems_ranking[id]
end

def most_recent_version
versions.most_recent
end
Expand Down Expand Up @@ -446,4 +455,17 @@ def bulk_reorder_versions
sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, update_query)
ActiveRecord::Base.connection.execute(sanitized_query)
end

def update_gems_ranking
Rails.cache.fetch("gems_ranking", expires_in: 24.hours) do
update_query = <<-SQL.squish
SELECT rubygem_id, row_number() OVER(ORDER BY count DESC) AS position
FROM gem_downloads
WHERE version_id = 0 AND rubygem_id <> 0
SQL
sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, [update_query])
pg_result = ActiveRecord::Base.connection.execute(sanitized_query)
pg_result.to_h { |row| [row["rubygem_id"], row["position"]] }
end
end
end
8 changes: 4 additions & 4 deletions app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@
</div>

<div class="profile__downloads-wrap">
<h4 class="gem__downloads__heading t-text--s">
<h4 class="gem__downloads__heading t-text--s t-uppercase">
<%= t('stats.index.total_gems') %>
</h4>

<h2 id="profile-gems-count" class="gem__downloads">
<h2 id="profile-gems-count" class="gem__downloads l-mb-6">
<%= @user.total_rubygems_count.to_s %>
</h2>

<h4 id="downloads" class="gem__downloads__heading t-text--s">
<h4 id="downloads" class="gem__downloads__heading t-text--s t-uppercase">
<%= t('stats.index.total_downloads') %>
</h4>

<h2 id="downloads_count" class="gem__downloads">
<h2 id="downloads_count" class="gem__downloads l-mb-6">
<%= number_with_delimiter(@user.total_downloads_count) %>
</h2>
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/views/rubygems/_aside.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

<div class="gem__downloads-wrap" data-href="<%= api_v1_download_path(@latest_version.full_name, :format => 'json') %>">
<h2 class="gem__downloads__heading t-text--s">
<%= t('stats.index.total_downloads') %>
<span class="t-uppercase"><%= t('stats.index.total_downloads') %></span>
<span class="gem__downloads"><%= number_with_delimiter(@rubygem.downloads) %></span>
<span class="gem__downloads t-text--s l-mb-6"><%= t('stats.index.ranking', count: number_with_delimiter(@rubygem.gem_rank), total: number_with_delimiter(Rubygem.total_count)) %></span>
</h2>
<h2 class="gem__downloads__heading t-text--s">
<h2 class="gem__downloads__heading t-text--s t-uppercase">
<%= t('.downloads_for_this_version') %>
<span class="gem__downloads"><%= number_with_delimiter(@latest_version.downloads_count) %></span>
</h2>
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ de:
total_downloads: Downloads insgesamt
total_gems: Gems insgesamt
total_users: Benutzer insgesamt
ranking: "%{count} von %{total}"
users:
create:
email_sent:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ en:
total_downloads: Total downloads
total_gems: Total gems
total_users: Total users
ranking: "%{count} of %{total}"
users:
create:
email_sent: A confirmation mail has been sent to your email address.
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ es:
total_downloads: Total de descargas
total_gems: Gemas totales
total_users: Usuarios totales
ranking: "%{count} de %{total}"
users:
create:
email_sent: Se ha enviado un correo de confirmación a tu casilla de correo electrónico.
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ fr:
total_downloads: Total de téléchargements
total_gems: Total de gems
total_users: Total d'utilisateurs
ranking: "%{count} de %{total}"
users:
create:
email_sent: Un email de confirmation a été envoyé sur votre adresse email.
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ ja:
total_downloads: 累計ダウンロード数
total_gems: Gem総数
total_users: ユーザー総数
ranking:
users:
create:
email_sent: 確認メールがあなたのメールアドレスに送信されました。
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ nl:
total_downloads:
total_gems:
total_users:
ranking:
users:
create:
email_sent: Een bevestigingsemail is naar je e-mailadres gestuurd
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ pt-BR:
total_downloads: Total de downloads
total_gems: Total de gems
total_users: Total de usuários
ranking: "%{count} de %{total}"
users:
create:
email_sent:
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ zh-CN:
total_downloads: 下载总次数
total_gems: Gems 总数
total_users: 用户总数
ranking:
users:
create:
email_sent: 确认邮件已发送到您的电子邮件地址。
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ zh-TW:
total_downloads: 總下載次數
total_gems: Gems 總數
total_users: 總使用者數量
ranking:
users:
create:
email_sent: 已發送確認信到你的信箱地址。
Expand Down
37 changes: 37 additions & 0 deletions test/models/rubygem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,41 @@ class RubygemTest < ActiveSupport::TestCase
refute_predicate @version_three, :yanked?
end
end

context "#gem_rank" do
setup do
@rank_one = create(:rubygem, downloads: 5)
@rank_two = create(:rubygem, downloads: 1)

@dummy_rank = { @rank_one.id => 1, @rank_two.id => 2 }
end

should "write to cache" do
@rank_one.gem_rank

assert_equal Rails.cache.read("gems_ranking"), @dummy_rank
end

should "read from cache" do
@rank_two.gem_rank

GemDownload.increment(10, rubygem_id: @rank_two.id)

@rank_two.gem_rank

assert_equal Rails.cache.read("gems_ranking"), @dummy_rank
end

should "return the gem rank" do
assert_equal 1, @rank_one.gem_rank
end

should "update when rubygem not ranked" do
@rank_two.gem_rank

@rank_three = create(:rubygem, downloads: 3)

assert_equal(2, @rank_three.gem_rank)
end
end
end