Skip to content

Commit

Permalink
Improve performance of users/show
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas16 committed Oct 19, 2024
1 parent f4020b5 commit 93880d1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
25 changes: 19 additions & 6 deletions app/models/following.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#
# Table name: followings
#
# id :integer not null, primary key
# submission_id :integer
# user_id :integer
# read :boolean
# created_at :datetime not null
# kind :integer default(NULL)
# id :integer not null, primary key
# submission_id :integer
# user_id :integer
# read :boolean
# created_at :datetime not null
# kind :integer default(NULL)
# submission_user_id :integer
#
class Following < ActiveRecord::Base

Expand All @@ -21,10 +22,22 @@ class Following < ActiveRecord::Base

belongs_to :submission
belongs_to :user
belongs_to :submission_user, class_name: "User" # Could be avoided, but to go faster in users/show

# VALIDATIONS

validates :user_id, uniqueness: { scope: :submission_id }
validates :kind, presence: true

# BEFORE, AFTER

before_validation :set_submission_user_id

# Automatically compute submission_user_id
def set_submission_user_id
unless self.submission.nil?
self.submission_user = self.submission.user
end
end

end
7 changes: 5 additions & 2 deletions app/models/globalstatistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ class Globalstatistic < ActiveRecord::Base
# Get the only one row of this table
def self.get
statistic = Globalstatistic.first
return statistic unless statistic.nil?
return Globalstatistic.create
if statistic.nil?
statistic = Globalstatistic.create
statistic.update_all
end
return statistic
end

# Recompute all statistics from scratch
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def num_notifications_new(levels)

# Gives the number of submissions with a new comment to read
def num_notifications_update
return followed_submissions.where(followings: { read: false }).count
return followings.where(:read => false).count
end

# Gives the "level" of the user
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<% actualrating = @user.rating %>
<% if actualrating > 0 %>
<% rank = User.where("rating > ? AND admin = ? AND active = ?", actualrating, false, true).count + 1 %>
<% num_users = User.where("rating > 0 AND admin = ? AND active = ?", false, true).count %>
<% num_users = Globalstatistic.get.nb_ranked_users %>
<% rank_centile = (rank == 1 ? 100 : (100.0 * (num_users - rank + 1) / num_users).round(1)) %>
<% end %>
<% niveau = @user.level %>
Expand Down Expand Up @@ -548,7 +548,7 @@
<div class="pe-2 pb-2" style="padding-left:20px;">
<%= @user.name %> s'est inscrit<%= "e" if @user.sex == 1 %> sur Mathraining le <span class="user_color"><%= write_date_only(@user.created_at) %></span>.
<% if nb_p >= 3 %>
<% followings = Following.joins(:submission).includes(:user).where("submissions.user_id = ?", @user.id).group("followings.user_id").select("count(followings.id) AS num_corrections, followings.user_id").order("num_corrections DESC, followings.user_id ASC").to_a %>
<% followings = Following.includes(:user).where(:submission_user => @user, :kind => [:first_corrector, :other_corrector]).group(:user_id).select("count(id) AS num_corrections, user_id").order("num_corrections DESC, user_id ASC").to_a %>
<% if followings.size > 0 %>
<% tot_num_corr = followings.sum{|f| f.num_corrections} %>
<% num_correctors_to_show = [3, followings.size].min %>
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241019083617_add_submission_user_id_to_followings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddSubmissionUserIdToFollowings < ActiveRecord::Migration[7.1]
def change
add_column :followings, :submission_user_id, :integer
add_index :followings, :submission_user_id

up_only do
execute("UPDATE followings f SET submission_user_id = s.user_id FROM submissions s WHERE s.id = f.submission_id")
end
end
end
13 changes: 7 additions & 6 deletions spec/models/following_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#
# Table name: followings
#
# id :integer not null, primary key
# submission_id :integer
# user_id :integer
# read :boolean
# created_at :datetime not null
# kind :integer default(NULL)
# id :integer not null, primary key
# submission_id :integer
# user_id :integer
# read :boolean
# created_at :datetime not null
# kind :integer default(NULL)
# submission_user_id :integer
#
require "spec_helper"

Expand Down

0 comments on commit 93880d1

Please sign in to comment.