Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally support cocina lookup #442

Merged
merged 2 commits into from Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-10-06 20:10:00 UTC using RuboCop version 0.92.0.
# on 2020-10-08 19:34:35 UTC using RuboCop version 0.92.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -14,7 +14,13 @@ Layout/ArgumentAlignment:
Exclude:
- 'spec/indexers/processable_indexer_spec.rb'

# Offense count: 9
# Offense count: 18
# Cop supports --auto-correct.
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
Lint/UnusedMethodArgument:
Enabled: false

# Offense count: 8
# Configuration parameters: IgnoredMethods.
Metrics/AbcSize:
Max: 35
Expand All @@ -24,7 +30,7 @@ Metrics/AbcSize:
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 5
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
Metrics/MethodLength:
Max: 26
Expand All @@ -45,10 +51,10 @@ Performance/CollectionLiteralInLoop:
RSpec/MultipleExpectations:
Max: 10

# Offense count: 25
# Offense count: 30
# Configuration parameters: AllowSubject.
RSpec/MultipleMemoizedHelpers:
Max: 11
Max: 12

# Offense count: 2
# Configuration parameters: IgnoreSharedExamples.
Expand Down Expand Up @@ -96,4 +102,4 @@ Style/FormatStringToken:
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 189
Max: 185
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -17,11 +17,11 @@ gem 'rack-timeout', '~> 0.5.1'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

gem 'dor-rights-auth', '~>1.5' # 1.5 or better is needed to ensure we index controlled digital lending rights object correctly
gem 'dor-services', '~> 9.0', '>= 9.2.1' # must be 9.2.1 to get fix from https://github.com/sul-dlss/dor-services/pull/695
gem 'dor-services-client', '~> 6.0'
gem 'dor-workflow-client', '~> 3.20'
gem 'dry-monads', '~> 1.3'
gem 'okcomputer' # for monitoring
gem 'rsolr', '~> 2.0'

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -177,6 +177,10 @@ GEM
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-monads (1.3.5)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer
dry-schema (1.5.4)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.8, >= 0.8.3)
Expand Down Expand Up @@ -479,6 +483,7 @@ DEPENDENCIES
dor-services (~> 9.0, >= 9.2.1)
dor-services-client (~> 6.0)
dor-workflow-client (~> 3.20)
dry-monads (~> 1.3)
erubis
faraday
honeybadger (~> 4.5)
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/dor_controller.rb
Expand Up @@ -2,6 +2,8 @@

# Main controller of application
class DorController < ApplicationController
include Dry::Monads[:result]

def reindex
@solr_doc = reindex_pid params[:pid], logger: generate_index_logger(request.uuid), add_attributes: { commitWithin: params.fetch(:commitWithin, 1000).to_i }
solr.commit unless params[:commitWithin] # reindex_pid doesn't commit, but callers of this method may expect the update to be committed immediately
Expand Down Expand Up @@ -39,15 +41,22 @@ def generate_index_logger(entry_id)
def reindex_pid(pid, logger:, add_attributes:)
obj = nil
solr_doc = nil
cocina = nil

# benchmark how long it takes to load the object
load_stats = Benchmark.measure('load_instance') do
obj = Dor.find pid
cocina = begin
Success(Dor::Services::Client.object(pid).find)
rescue StandardError => e
Honeybadger.notify("Unable to convert #{pid} to a cocina model. #{e.message}")
Failure(:conversion_error)
end
end.format('%n realtime %rs total CPU %ts').gsub(/[()]/, '')

# benchmark how long it takes to convert the object to a Solr document
to_solr_stats = Benchmark.measure('to_solr') do
indexer = Indexer.for(obj)
indexer = Indexer.for(obj, cocina: cocina)
solr_doc = indexer.to_solr
solr.add(solr_doc, add_attributes: add_attributes)
end.format('%n realtime %rs total CPU %ts').gsub(/[()]/, '')
Expand Down
2 changes: 1 addition & 1 deletion app/indexers/administrative_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class AdministrativeMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/administrative_tag_indexer.rb
Expand Up @@ -9,7 +9,7 @@ class AdministrativeTagIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
8 changes: 4 additions & 4 deletions app/indexers/composite_indexer.rb
Expand Up @@ -8,16 +8,16 @@ def initialize(*indexers)
@indexers = indexers
end

def new(resource:)
Instance.new(indexers, resource: resource)
def new(resource:, cocina:)
Instance.new(indexers, resource: resource, cocina: cocina)
end

class Instance
attr_reader :indexers, :resource

