Skip to content

Commit

Permalink
Merge pull request #131 from pulibrary/CardImage-Ruby-ProgressBar
Browse files Browse the repository at this point in the history
Card image ruby progress bar
  • Loading branch information
sandbergja committed Aug 24, 2023
2 parents c214df8 + 9898ae3 commit 5b71b89
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
37 changes: 27 additions & 10 deletions app/services/card_image_loading_service.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
# frozen_string_literal: true

require 'ruby-progressbar'

# Class for card image loading service
class CardImageLoadingService
attr_reader :progressbar

def initialize(progressbar: nil)
@progressbar = progressbar || ProgressBar.create(format: "\e[1;35m%t: |%B|\e[0m")
end

# For each SubGuideCard, take its path and query s3 to get all of the image names
# for that path. For each image file, create a CardImage object with the path and
# image name.

def import
SubGuideCard.all.each do |sgc|
# Get an array of all file names on s3 that start that sgc.path
# For each file name
@progressbar.total = SubGuideCard.all.count
SubGuideCard.all.each_with_index do |sgc, index|
progress_bar_random_color if (index % 100).zero?
@progressbar.increment
image_array(sgc.path).each do |file_name|
ci = CardImage.new
ci.path = sgc.path
ci.image_name = file_name
ci.save
print '#'
$stdout.flush
create_card_image(sgc, file_name)
end
end
puts 'task completed!'
end

# returns something like
Expand All @@ -33,4 +37,17 @@ def s3_image_list(path)
s3_query = "aws s3 ls s3://puliiif-production/imagecat-disk#{path.tr('/', '-')}"
`#{s3_query}`
end

private

def create_card_image(sgc, file_name)
ci = CardImage.new
ci.path = sgc.path
ci.image_name = file_name
ci.save
end

def progress_bar_random_color
@progressbar.format = "%t: |\e[#{rand(91..97)}m%B\e[0m|"
end
end
9 changes: 8 additions & 1 deletion spec/services/card_image_loading_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

require 'rails_helper'
require 'ruby-progressbar/outputs/null'

describe CardImageLoadingService do
let(:cils) { described_class.new }
let(:cils) { described_class.new(progressbar: ProgressBar.create(output: ProgressBar::Outputs::Null)) }
let(:sgls) do
SubGuideLoadingService.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv'),
progressbar: ProgressBar.create(output: ProgressBar::Outputs::Null))
Expand Down Expand Up @@ -33,4 +34,10 @@
image_list = cils.s3_image_list('9/0091/A3037')
expect(image_list.split("\n").count).to eq 2
end

it 'displays ruby-progress bar during import' do
expect(cils.progressbar.to_h['percentage']).to eq 0
cils.import
expect(cils.progressbar.to_h['percentage']).to eq 100
end
end

0 comments on commit 5b71b89

Please sign in to comment.