Skip to content

Commit

Permalink
refine scripts that help explore data
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Mar 3, 2020
1 parent 0236f3b commit 7cd49f7
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 17 deletions.
91 changes: 91 additions & 0 deletions scripts/exhibit_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,42 @@
# Run rails console: bundle exec rails c <RAILS_ENV>
# Load the script: load 'scripts/exhibit_service.rb'
# List resources with null image: ExhibitService.database_entries _ID_
load 'scripts/thumbnail_service.rb'

class ExhibitService
class << self
def services
puts 'database_entries'
puts ' # @param [Integer] exhibit id'
puts 'counts'
puts ' # @param [Integer] exhibit id'
end

# @param [Integer] exhibit id
def counts(id)
puts "====================================================="
begin
exhibit = Spotlight::Exhibit.find(id)
puts "Database entries related to exhibit #{id} -- title: #{exhibit.title} slug: #{exhibit.slug} featured_image: #{exhibit.featured_image} masthead: #{exhibit.masthead_id} thumbnail: #{exhibit.thumbnail_id}"
rescue ActiveRecord::RecordNotFound => e
puts "EXHIBIT DOES NOT EXIST: #{id}"
puts "Looking for related database entries that may be hanging around"
end
puts "====================================================="
count_resources(id)
count_solr_document_sidecars(id)
count_searches(id)
count_filters(id)
count_main_navigations(id)
count_pages(id)
count_attachments(id)
count_blacklight_configurations(id)
count_contact_emails(id)
count_contacts(id)
count_custom_fields(id)
count_reindexing_log_entries(id)
count_slugs(id)
puts "====================================================="
end

# @param [Integer] exhibit id
Expand All @@ -35,10 +65,15 @@ def database_entries(id)
list_contacts(id)
list_custom_fields(id)
list_reindexing_log_entries(id)
list_slugs(id)
end

private

def count_attachments exhibit_id
puts "Attachments: #{Spotlight::Attachment.where(exhibit_id: exhibit_id).count}"
end

def list_attachments exhibit_id
puts 'Attachments:'
Spotlight::Attachment.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -47,6 +82,10 @@ def list_attachments exhibit_id
puts '------------'
end

def count_blacklight_configurations exhibit_id
puts "Blacklight Configurations: #{Spotlight::BlacklightConfiguration.where(exhibit_id: exhibit_id).count}"
end

def list_blacklight_configurations exhibit_id
puts 'Blacklight Configurations:'
Spotlight::BlacklightConfiguration.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -55,6 +94,10 @@ def list_blacklight_configurations exhibit_id
puts '------------'
end

def count_contact_emails exhibit_id
puts "Contact Emails: #{Spotlight::ContactEmail.where(exhibit_id: exhibit_id).count}"
end

def list_contact_emails exhibit_id
puts 'Contact Emails:'
Spotlight::ContactEmail.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -63,6 +106,10 @@ def list_contact_emails exhibit_id
puts '------------'
end

def count_contacts exhibit_id
puts "Contacts: #{Spotlight::Contact.where(exhibit_id: exhibit_id).count}"
end

def list_contacts exhibit_id
puts 'Contacts:'
Spotlight::Contact.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -71,6 +118,10 @@ def list_contacts exhibit_id
puts '------------'
end

def count_custom_fields exhibit_id
puts "Custom Fields: #{Spotlight::CustomField.where(exhibit_id: exhibit_id).count}"
end

def list_custom_fields exhibit_id
puts 'Custom Fields:'
Spotlight::CustomField.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -79,6 +130,10 @@ def list_custom_fields exhibit_id
puts '------------'
end

def count_filters exhibit_id
puts "Filters: #{Spotlight::Filter.where(exhibit_id: exhibit_id).count}"
end

def list_filters exhibit_id
puts 'Filters:'
Spotlight::Filter.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -87,6 +142,10 @@ def list_filters exhibit_id
puts '------------'
end

def count_main_navigations exhibit_id
puts "Main Navigation: #{Spotlight::MainNavigation.where(exhibit_id: exhibit_id).count}"
end

def list_main_navigations exhibit_id
puts 'Main Navigation:'
Spotlight::MainNavigation.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -95,6 +154,10 @@ def list_main_navigations exhibit_id
puts '------------'
end

def count_pages exhibit_id
puts "Pages: #{Spotlight::Page.where(exhibit_id: exhibit_id).count}"
end

def list_pages exhibit_id
puts 'Pages:'
Spotlight::Page.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -103,6 +166,10 @@ def list_pages exhibit_id
puts '------------'
end

