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

Ruby progress bar #124

Merged
merged 4 commits into from
Aug 17, 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ DEPENDENCIES
rails (~> 7.0.4)
rspec-rails
rubocop-rails
ruby-progressbar
selenium-webdriver
simplecov
simplecov-lcov (~> 0.8.0)
Expand Down
18 changes: 12 additions & 6 deletions app/services/sub_guide_loading_service.rb
Original file line number Diff line number Diff line change
@@ -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|"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁

end

private
Expand Down
3 changes: 2 additions & 1 deletion spec/services/card_image_loading_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 9 additions & 3 deletions spec/services/sub_guide_loading_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion spec/system/guide_cards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down