Skip to content

Commit

Permalink
Merge pull request #5713 from avalonmediasystem/manage_content_quickly
Browse files Browse the repository at this point in the history
Optimize collection media object counts
  • Loading branch information
cjcolvar authored Mar 5, 2024
2 parents a142cb5 + 626c426 commit 1e96a12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 12 additions & 1 deletion app/controllers/admin/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ def load_and_authorize_collections
builder.user = user
end
response = repository.search(builder)
@collections = response.documents.collect { |doc| ::Admin::CollectionPresenter.new(doc) }.sort_by { |c| c.name.downcase }

# Query solr for facet values for collection media object counts and pass into presenter to avoid making 2 solr queries per collection
count_query = "has_model_ssim:MediaObject"
count_response = ActiveFedora::SolrService.get(count_query, { rows: 0, facet: true, 'facet.field': "isMemberOfCollection_ssim", 'facet.limit': -1 })
counts_array = count_response["facet_counts"]["facet_fields"]["isMemberOfCollection_ssim"] rescue []
counts = counts_array.blank? ? {} : [counts_array].to_h
unpublished_query = count_query + " AND workflow_published_sim:Unpublished"
unpublished_count_response = ActiveFedora::SolrService.get(unpublished_query, { rows: 0, facet: true, 'facet.field': "isMemberOfCollection_ssim", 'facet.limit': -1 })
unpublished_counts_array = unpublished_count_response["facet_counts"]["facet_fields"]["isMemberOfCollection_ssim"] rescue []
unpublished_counts = unpublished_counts_array.blank? ? {} : [unpublished_counts_array].to_h

@collections = response.documents.collect { |doc| ::Admin::CollectionPresenter.new(doc, media_object_count: counts[doc.id], unpublished_media_object_count: unpublished_counts[doc.id]) }.sort_by { |c| c.name.downcase }
end

# GET /collections
Expand Down
5 changes: 3 additions & 2 deletions app/presenters/admin/collection_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
class Admin::CollectionPresenter
attr_reader :document

def initialize(solr_doc)
def initialize(solr_doc, media_object_count: nil, unpublished_media_object_count: nil)
@document = solr_doc
@media_object_count = media_object_count
@unpublished_media_object_count = unpublished_media_object_count
end

delegate :id, to: :document
Expand All @@ -33,7 +35,6 @@ def description
document["description_tesim"]&.first
end

# TODO: do these counts in one large query for all collections on the index page to avoid having to do them here
def media_object_count
@media_object_count ||= MediaObject.where("collection_ssim" => name).count
end
Expand Down

0 comments on commit 1e96a12

Please sign in to comment.