Skip to content

Commit 91e5253

Browse files
authored
Remove unnecessary query to try to speed up ht_users index page. (#243)
* Remove unnecessary query to try to speed up ht_users index page. - `@all_users` was being populated via SQL in all cases even though it was only used for CSV download. - The query has been removed and the CSV now uses the active + inactive arrays so the query was redundant anyway. * - (Controller) `HTUser.includes` changed from `HTUser.joins` to pick up the approval requests without having to make a query for each. - `HTApprovalRequest.most_recent_order` made public so the correct request can be selected by the presenter. - `HTUser#ht_approval_request` uses `most_recent_order` by default for cases (if any) not covered by the controller change.
1 parent 7cac7e4 commit 91e5253

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

app/controllers/ht_users_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ class HTUsersController < ApplicationController
77
usertype role access expires expire_type iprestrict mfa].freeze
88

99
def index
10-
users = HTUser.joins(:ht_institution).order("ht_institutions.name")
10+
users = HTUser.includes(:ht_institution, :ht_approval_request).order("ht_institutions.name").order(HTApprovalRequest.most_recent_order)
1111
@users = users.active.map { |u| presenter u }
1212
@expired_users = users.expired.map { |u| presenter u }
13-
@all_users = users.map { |u| presenter u }
1413
respond_to do |format|
1514
format.html
1615
format.csv do
@@ -65,9 +64,10 @@ def user_params
6564

6665
def users_csv
6766
require "csv"
67+
all_users = @users + @expired_users
6868
CSV.generate do |csv|
69-
csv << @all_users.first.csv_cols
70-
@all_users.each do |user|
69+
csv << all_users.first.csv_cols
70+
all_users.each do |user|
7171
if params[:role_filter]&.include?(user.role)
7272
next
7373
end

app/models/ht_approval_request.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ class HTApprovalRequest < ApplicationRecord
77
def self.expiration_date
88
Date.today - 1.week
99
end
10+
11+
# Make sure that the most recent and most "incomplete" request comes first when fetching request for user
12+
# If we had created/updated timestamps we could use those.
13+
def self.most_recent_order
14+
{
15+
Arel.sql("sent IS NULL") => :desc, :sent => :desc,
16+
Arel.sql("received IS NULL") => :desc, :received => :desc,
17+
Arel.sql("renewed IS NULL") => :desc, :renewed => :desc
18+
}
19+
end
1020
# "Active" requests that may be subject to further action
1121
scope :not_renewed, -> { where(renewed: nil) }
1222
# "Inactive" requests that are only of historical interest
@@ -15,11 +25,6 @@ def self.expiration_date
1525
scope :active, -> { where(renewed: nil, received: nil).where.not(sent: nil).where("sent >= ?", HTApprovalRequest.expiration_date) }
1626
scope :for_approver, ->(approver) { where(approver: approver).order(:sent, :received, :renewed) }
1727
scope :for_user, ->(user) { where(userid: user).order(:sent, :received, :renewed) }
18-
# Make sure that the most recent and most "incomplete" request comes first when fetching request for user
19-
# If we had created/updated timestamps we could use those.
20-
most_recent_order = {Arel.sql("sent IS NULL") => :desc, :sent => :desc,
21-
Arel.sql("received IS NULL") => :desc, :received => :desc,
22-
Arel.sql("renewed IS NULL") => :desc, :renewed => :desc}
2328
scope :most_recent, ->(user) { for_user(user).order(most_recent_order) }
2429
scope :not_approved, -> { where(received: nil) }
2530
scope :approved, -> { where.not(received: nil) }

app/models/ht_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HTUser < ApplicationRecord
2323
belongs_to :ht_institution, foreign_key: :inst_id, primary_key: :inst_id
2424
has_one :ht_count, foreign_key: :userid, primary_key: :userid
2525
has_many :ht_logs, -> { HTLog.ht_user }, foreign_key: :objid, primary_key: :email
26-
has_many :ht_approval_request, foreign_key: :userid, primary_key: :email
26+
has_many :ht_approval_request, -> { order(HTApprovalRequest.most_recent_order) }, foreign_key: :userid, primary_key: :email
2727
has_many :ht_registration, foreign_key: :applicant_email, primary_key: :email
2828

2929
validates :iprestrict, presence: true, unless: :mfa

app/presenters/ht_user_presenter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def show_expires
9292
end
9393

9494
def show_institution
95+
return institution if ht_institution.nil?
96+
9597
link_to institution, ht_institution_path(ht_institution.inst_id)
9698
end
9799

@@ -130,7 +132,7 @@ def simple_email_link
130132
end
131133

132134
def approval_request
133-
@approval_request ||= HTApprovalRequest.most_recent(email).first
135+
@approval_request ||= ht_approval_request.first
134136
end
135137

136138
def renewal_status_badge

0 commit comments

Comments
 (0)