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

Card image ruby progress bar #131

Merged
merged 3 commits into from
Aug 24, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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