|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Compare SDR versions against Preservation versions |
| 4 | +# https://github.com/sul-dlss/dor-services-app/issues/5074 |
| 5 | +# Invoke via: |
| 6 | +# bin/rails r -e production "PreservationVersions.report" |
| 7 | +class PreservationVersions |
| 8 | + def self.report |
| 9 | + new.report |
| 10 | + end |
| 11 | + |
| 12 | + def report |
| 13 | + # Objects with a closed version, indicating they have been accessioned. |
| 14 | + data = RepositoryObject.joins(:head_version).where.not(last_closed_version: nil).pluck(:external_identifier, :version) |
| 15 | + |
| 16 | + progress_bar = tty_progress_bar(data.length) |
| 17 | + progress_bar.start |
| 18 | + |
| 19 | + results = Parallel.map(data.each_slice(2500), |
| 20 | + in_processes: 3, |
| 21 | + finish: ->(finish_data, _, _) { progress_bar.advance(finish_data.length) }) do |data_slice| |
| 22 | + data_slice.map do |druid, version| |
| 23 | + begin |
| 24 | + preservation_version = Preservation::Client.objects.current_version(druid) |
| 25 | + rescue Preservation::Client::NotFoundError |
| 26 | + preservation_version = nil |
| 27 | + end |
| 28 | + preservation_version == version ? nil : [druid, version, preservation_version] |
| 29 | + end |
| 30 | + end.flatten(1).compact |
| 31 | + |
| 32 | + CSV.open('preservation_versions_report.csv', 'w') do |csv| |
| 33 | + csv << %w[druid version preservation_version] |
| 34 | + results.each { |result| csv << result } |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + def tty_progress_bar(count) |
| 39 | + TTY::ProgressBar.new( |
| 40 | + '[:bar] (:percent (:current/:total), rate: :rate/s, mean rate: :mean_rate/s, :elapsed total, ETA: :eta_time)', |
| 41 | + bar_format: :box, |
| 42 | + advance: num_for_progress_advance(count), |
| 43 | + total: count |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + def num_for_progress_advance(count) |
| 48 | + return 1 if count < 100 |
| 49 | + |
| 50 | + count / 100 |
| 51 | + end |
| 52 | +end |
0 commit comments