diff --git a/Gemfile b/Gemfile index 3c928eb..b302e59 100644 --- a/Gemfile +++ b/Gemfile @@ -72,6 +72,7 @@ group :development, :test do gem 'pry-byebug' gem 'rspec-rails' gem 'rubocop-rails', require: false + gem 'ruby-progressbar' gem 'selenium-webdriver' gem 'simplecov' gem 'simplecov-lcov', '~> 0.8.0' diff --git a/Gemfile.lock b/Gemfile.lock index 17966e6..6a3b88c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -349,6 +349,7 @@ DEPENDENCIES rails (~> 7.0.4) rspec-rails rubocop-rails + ruby-progressbar selenium-webdriver simplecov simplecov-lcov (~> 0.8.0) diff --git a/app/services/sub_guide_loading_service.rb b/app/services/sub_guide_loading_service.rb index 2b8cbbf..2482eac 100644 --- a/app/services/sub_guide_loading_service.rb +++ b/app/services/sub_guide_loading_service.rb @@ -1,23 +1,29 @@ # frozen_string_literal: true require 'csv' +require 'ruby-progressbar' # service for loading SubGuide cards data class SubGuideLoadingService - attr_reader :csv_location + attr_reader :csv_location, :progressbar # @param csv_location [String] location of source data for SubGuideCards - def initialize(csv_location: nil) + def initialize(csv_location: nil, progressbar: nil) @csv_location = csv_location || Rails.root.join('data', 'dbo-subguides', 'dbo.Subguides.17917.csv') + @progressbar = progressbar || ProgressBar.create(format: "\e[1;35m%t: |%B|\e[0m") end def import sub_guide_card_data = CSV.parse(File.read(csv_location), headers: true, liberal_parsing: true) - sub_guide_card_data.each do |entry| - print '#' - $stdout.flush + @progressbar.total = sub_guide_card_data.count + sub_guide_card_data.each_with_index do |entry, index| + progress_bar_random_color if (index % 100).zero? + @progressbar.increment import_sub_guide_card(entry) end - puts 'task completed!' + end + + def progress_bar_random_color + @progressbar.format = "%t: |\e[#{rand(91..97)}m%B\e[0m|" end private diff --git a/spec/services/card_image_loading_service_spec.rb b/spec/services/card_image_loading_service_spec.rb index 8fd18a7..d0d1fca 100644 --- a/spec/services/card_image_loading_service_spec.rb +++ b/spec/services/card_image_loading_service_spec.rb @@ -5,7 +5,8 @@ describe CardImageLoadingService do let(:cils) { described_class.new } let(:sgls) do - SubGuideLoadingService.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv')) + SubGuideLoadingService.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv'), + progressbar: ProgressBar.create(output: ProgressBar::Outputs::Null)) end let(:s3_response) do "2023-07-19 14:39:38 3422 imagecat-disk9-0091-A3037-1358.0110.tif\n2023-07-19 14:39:38 7010 imagecat-disk9-0091-A3037-1358.0111.tif\n" diff --git a/spec/services/sub_guide_loading_service_spec.rb b/spec/services/sub_guide_loading_service_spec.rb index 94cba12..881ae89 100644 --- a/spec/services/sub_guide_loading_service_spec.rb +++ b/spec/services/sub_guide_loading_service_spec.rb @@ -1,9 +1,13 @@ # frozen_string_literal: true require 'rails_helper' +require 'ruby-progressbar/outputs/null' describe SubGuideLoadingService do - let(:sgls) { described_class.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv')) } + let(:sgls) do + described_class.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv'), + progressbar: ProgressBar.create(output: ProgressBar::Outputs::Null)) + end it 'can instantiate' do expect(sgls).to be_instance_of described_class end @@ -18,7 +22,9 @@ expect(SubGuideCard.count).to eq 7 end - it 'displays progress status during import' do - expect { sgls.import }.to output("#######task completed!\n").to_stdout + it 'displays ruby-progress bar during import' do + expect(sgls.progressbar.to_h['percentage']).to eq 0 + sgls.import + expect(sgls.progressbar.to_h['percentage']).to eq 100 end end diff --git a/spec/system/guide_cards_spec.rb b/spec/system/guide_cards_spec.rb index ea769e7..ccccae8 100644 --- a/spec/system/guide_cards_spec.rb +++ b/spec/system/guide_cards_spec.rb @@ -7,7 +7,8 @@ let(:subguide_card_fixture) { Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv') } before do GuideCardLoadingService.new(csv_location: guide_card_fixture).import - SubGuideLoadingService.new(csv_location: subguide_card_fixture).import + SubGuideLoadingService.new(csv_location: subguide_card_fixture, + progressbar: ProgressBar.create(output: ProgressBar::Outputs::Null)).import end describe 'GuideCards index page' do