def count_reindexing_log_entries exhibit_id
puts "Reindexing Log Entries: #{Spotlight::ReindexingLogEntry.where(exhibit_id: exhibit_id).count}"
end

def list_reindexing_log_entries exhibit_id
puts 'Reindexing Log Entries:'
Spotlight::ReindexingLogEntry.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -111,6 +178,10 @@ def list_reindexing_log_entries exhibit_id
puts '------------'
end

def count_resources exhibit_id
puts "Resources: #{Spotlight::Resource.where(exhibit_id: exhibit_id).count}"
end

def list_resources exhibit_id
puts 'Resources:'
Spotlight::Resource.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -126,6 +197,10 @@ def list_resources exhibit_id
puts '------------'
end

def count_searches exhibit_id
puts "Searches: #{Spotlight::Search.where(exhibit_id: exhibit_id).count}"
end

def list_searches exhibit_id
puts 'Searches:'
Spotlight::Search.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -134,6 +209,10 @@ def list_searches exhibit_id
puts '------------'
end

def count_solr_document_sidecars exhibit_id
puts "Solr Document Sidecars: #{Spotlight::SolrDocumentSidecar.where(exhibit_id: exhibit_id).count}"
end

def list_solr_document_sidecars exhibit_id
puts 'Solr Document Sidecars:'
Spotlight::SolrDocumentSidecar.where(exhibit_id: exhibit_id).each do |r|
Expand All @@ -146,5 +225,17 @@ def list_solr_document_sidecars exhibit_id
end
puts '------------'
end

def count_slugs exhibit_id
puts "Friendly ID Slugs: #{FriendlyId::Slug.where(scope: "exhibit_id:#{exhibit_id}").count}"
end

def list_slugs exhibit_id
puts 'Friendly ID Slugs:'
FriendlyId::Slug.where(scope: "exhibit_id:#{exhibit_id}").each do |slug|
puts " exhibit: #{slug.scope} id: #{slug.id} slug: #{slug.slug} type: #{slug.sluggable_type}"
end
puts '------------'
end
end
end
63 changes: 50 additions & 13 deletions scripts/featured_image_delete_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# upload and has NULL image:
# Run rails console: bundle exec rails c <RAILS_ENV>
# Load the script: load 'scripts/featured_image_delete_service.rb'
# List featured images with null image: ids = FeaturedImageDeleteService.find_null_images
# List featured images with null image: ids = FeaturedImageDeleteService.list_null_images
# Delete all featured images with null image: FeaturedImageDeleteService.delete_featured_images(ids)
load 'scripts/thumbnail_service.rb'

class FeaturedImageDeleteService
class << self
def services
puts 'find_null_images(limit = 1000)'
puts 'list_null_images(limit = 1000)'
puts ' # @param [Integer] limit the number of image ids returned (default: 1000)'
puts ' # @return [Array<Integer>] featured_image_ids - list of ids of featured images with image=NULL'
puts 'count_null_images'
Expand All @@ -27,16 +28,16 @@ def services

# @param [Integer] limit the number of image ids returned (default: 1000)
# @return [Array<Integer>] featured_image_ids - list of ids of featured images with image=NULL
def find_null_images(limit = 1000)
def list_null_images(limit: 1000, include_thumbnails: false)
puts "Featured Images where image is NULL"
featured_image_ids = Spotlight::FeaturedImage.where(image: nil).limit(limit).map(&:id)
featured_image_ids = find_null_images(include_thumbnails: include_thumbnails, limit: limit)
delete_featured_images(featured_image_ids, pretest: true)
featured_image_ids
end

# @return [Integer] the number of featured images with image=NULL'
def count_null_images
ids = find_null_images
ids = list_null_images
ids.count >= 1000 ? puts('> 1000') : puts(ids.count)
end

Expand All @@ -54,40 +55,72 @@ def delete_featured_image(featured_image_id, auto_delete: false, pretest: false)
featured_image = find_featured_image(featured_image_id)
return if featured_image.blank?

thumbnails = find_thumbnails(featured_image)
resources = find_resources(featured_image_id)
solr_document_sidecars = find_solr_document_sidecars(resources)
bookmarks = find_bookmarks(solr_document_sidecars)

