Skip to content

Commit

Permalink
Merge pull request #3630 from mlibrary/HELIO-4636/still-more-catalog-…
Browse files Browse the repository at this point in the history
…fixes

HELIO-4636 speed up catalog pages
  • Loading branch information
sethaj authored Apr 10, 2024
2 parents fc279a1 + 44757b0 commit 523ea6f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def current_institutions
@current_institutions ||= current_actor.institutions
end

instrument_method
def auth_for(entity)
@auth ||= Auth.new(current_actor, entity) # rubocop:disable Naming/MemoizedInstanceVariableName
end
Expand Down
18 changes: 13 additions & 5 deletions app/controllers/monograph_catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

class MonographCatalogController < ::CatalogController
include IrusAnalytics::Controller::AnalyticsBehaviour
include Skylight::Helpers

before_action :load_presenter, only: %i[index facet purchase]
before_action :monograph_auth_for, only: %i[index purchase]
before_action :load_press_presenter, only: %i[index purchase]
before_action :wayfless_redirect_to_shib_login, only: %i[index]
after_action :add_counter_stat, only: %i[index]
# after_action :send_irus_analytics_investigation, only: %i[index]

self.theme = 'hyrax'
with_themed_layout 'catalog'
Expand Down Expand Up @@ -99,12 +100,21 @@ def item_identifier_for_irus_analytics

private

