Skip to content

Commit

Permalink
Reindex the entire collection when you hit 'reindex
Browse files Browse the repository at this point in the history
  • Loading branch information
tpendragon committed Nov 17, 2016
1 parent ff769b4 commit 6ebe7b2
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 27 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Rails/TimeZone:
Rails/DynamicFindBy:
Exclude:
- 'app/controllers/exhibits_controller.rb'
- 'app/models/exhibit_proxy.rb'
- 'app/decorators/applies_title_from_slug.rb'

RSpec/ExampleWording:
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/exhibits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class ExhibitsController < Spotlight::ExhibitsController

def ingest_members
return unless @exhibit.persisted?
collection_manifest = CollectionManifest.find_by_slug(@exhibit.slug)
members = collection_manifest.manifests.map { |x| x['@id'] }
IIIFIngestJob.new.perform members, @exhibit
ExhibitProxy.new(@exhibit).reindex
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/iiif_ingest_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ def perform(urls, exhibit)

# Ingest a single IIIF manifest URL as a resource.
def ingest(url, exhibit)
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.find_or_initialize_by(url: url, exhibit_id: exhibit.id).save_and_index
end
end
27 changes: 27 additions & 0 deletions app/jobs/spotlight/reindex_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Spotlight
##
# Reindex the given resources or exhibits
class ReindexJob < ActiveJob::Base
queue_as :default

before_enqueue do |job|
resource_list(job.arguments.first).each(&:waiting!)
end

def perform(exhibit_or_resources)
resource_list(exhibit_or_resources).each(&:reindex)
end

private

def resource_list(exhibit_or_resources)
if exhibit_or_resources.is_a?(Spotlight::Exhibit)
[ExhibitProxy.new(exhibit_or_resources)]
elsif exhibit_or_resources.is_a?(Enumerable)
exhibit_or_resources
else
Array(exhibit_or_resources)
end
end
end
end
21 changes: 21 additions & 0 deletions app/models/exhibit_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ExhibitProxy
attr_reader :exhibit
def initialize(exhibit)
@exhibit = exhibit
end

def reindex
IIIFIngestJob.perform_now members, exhibit
end

def collection_manifest
CollectionManifest.find_by_slug(exhibit.slug)
end

def members
collection_manifest.manifests.map { |x| x['@id'] }
end

def waiting!
end
end
6 changes: 0 additions & 6 deletions app/models/iiif_resource.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
class IIIFResource < Spotlight::Resources::IiifHarvester
belongs_to :exhibit, class_name: 'Spotlight::Exhibit'

def initialize(manifest_url: nil, exhibit: nil)
super()
self.url = manifest_url
self.exhibit_id = exhibit.id if exhibit
end
end
2 changes: 1 addition & 1 deletion app/services/plum_event_processor/create_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class PlumEventProcessor
class CreateProcessor < Processor
def process
exhibits.map do |exhibit|
resource = IIIFResource.new(manifest_url: manifest_url, exhibit: exhibit)
resource = IIIFResource.new(url: manifest_url, exhibit: exhibit)
resource.save_and_index
end.all?(&:present?)
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/plum_event_processor/update_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_existing_resources

def create_new_resources
new_exhibits.each do |exhibit|
IIIFResource.new(manifest_url: manifest_url, exhibit: exhibit).save_and_index
IIIFResource.new(url: manifest_url, exhibit: exhibit).save_and_index
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:url) { "https://hydra-dev.princeton.edu/concern/multi_volume_works/f4752g76q/manifest" }
it "hides scanned resources with parents" do
exhibit = Spotlight::Exhibit.create title: 'Exhibit A'
resource = IIIFResource.new manifest_url: url, exhibit: exhibit
resource = IIIFResource.new url: url, exhibit: exhibit
expect(resource.save_and_index).to be_truthy

get :index, params: { q: "", exhibit_id: exhibit.id }
Expand All @@ -14,7 +14,7 @@
end
it "returns MVW from metadata found in volume" do
exhibit = Spotlight::Exhibit.create title: 'Exhibit A'
resource = IIIFResource.new manifest_url: url, exhibit: exhibit
resource = IIIFResource.new url: url, exhibit: exhibit
expect(resource.save_and_index).to be_truthy

get :index, params: { q: "SR1", exhibit_id: exhibit.id }
Expand Down
8 changes: 4 additions & 4 deletions spec/jobs/iiif_ingest_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
let(:url1) { 'http://example.com/1/manifest' }
let(:url2) { 'http://example.com/2/manifest' }
let(:exhibit) { Spotlight::Exhibit.new }
let(:resource) { IIIFResource.new manifest_url: nil, exhibit: exhibit }
let(:resource) { IIIFResource.new url: nil, exhibit: exhibit }

before do
allow(exhibit).to receive(:id).and_return('exhibit1')
allow(resource).to receive(:save_and_index)
end

it 'ingests a single url' do
expect(IIIFResource).to receive(:new).with(manifest_url: url1, exhibit: exhibit).and_return(resource)
expect(IIIFResource).to receive(:new).with(url: url1, exhibit_id: exhibit.id).and_return(resource)

described_class.new.perform(url1, exhibit)
end

