Skip to content

Commit

Permalink
Reporter#moab_detail_csv_list: even fewer queries to run
Browse files Browse the repository at this point in the history
now instead of querying for the CompleteMoabs for each PreservedObject, leverage the fact that the query already gets most of that info, with the exception of the name of the from_moab_storage_root for each CompleteMoab.  since there are only 10s of storage roots in prod, and since the names are very stable, it's much cheaper to look up each of those names once and cache the result.  we already have the current storage root name in the MoabStorageRoot ivar.  bonus:  easier to get at the column values we want by using #each_row to get a hash, and also no extraneous ActiveRecord objects to instantiate (downside: manual translation of status enum values)
  • Loading branch information
jmartin-sul committed Feb 12, 2020
1 parent 8f8d095 commit ef57337
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions app/services/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class Reporter
# @return [Reporter] the reporter
def initialize(params)
@storage_root = MoabStorageRoot.find_by!(name: params[:storage_root_name])
@msr_names = {}
end

def moab_storage_root_list_preserved_objects_relation
def moab_storage_root_list_pres_obj_comp_moabs_relation
PreservedObject
.joins(:complete_moabs)
.where(complete_moabs: { moab_storage_root: storage_root })
Expand All @@ -24,7 +25,7 @@ def moab_storage_root_list_preserved_objects_relation
# @return [Array] an array of druids on the storage root
def druid_csv_list
druid_array = [['druid']]
moab_storage_root_list_preserved_objects_relation
moab_storage_root_list_pres_obj_comp_moabs_relation
.select(:druid)
.each_row do |po_hash|
druid_array << [po_hash['druid']]
Expand All @@ -35,22 +36,36 @@ def druid_csv_list
# @param [Boolean] errors_only (default: false) - optionally only output lines with audit errors
# @return [Array] an array of hashes with details for each druid provided
def moab_detail_csv_list(errors_only: false)
query = if errors_only
moab_storage_root_list_preserved_objects_relation.where.not(complete_moabs: { status: 'ok' })
else
moab_storage_root_list_preserved_objects_relation
end

detail_array = [['druid', 'from_storage_root', 'storage_root', 'last_checksum_validation', 'last_moab_validation', 'status', 'status_details']]
query.each_instance do |preserved_object|
preserved_object.complete_moabs.each do |cm|
detail_array << [
preserved_object.druid, cm.from_moab_storage_root&.name, cm.moab_storage_root.name,
cm.last_checksum_validation, cm.last_moab_validation, cm.status, cm.status_details
]
query =
if errors_only
moab_storage_root_list_pres_obj_comp_moabs_relation.where.not(complete_moabs: { status: 'ok' })
else
moab_storage_root_list_pres_obj_comp_moabs_relation
end

header_row = [['druid', 'from_storage_root', 'storage_root', 'last_checksum_validation', 'last_moab_validation', 'status', 'status_details']]

# cols doesn't include storage_root name (available via storage_root ivar). also we're not instantiating AR objects, so we have to translate
# the underlying status enum
cols = ['druid', 'from_moab_storage_root_id', 'last_checksum_validation', 'last_moab_validation', 'status AS status_code', 'status_details']
data_rows = query.select(cols).each_row.map do |po_cm_hash|
[
po_cm_hash['druid'], moab_storage_root_name(po_cm_hash['from_moab_storage_root_id']), storage_root.name,
po_cm_hash['last_checksum_validation'], po_cm_hash['last_moab_validation'], status_text_from_code(po_cm_hash['status_code']),
po_cm_hash['status_details']
]
end
detail_array
header_row + data_rows
end

def moab_storage_root_name(msr_id)
return nil unless msr_id.present?
@msr_names[msr_id] ||= MoabStorageRoot.find(msr_id).name
end

def status_text_from_code(status_code)
# statuses is a hash of the form { 'status_text' => status_code }. this is just a reverse hash lookup.
CompleteMoab.statuses.to_a.find { |s| s[1] == status_code }[0]
end

# @param [Array] lines - values to output on each line of the csv
Expand Down

0 comments on commit ef57337

Please sign in to comment.