instrument_method
def load_presenter
retries ||= 0
monograph_id = params[:monograph_id] || params[:id]
@monograph_presenter = Hyrax::PresenterFactory.build_for(ids: [monograph_id], presenter_class: Hyrax::MonographPresenter, presenter_args: current_ability).first
raise PageNotFoundError if @monograph_presenter.nil?
raise CanCan::AccessDenied unless current_ability&.can?(:read, @monograph_presenter)
rescue RSolr::Error::ConnectionRefused, RSolr::Error::Http => e
Rails.logger.error(%Q|[RSOLR ERROR TRY:#{retries}] #{e} #{e.backtrace.join("\n")}|)
retries += 1
retry if retries < 3
end

instrument_method
def monograph_auth_for
auth_for(Sighrax.from_presenter(@monograph_presenter))
@ebook_download_presenter = EBookDownloadPresenter.new(@monograph_presenter, current_ability, current_actor)
# For Access Icons HELIO-3346
Expand All @@ -115,21 +125,19 @@ def load_presenter
# never show up if there is no published ebook for CSB to use! This is important for the "Forthcoming" workflow.
@show_read_button = @monograph_presenter.reader_ebook? && @monograph_presenter&.reader_ebook['visibility_ssi'] == 'open'
@disable_read_button = disable_read_button?
rescue RSolr::Error::ConnectionRefused, RSolr::Error::Http => e
Rails.logger.error(%Q|[RSOLR ERROR TRY:#{retries}] #{e} #{e.backtrace.join("\n")}|)
retries += 1
retry if retries < 3
end

def load_press_presenter
@press_presenter = PressPresenter.for(@monograph_presenter.subdomain)
end

instrument_method
def disable_read_button?
return true if @monograph_presenter.access_level(@actor_product_ids, @allow_read_product_ids).show? && @monograph_presenter.access_level(@actor_product_ids, @allow_read_product_ids).level == :restricted
false
end

instrument_method
def add_counter_stat
# HELIO-2292
return unless @monograph_presenter.epub? || @monograph_presenter.pdf_ebook? || @monograph_presenter.mobi?
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/press_catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class PressCatalogController < ::CatalogController
include Skylight::Helpers
before_action :load_press
before_action :load_actor_product_ids
before_action :load_allow_read_product_ids
before_action :load_actor_product_ids, except: %i[facet]
before_action :load_allow_read_product_ids, except: %i[facet]
before_action :conditional_blacklight_configuration
before_action :wayfless_redirect_to_shib_login, only: %i[index]
after_action :add_counter_stat, only: %i[index]
Expand Down Expand Up @@ -63,7 +63,9 @@ def has_open_access?
instrument_method
def load_press
@press = Press.find_by(subdomain: params['press'])
auth_for(Sighrax::Publisher.from_press(@press))
# HELIO-4636 Don't do auth_for for facets since it's expensive and not needs (probably)
# Oddly :before_action wasn't working in specs for this so awkwardly match on action_name instead
auth_for(Sighrax::Publisher.from_press(@press)) unless self.action_name == "facet"
return @press if @press.present?

render file: Rails.root.join('public', '404.html'), status: :not_found, layout: false
Expand Down
37 changes: 35 additions & 2 deletions app/models/auth.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class Auth
include Skylight::Helpers

instrument_method
def initialize(actor, entity)
@actor = actor
@entity = entity
Expand Down Expand Up @@ -36,6 +39,7 @@ def return_location
end
end

instrument_method
def actor_authorized?
return true unless @monograph.restricted?

Expand All @@ -44,19 +48,23 @@ def actor_authorized?
EPubPolicy.new(@actor, @monograph.ebook, false).show?
end

instrument_method
def actor_unauthorized?
!actor_authorized?
end

instrument_method
def actor_authenticated?
@actor.institutions.present?
end

instrument_method
def actor_single_sign_on_authenticated?
entity_id = @actor.request_attributes[:identity_provider]
entity_id.present? || Incognito.developer?(@actor)
end

instrument_method
def actor_subscribing_institutions
return @actor_subscribing_institutions unless @actor_subscribing_institutions.nil?

Expand All @@ -65,23 +73,33 @@ def actor_subscribing_institutions
@actor_subscribing_institutions
end

instrument_method
def publisher?
@publisher.valid?
end

instrument_method
def publisher_subdomain
@publisher.subdomain
end

instrument_method
def publisher_name
@publisher.name
end

instrument_method
def publisher_work_noids
@publisher_work_noids ||= @publisher.work_noids(true)
end

instrument_method
def publisher_restricted_content?
@publisher_restricted_content ||= Greensub::Component.where(noid: @publisher.work_noids(true)).any? &&
@publisher_restricted_content ||= Greensub::Component.where(noid: publisher_work_noids).any? &&
(publisher_subscribing_institutions - Greensub::Institution.where(identifier: Settings.world_institution_identifier)).present?
end

instrument_method
def publisher_individual_subscribers?
case publisher_subdomain
when 'heliotrope'
Expand All @@ -93,46 +111,56 @@ def publisher_individual_subscribers?
end
end

instrument_method
def publisher_subscribing_institutions
return @publisher_subscribing_institutions unless @publisher_subscribing_institutions.nil?

return @publisher_subscribing_institutions = [] unless @publisher.valid?

@publisher_subscribing_institutions = subscribing_institutions(@publisher.work_noids(true))
@publisher_subscribing_institutions = subscribing_institutions(publisher_work_noids)
end

instrument_method
def monograph?
@monograph.valid?
end

instrument_method
def monograph_id
@monograph.noid
end

instrument_method
def monograph_buy_url?
monograph_buy_url.present?
end

instrument_method
def monograph_buy_url
@monograph.buy_url
end

instrument_method
def monograph_worldcat_url?
monograph_worldcat_url.present?
end

instrument_method
def monograph_worldcat_url
@monograph.worldcat_url
end

instrument_method
def monograph_isbn?
@monograph.preferred_isbn.present?
end

instrument_method
def monograph_isbn
@monograph.preferred_isbn
end

instrument_method
def monograph_subscribing_institutions
return @monograph_subscribing_institutions unless @monograph_subscribing_institutions.nil?

Expand All @@ -141,24 +169,29 @@ def monograph_subscribing_institutions
@monograph_subscribing_institutions = subscribing_institutions(@monograph.noid)
end

instrument_method
def resource?
@resource.valid?
end

instrument_method
def resource_id
@resource.noid
end

instrument_method
def institution?
institution.present?
end

instrument_method
def institution
@institution ||= actor_subscribing_institutions.first || @actor.institutions.first
end

private

instrument_method
def subscribing_institutions(noids)
component_ids = Greensub::Component.where(noid: noids).pluck(:id).uniq
product_ids = Greensub::ComponentsProduct.where(component_id: component_ids).pluck(:product_id).uniq
Expand Down
4 changes: 4 additions & 0 deletions app/models/sighrax/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Sighrax
class Publisher
include Skylight::Helpers

private_class_method :new

attr_reader :subdomain
Expand Down Expand Up @@ -57,13 +59,15 @@ def children
children
end

instrument_method
def work_noids(recursive = false)
subdomains = recursive ? press.children.pluck(:subdomain) : []
subdomains = subdomains.push(subdomain).uniq
docs = ActiveFedora::SolrService.query("{!terms f=press_sim}#{subdomains.map(&:downcase).join(',')}", fl: ['id'], rows: 100_000)
docs.map { |doc| doc['id'] }.uniq
end

instrument_method
def resource_noids(recursive = false)
resource_noids = []
work_noids(recursive).each do |noid|
Expand Down
2 changes: 2 additions & 0 deletions app/overrides/hyrax/file_sets_controller_overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
prepend(FileSetsControllerBehavior = Module.new do
Hyrax::FileSetsController.form_class = ::Heliotrope::FileSetEditForm
include IrusAnalytics::Controller::AnalyticsBehaviour
include Skylight::Helpers

instrument_method
def show # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
# local heliotrope changes
(redirect_to Rails.application.routes.url_helpers.monograph_catalog_path(presenter&.parent&.id)) && return if bounce_from_representatives?
Expand Down

0 comments on commit 523ea6f

Please sign in to comment.