Browse Source

Add zip information to master manifest

pull/587/head
Stéphane Meny 3 years ago
parent
commit
c47bdeeb45
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 21
      app/services/exports/case_log_export_service.rb
  2. 18
      spec/services/exports/case_log_export_service_spec.rb

21
app/services/exports/case_log_export_service.rb

@ -20,8 +20,8 @@ module Exports
case_logs = retrieve_case_logs(current_time) case_logs = retrieve_case_logs(current_time)
export = build_export_run(current_time) export = build_export_run(current_time)
daily_run = get_daily_run_number daily_run = get_daily_run_number
archive_list = write_export_archive(case_logs) archive_datetimes = write_export_archive(case_logs)
write_master_manifest(daily_run, archive_list) write_master_manifest(daily_run, archive_datetimes)
export.save! export.save!
end end
@ -38,8 +38,8 @@ module Exports
LogsExport.where(created_at: today.beginning_of_day..today.end_of_day).count + 1 LogsExport.where(created_at: today.beginning_of_day..today.end_of_day).count + 1
end end
def build_export_run(current_time, full_update = false) def build_export_run(current_time, full_update: false)
if LogsExport.count == 0 if LogsExport.count.zero?
return LogsExport.new(started_at: current_time) return LogsExport.new(started_at: current_time)
end end
@ -56,13 +56,13 @@ module Exports
LogsExport.new(started_at: current_time, base_number:, increment_number:) LogsExport.new(started_at: current_time, base_number:, increment_number:)
end end
def write_master_manifest(daily_run, archive_list) def write_master_manifest(daily_run, archive_datetimes)
today = Time.zone.today today = Time.zone.today
increment_number = daily_run.to_s.rjust(4, "0") increment_number = daily_run.to_s.rjust(4, "0")
month = today.month.to_s.rjust(2, "0") month = today.month.to_s.rjust(2, "0")
day = today.day.to_s.rjust(2, "0") day = today.day.to_s.rjust(2, "0")
file_path = "Manifest_#{today.year}_#{month}_#{day}_#{increment_number}.csv" file_path = "Manifest_#{today.year}_#{month}_#{day}_#{increment_number}.csv"
string_io = build_manifest_csv_io string_io = build_manifest_csv_io(archive_datetimes)
@storage_service.write_file(file_path, string_io) @storage_service.write_file(file_path, string_io)
end end
@ -88,6 +88,7 @@ module Exports
end end
# Write all archives # Write all archives
archive_datetimes = {}
case_logs_per_archive.each do |archive, case_logs_to_export| case_logs_per_archive.each do |archive, case_logs_to_export|
manifest_xml = build_manifest_xml(case_logs_to_export.count) manifest_xml = build_manifest_xml(case_logs_to_export.count)
zip_io = Zip::File.open_buffer(StringIO.new) zip_io = Zip::File.open_buffer(StringIO.new)
@ -102,9 +103,10 @@ module Exports
end end
@storage_service.write_file("#{archive}.zip", zip_io.write_buffer) @storage_service.write_file("#{archive}.zip", zip_io.write_buffer)
archive_datetimes[archive] = Time.zone.now
end end
case_logs_per_archive.keys archive_datetimes
end end
def retrieve_case_logs(current_time) def retrieve_case_logs(current_time)
@ -118,10 +120,13 @@ module Exports
end end
end end
def build_manifest_csv_io def build_manifest_csv_io(archive_datetimes)
headers = ["zip-name", "date-time zipped folder generated", "zip-file-uri"] headers = ["zip-name", "date-time zipped folder generated", "zip-file-uri"]
csv_string = CSV.generate do |csv| csv_string = CSV.generate do |csv|
csv << headers csv << headers
archive_datetimes.each do |archive, datetime|
csv << [archive, datetime, "#{archive}.zip"]
end
end end
StringIO.new(csv_string) StringIO.new(csv_string)
end end

18
spec/services/exports/case_log_export_service_spec.rb

@ -11,9 +11,7 @@ RSpec.describe Exports::CaseLogExportService do
let(:expected_zip_filename) { "core_2021_2022_jan_mar_f0001_inc0001.zip" } let(:expected_zip_filename) { "core_2021_2022_jan_mar_f0001_inc0001.zip" }
let(:expected_manifest_filename) { "manifest.xml" } let(:expected_manifest_filename) { "manifest.xml" }
let!(:case_log) { FactoryBot.create(:case_log, :completed) } def replace_entity_ids(case_log, export_template)
def replace_entity_ids(export_template)
export_template.sub!(/\{id\}/, (case_log["id"] + Exports::CaseLogExportService::LOG_ID_OFFSET).to_s) export_template.sub!(/\{id\}/, (case_log["id"] + Exports::CaseLogExportService::LOG_ID_OFFSET).to_s)
export_template.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s) export_template.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s)
export_template.sub!(/\{managing_org_id\}/, case_log["managing_organisation_id"].to_s) export_template.sub!(/\{managing_org_id\}/, case_log["managing_organisation_id"].to_s)
@ -51,6 +49,7 @@ RSpec.describe Exports::CaseLogExportService do
end end
context "and one case log is available for export" do context "and one case log is available for export" do
let!(:case_log) { FactoryBot.create(:case_log, :completed) }
let(:expected_data_filename) { "core_2021_2022_jan_mar_f0001_inc0001_pt001.xml" } let(:expected_data_filename) { "core_2021_2022_jan_mar_f0001_inc0001_pt001.xml" }
it "generates a ZIP export file with the expected filename" do it "generates a ZIP export file with the expected filename" do
@ -88,7 +87,7 @@ RSpec.describe Exports::CaseLogExportService do
end end
it "generates an XML export file with the expected content within the ZIP file" do it "generates an XML export file with the expected content within the ZIP file" do
expected_content = replace_entity_ids(export_file.read) expected_content = replace_entity_ids(case_log, export_file.read)
expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content|
entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) entry = Zip::File.open_buffer(content).find_entry(expected_data_filename)
expect(entry).not_to be_nil expect(entry).not_to be_nil
@ -101,7 +100,11 @@ RSpec.describe Exports::CaseLogExportService do
context "and multiple case logs are available for export on different periods" do context "and multiple case logs are available for export on different periods" do
let(:expected_zip_filename2) { "core_2022_2023_apr_jun_f0001_inc0001.zip" } let(:expected_zip_filename2) { "core_2022_2023_apr_jun_f0001_inc0001.zip" }
before { FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 4, 1)) }
before do
FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 2, 1))
FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 4, 1))
end
context "when case logs are across multiple quarters" do context "when case logs are across multiple quarters" do
it "generates multiple ZIP export files with the expected filenames" do it "generates multiple ZIP export files with the expected filenames" do
@ -114,7 +117,10 @@ RSpec.describe Exports::CaseLogExportService do
end end
context "and multiple case logs are available for export on same periods" do context "and multiple case logs are available for export on same periods" do
before { FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20)) } before do
FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20))
FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20))
end
it "generates an XML manifest file with the expected content within the ZIP file" do it "generates an XML manifest file with the expected content within the ZIP file" do
expected_content = replace_record_number(local_manifest_file.read, 2) expected_content = replace_record_number(local_manifest_file.read, 2)

Loading…
Cancel
Save