From 02dea16fe2faa744dac95b3ca2eaed73811cf49b Mon Sep 17 00:00:00 2001 From: leefaisonr Date: Wed, 16 Aug 2023 12:11:07 -0400 Subject: [PATCH 1/4] Adds ruby progress bar to subguide import Co-authored-by: Bess Sadler --- Gemfile | 1 + Gemfile.lock | 1 + app/services/sub_guide_loading_service.rb | 5 +++++ spec/services/sub_guide_loading_service_spec.rb | 7 ++++++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 3c928ebb..b302e596 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 17966e6a..6a3b88cc 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 2b8cbbf5..148ef0e8 100644 --- a/app/services/sub_guide_loading_service.rb +++ b/app/services/sub_guide_loading_service.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'csv' +require 'ruby-progressbar' # service for loading SubGuide cards data class SubGuideLoadingService attr_reader :csv_location @@ -20,6 +21,10 @@ def import puts 'task completed!' end +def progressbar + 100.times { progressbar.increment } +end + private def import_sub_guide_card(card) diff --git a/spec/services/sub_guide_loading_service_spec.rb b/spec/services/sub_guide_loading_service_spec.rb index 94cba120..0203edaa 100644 --- a/spec/services/sub_guide_loading_service_spec.rb +++ b/spec/services/sub_guide_loading_service_spec.rb @@ -18,7 +18,12 @@ expect(SubGuideCard.count).to eq 7 end - it 'displays progress status during import' do + xit 'displays progress status during import' do expect { sgls.import }.to output("#######task completed!\n").to_stdout end + + it 'displays ruby-progress bar during import' do + progressbar = ProgressBar.create + expect { sgls.import }.to output(progressbar.increment).to_stdout + end end From 65b87afe71307b002c23597d306dcc48498e4b87 Mon Sep 17 00:00:00 2001 From: leefaisonr Date: Wed, 16 Aug 2023 12:14:17 -0400 Subject: [PATCH 2/4] rubocop corrections --- app/services/sub_guide_loading_service.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/sub_guide_loading_service.rb b/app/services/sub_guide_loading_service.rb index 148ef0e8..3cb136e3 100644 --- a/app/services/sub_guide_loading_service.rb +++ b/app/services/sub_guide_loading_service.rb @@ -21,9 +21,9 @@ def import puts 'task completed!' end -def progressbar - 100.times { progressbar.increment } -end + def progressbar + 100.times { progressbar.increment } + end private From 80829092cb20ea331bba5620decdc6b601ece347 Mon Sep 17 00:00:00 2001 From: leefaisonr Date: Thu, 17 Aug 2023 13:42:20 -0400 Subject: [PATCH 3/4] Add progress bar implement for import task Co-authored-by: Bess Sadler Co-authored-by: Carolyn Cole Co-authored-by: Hector Correa Co-authored-by: James R. Griffin III --- app/services/sub_guide_loading_service.rb | 13 +++++-------- spec/services/sub_guide_loading_service_spec.rb | 3 +-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/app/services/sub_guide_loading_service.rb b/app/services/sub_guide_loading_service.rb index 3cb136e3..a7b47ac6 100644 --- a/app/services/sub_guide_loading_service.rb +++ b/app/services/sub_guide_loading_service.rb @@ -13,16 +13,13 @@ def initialize(csv_location: nil) 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 = ProgressBar.create + pb_increment = sub_guide_card_data.count / 100 + sub_guide_card_data.each_with_index do |entry, index| + byebug + 2.times { progressbar.increment } import_sub_guide_card(entry) end - puts 'task completed!' - end - - def progressbar - 100.times { progressbar.increment } end private diff --git a/spec/services/sub_guide_loading_service_spec.rb b/spec/services/sub_guide_loading_service_spec.rb index 0203edaa..57403670 100644 --- a/spec/services/sub_guide_loading_service_spec.rb +++ b/spec/services/sub_guide_loading_service_spec.rb @@ -23,7 +23,6 @@ end it 'displays ruby-progress bar during import' do - progressbar = ProgressBar.create - expect { sgls.import }.to output(progressbar.increment).to_stdout + expect { sgls.import }.to output(/Progress: /).to_stdout_from_any_process end end From 9c7a8ebc875ff9bd21f736f348adaec52a396aa1 Mon Sep 17 00:00:00 2001 From: leefaisonr Date: Thu, 17 Aug 2023 17:28:11 -0400 Subject: [PATCH 4/4] Added and colorfied ruby progress bar Co-authored-by: Jane Sandberg --- app/services/sub_guide_loading_service.rb | 16 ++++++++++------ spec/services/card_image_loading_service_spec.rb | 3 ++- spec/services/sub_guide_loading_service_spec.rb | 14 ++++++++------ spec/system/guide_cards_spec.rb | 3 ++- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/services/sub_guide_loading_service.rb b/app/services/sub_guide_loading_service.rb index a7b47ac6..2482eac9 100644 --- a/app/services/sub_guide_loading_service.rb +++ b/app/services/sub_guide_loading_service.rb @@ -4,24 +4,28 @@ 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) - progressbar = ProgressBar.create - pb_increment = sub_guide_card_data.count / 100 + @progressbar.total = sub_guide_card_data.count sub_guide_card_data.each_with_index do |entry, index| - byebug - 2.times { progressbar.increment } + progress_bar_random_color if (index % 100).zero? + @progressbar.increment import_sub_guide_card(entry) end end + def progress_bar_random_color + @progressbar.format = "%t: |\e[#{rand(91..97)}m%B\e[0m|" + end + private def import_sub_guide_card(card) diff --git a/spec/services/card_image_loading_service_spec.rb b/spec/services/card_image_loading_service_spec.rb index 8fd18a70..d0d1fcad 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 57403670..881ae891 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,11 +22,9 @@ expect(SubGuideCard.count).to eq 7 end - xit 'displays progress status during import' do - expect { sgls.import }.to output("#######task completed!\n").to_stdout - end - it 'displays ruby-progress bar during import' do - expect { sgls.import }.to output(/Progress: /).to_stdout_from_any_process + 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 ea769e7f..ccccae80 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