Skip to content

Commit

Permalink
Added and colorfied ruby progress bar
Browse files Browse the repository at this point in the history
Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>
  • Loading branch information
leefaisonr and sandbergja committed Aug 17, 2023
1 parent 8082909 commit 9c7a8eb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
16 changes: 10 additions & 6 deletions app/services/sub_guide_loading_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
14 changes: 8 additions & 6 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,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
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

0 comments on commit 9c7a8eb

Please sign in to comment.