it 'ingests each of an array of urls' do
expect(IIIFResource).to receive(:new).with(manifest_url: url1, exhibit: exhibit).and_return(resource)
expect(IIIFResource).to receive(:new).with(manifest_url: url2, exhibit: exhibit).and_return(resource)
expect(IIIFResource).to receive(:new).with(url: url1, exhibit_id: exhibit.id).and_return(resource)
expect(IIIFResource).to receive(:new).with(url: url2, exhibit_id: exhibit.id).and_return(resource)

described_class.new.perform([url1, url2], exhibit)
end
Expand Down
31 changes: 31 additions & 0 deletions spec/jobs/spotlight/reindex_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.describe Spotlight::ReindexJob do
let(:url1) { 'http://example.com/1/manifest' }
let(:exhibit) { Spotlight::Exhibit.new }
let(:resource) { IIIFResource.new url: nil, exhibit: exhibit }
let(:manifest) { object_double(CollectionManifest.new, manifests: [{ "@id" => url1 }]) }

before do
allow(exhibit).to receive(:id).and_return('exhibit1')
allow(CollectionManifest).to receive(:find_by_slug).and_return(manifest)
allow(resource).to receive(:save_and_index)
end

it 'reindexes an exhibit' do
allow(IIIFResource).to receive(:new).and_return(resource)

described_class.perform_now(exhibit)

expect(IIIFResource).to have_received(:new).with(url: url1, exhibit_id: exhibit.id)
end

it 'can reindex multiple IIIF Resources' do
resources = [instance_double(IIIFResource, reindex: true), instance_double(IIIFResource, reindex: true)]

described_class.perform_now(resources)

expect(resources.first).to have_received(:reindex)
expect(resources.last).to have_received(:reindex)
end
end
6 changes: 3 additions & 3 deletions spec/models/iiif_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:url) { 'https://hydra-dev.princeton.edu/concern/scanned_resources/1r66j1149/manifest' }
it 'ingests a iiif manifest' do
exhibit = Spotlight::Exhibit.create title: 'Exhibit A'
resource = described_class.new manifest_url: url, exhibit: exhibit
resource = described_class.new url: url, exhibit: exhibit
expect(resource.save).to be true

solr_doc = nil
Expand All @@ -18,7 +18,7 @@
let(:url) { "https://hydra-dev.princeton.edu/concern/multi_volume_works/f4752g76q/manifest" }
it "ingests both items as individual solr records, marking the child" do
exhibit = Spotlight::Exhibit.create title: 'Exhibit A'
resource = described_class.new manifest_url: url, exhibit: exhibit
resource = described_class.new url: url, exhibit: exhibit
expect(resource.save_and_index).to be_truthy

docs = Blacklight.default_index.connection.get("select", params: { q: "*:*" })["response"]["docs"]
Expand All @@ -33,7 +33,7 @@
let(:url) { "https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest" }
it "ingests a iiif manifest using the metadata pool, excludes range labels when missing" do
exhibit = Spotlight::Exhibit.create title: 'Exhibit A'
resource = described_class.new manifest_url: url, exhibit: exhibit
resource = described_class.new url: url, exhibit: exhibit
expect(resource.save_and_index).to be_truthy
docs = Blacklight.default_index.connection.get("select", params: { q: "*:*" })["response"]["docs"]
scanned_resource_doc = docs.find { |x| x["full_title_ssim"] == ["Christopher and his kind, 1929-1939"] }
Expand Down
12 changes: 6 additions & 6 deletions spec/services/plum_event_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
end
it "deletes that resource" do
exhibit = FactoryGirl.create(:exhibit, slug: "first")
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.new(url: url, exhibit: exhibit).save_and_index

expect(processor.process).to eq true

Expand All @@ -51,7 +51,7 @@
let(:type) { "UPDATED" }
it "updates that resource" do
exhibit = FactoryGirl.create(:exhibit, slug: "first")
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.new(url: url, exhibit: exhibit).save_and_index

expect(processor.process).to eq true
resource = Blacklight.default_index.connection.get("select", params: { q: "*:*" })["response"]["docs"].first
Expand All @@ -61,7 +61,7 @@
context "when it's no longer accessible" do
it "marks it as non-public" do
exhibit = FactoryGirl.create(:exhibit, slug: "first")
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.new(url: url, exhibit: exhibit).save_and_index

# swap casseette to make the resource inaccessible
VCR.use_cassette('plum_events_no_permission') do
Expand All @@ -75,7 +75,7 @@
context "when it's private and then is made accessible" do
it "marks it as public" do
exhibit = FactoryGirl.create(:exhibit, slug: "first")
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.new(url: url, exhibit: exhibit).save_and_index
resource_id = Blacklight.default_index.connection.get("select", params: { q: "*:*" })["response"]["docs"].first["id"]
document = SolrDocument.find(resource_id)
document.make_private!(exhibit)
Expand All @@ -93,7 +93,7 @@
let(:collection_slugs) { [] }
it "removes old ones" do
exhibit = FactoryGirl.create(:exhibit, slug: "first")
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.new(url: url, exhibit: exhibit).save_and_index

expect(processor.process).to eq true

Expand All @@ -106,7 +106,7 @@
it "moves it to a new one" do
exhibit = FactoryGirl.create(:exhibit, slug: "first")
FactoryGirl.create(:exhibit, slug: "banana")
IIIFResource.new(manifest_url: url, exhibit: exhibit).save_and_index
IIIFResource.new(url: url, exhibit: exhibit).save_and_index

expect(processor.process).to eq true

Expand Down

0 comments on commit 6ebe7b2

Please sign in to comment.