def initialize(indexers, resource:)
def initialize(indexers, resource:, cocina:)
@resource = resource
@indexers = indexers.map { |i| i.new(resource: resource) }
@indexers = indexers.map { |i| i.new(resource: resource, cocina: cocina) }
end

# @return [Hash] the merged solr document for all the sub-indexers
Expand Down
2 changes: 1 addition & 1 deletion app/indexers/content_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class ContentMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/data_indexer.rb
Expand Up @@ -6,7 +6,7 @@ class DataIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
39 changes: 39 additions & 0 deletions app/indexers/data_quality_indexer.rb
@@ -0,0 +1,39 @@
# frozen_string_literal: true

class DataQualityIndexer
attr_reader :resource, :cocina

def initialize(resource:, cocina:)
@resource = resource
@cocina = cocina
end

# @return [Hash] the partial solr document for identityMetadata
def to_solr
{ 'data_quality_ssim' => messages }
end

private

def messages
[source_id_message].compact.tap do |messages|
messages << 'Cocina conversion failed' if cocina.failure?
end
end

def source_id_message
if source_id.present?
'non-comformant sourceId' unless valid_source_id?
else
'missing sourceId'
end
end

def source_id
@source_id ||= resource.identityMetadata.sourceId
end

def valid_source_id?
/^.+:.+$/.match?(source_id)
end
end
2 changes: 1 addition & 1 deletion app/indexers/default_object_rights_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class DefaultObjectRightsDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/describable_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class DescribableIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/descriptive_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class DescriptiveMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/embargo_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class EmbargoMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/identifiable_indexer.rb
Expand Up @@ -22,7 +22,7 @@ class IdentifiableIndexer
}.freeze
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
3 changes: 1 addition & 2 deletions app/indexers/identity_metadata_datastream_indexer.rb
Expand Up @@ -5,7 +5,7 @@ class IdentityMetadataDatastreamIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand All @@ -21,7 +21,6 @@ def to_solr
plain_identifiers << id
ns_identifiers << source_id
solr_doc['source_id_ssim'] = [source_id]
solr_doc['source_id_errors_ssim'] = 'non-comformant' unless /^.+:.+$/.match?(source_id)
end

resource.identityMetadata.otherId.compact.each do |qid|
Expand Down
2 changes: 1 addition & 1 deletion app/indexers/object_profile_indexer.rb
Expand Up @@ -5,7 +5,7 @@ class ObjectProfileIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/processable_indexer.rb
Expand Up @@ -5,7 +5,7 @@ class ProcessableIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/provenance_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class ProvenanceMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/releasable_indexer.rb
Expand Up @@ -5,7 +5,7 @@ class ReleasableIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/rights_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class RightsMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/role_metadata_datastream_indexer.rb
Expand Up @@ -5,7 +5,7 @@ class RoleMetadataDatastreamIndexer

attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/version_metadata_datastream_indexer.rb
Expand Up @@ -3,7 +3,7 @@
class VersionMetadataDatastreamIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/workflows_indexer.rb
Expand Up @@ -4,7 +4,7 @@
class WorkflowsIndexer
attr_reader :resource

def initialize(resource:)
def initialize(resource:, cocina:)
@resource = resource
end

Expand Down
10 changes: 8 additions & 2 deletions app/services/indexer.rb
Expand Up @@ -2,6 +2,7 @@

class Indexer
ADMIN_POLICY_INDEXER = CompositeIndexer.new(
DataQualityIndexer,
AdministrativeTagIndexer,
DataIndexer,
RoleMetadataDatastreamIndexer,
Expand All @@ -20,6 +21,7 @@ class Indexer
)

COLLECTION_INDEXER = CompositeIndexer.new(
DataQualityIndexer,
AdministrativeTagIndexer,
DataIndexer,
ProvenanceMetadataDatastreamIndexer,
Expand All @@ -36,6 +38,7 @@ class Indexer
)

ITEM_INDEXER = CompositeIndexer.new(
DataQualityIndexer,
AdministrativeTagIndexer,
DataIndexer,
ProvenanceMetadataDatastreamIndexer,
Expand All @@ -54,6 +57,7 @@ class Indexer
)

SET_INDEXER = CompositeIndexer.new(
DataQualityIndexer,
AdministrativeTagIndexer,
DataIndexer,
ProvenanceMetadataDatastreamIndexer,
Expand All @@ -78,7 +82,9 @@ class Indexer
Dor::Set => SET_INDEXER
}.freeze

def self.for(obj)
INDEXERS.fetch(obj.class).new(resource: obj)
# @param [Dor::Abstract] obj
# @param [Dry::Monads::Result] cocina
def self.for(obj, cocina:)
INDEXERS.fetch(obj.class).new(resource: obj, cocina: cocina)
end
end