perform_pretest(featured_image, resources, solr_document_sidecars, bookmarks)
perform_pretest(featured_image: featured_image, thumbnails: thumbnails, resources: resources,
solr_document_sidecars: solr_document_sidecars, bookmarks: bookmarks)
return if pretest
return unless delete?(featured_image, resources, auto_delete)
perform_deletes(featured_image, resources, solr_document_sidecars, bookmarks)
return unless delete?(featured_image, resources, thumbnails, auto_delete)
perform_deletes(featured_image: featured_image, thumbnails: thumbnails, resources: resources,
solr_document_sidecars: solr_document_sidecars, bookmarks: bookmarks)
end

private

def perform_deletes(featured_image, resources, solr_document_sidecars, bookmarks)
def perform_deletes(featured_image:, thumbnails: [], resources: [], solr_document_sidecars: [], bookmarks: [])
bookmarks.each { |b| b.delete }
solr_document_sidecars.each do |d|
Blacklight.default_index.connection.delete_by_id d.document_id
Blacklight.default_index.connection.commit
d.delete
end
resources.each { |r| r.delete }
thumbnails.each { |t| t.delete }
featured_image.remove_image!
featured_image.delete
puts("DELETE Complete!")
end

def perform_pretest(featured_image, resources, solr_document_sidecars, bookmarks)
def perform_pretest(featured_image:, thumbnails: [], resources: [], solr_document_sidecars: [], bookmarks: [])
puts "-----------------------------------"
puts("Will DELETE...")
puts(" featured_image - id: #{featured_image.id} type: #{featured_image.type} image: #{featured_image.image}")
thumbnail_text = test_for_thumbnail(featured_image)
puts(" featured_image - id: #{featured_image.id} type: #{featured_image.type} image: #{featured_image.image} iiif_tilesource: #{featured_image.iiif_tilesource} #{thumbnail_text}")
thumbnails.each { |t| puts(" thumbnail - id: #{t.id} type: #{featured_image.type} image: #{featured_image.image} iiif_tilesource: #{t.iiif_tilesource}") }
resources.each { |r| puts(" resource - id: #{r.id} type: #{r.type} upload_id: #{r.upload_id} exhibit_id: #{r.exhibit.id}") }
solr_document_sidecars.each { |d| puts(" solr_document_sidecar - id: #{d.id} document_id: #{d.document_id} document_type: #{d.document_type} resource_id: #{d.resource_id} exhibit_id: #{d.exhibit_id}") }
bookmarks.each { |b| puts(" bookmark - id: #{b.id}") }
end

def test_for_thumbnail(featured_image)
return '' unless ThumbnailService.thumbnail? featured_image
"THUMBNAIL from source featured_image: #{ThumbnailService.thumbnail_source_id(featured_image)}"
end

# def thumbnail?(featured_image)
# !featured_image.iiif_tilesource.include? featured_image.id.to_s
# end
#
# def thumbnail_source_id(featured_image)
# source_id = featured_image.iiif_tilesource.scan(/.*\/images\/(.*)\/info.json/).flatten.first
# end

def find_null_images(include_thumbnails: false, limit: 1000)
null_images = Spotlight::FeaturedImage.where(image: nil).limit(limit)
return null_images.map(&:id) if include_thumbnails
non_thumbnail_null_images = null_images.to_a.delete_if do |nfi|
next unless ThumbnailService.thumbnail?(nfi)
source_id = ThumbnailService.thumbnail_source_id(nfi)
next if source_id.blank?
source_image = Spotlight::FeaturedImage.find(source_id)
!source_image.image.nil?
end
non_thumbnail_null_images.map(&:id)
end

def find_featured_image(featured_image_id)
featured_image = Spotlight::FeaturedImage.find(featured_image_id)
featured_image
Expand All @@ -96,6 +129,10 @@ def find_featured_image(featured_image_id)
puts("featured_image NOT FOUND - id: #{featured_image_id}") if featured_image.blank?
end

def find_thumbnails(featured_image)
ThumbnailService.related_thumbnails(featured_image)
end

def find_resources(featured_image_id)
resources = Spotlight::Resource.where(upload_id: featured_image_id)
return [] if resources.blank?
Expand Down Expand Up @@ -126,8 +163,8 @@ def find_bookmarks(solr_document_sidecars)
all_bookmarks
end

def delete? featured_image, resources, auto_delete
return true if auto_delete && resources.blank? && featured_image.type.nil?
def delete? featured_image, resources, thumbnails, auto_delete
return true if auto_delete && resources.blank? && featured_image.type.nil? && thumbnails.blank?
puts("Do you want to perform the deletes? [Yes, no]")
gets.chomp == 'Yes'
end
Expand Down
Loading

0 comments on commit 7cd49f7

Please sign in to comment.