Skip to content

Commit

Permalink
Export Service - add simple manifest file, manifest.txt (#700)
Browse files Browse the repository at this point in the history
* first cut, working functionality

* Fix existing tests, add comments

* simplify recursive function

* fix folder line formatting

* Add test for output of simple manifest file

* fix test case failing from mismatched date
  • Loading branch information
JeffreyThiessen authored Aug 16, 2024
1 parent 9acd990 commit b3859ea
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
62 changes: 62 additions & 0 deletions app/jobs/data_exports/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,66 @@ def write_manifest(zip)
manifest_file.unlink
end

def write_simple_manifest(data_export_id:, zip:)
output_lines = simple_manifest_begin(data_export_id:)

simple_manifest_file = Tempfile.new
File.open(simple_manifest_file, 'a+') { |f| f.puts(output_lines) }
zip.write_file('manifest.txt') { |writer_for_file| IO.copy_stream(simple_manifest_file.open, writer_for_file) }

simple_manifest_file.close
simple_manifest_file.unlink
end

def simple_manifest_begin(data_export_id:)
output_lines = [
@manifest['type'],
@manifest['date'],
'',
'contents:',
"#{data_export_id}/"
]

child_count = @manifest['children'].count
(@manifest['children']).each_with_index do |child, index|
# push with * is used to flatten array without creating a new array
output_lines.push(*simple_manifest_gen_lines_recursive(
cursor: child, prefix: '', final_child: child_count == index + 1
))
end

output_lines
end

def simple_manifest_gen_lines_recursive(cursor:, prefix: '', final_child: false) # rubocop:disable Metrics/MethodLength
output = []

# when the current cursor is the final child of the parent, we change which symbols are used to generate the lines
if final_child
line = "#{prefix}└─ #{cursor['name']}"
child_prefix = "#{prefix} "
else
line = "#{prefix}├─ #{cursor['name']}"
child_prefix = "#{prefix}│ "
end

line += '/' if cursor['type'] == 'folder'
line += " (#{cursor['irida-next-name']})" if cursor.key?('irida-next-name')
output.append(line)

if cursor['type'] == 'folder'
child_count = cursor['children'].count
(cursor['children']).each_with_index do |child, index|
# push with * is used to flatten array without creating a new array
output.push(*simple_manifest_gen_lines_recursive(
cursor: child, prefix: child_prefix, final_child: child_count == index + 1
))
end
end

output # array of lines
end

# Sample export specific functions------------------------------------------------------------------
def create_sample_zip(data_export)
Tempfile.new(binmode: true).tap do |tempfile|
Expand All @@ -92,6 +152,7 @@ def create_sample_zip(data_export)
end

write_manifest(zip)
write_simple_manifest(data_export_id: data_export.id.to_s, zip:)
end
end
end
Expand Down Expand Up @@ -167,6 +228,7 @@ def create_analysis_zip(data_export)
end

write_manifest(zip)
write_simple_manifest(data_export_id: data_export.id.to_s, zip:)
end
end
end
Expand Down
50 changes: 47 additions & 3 deletions test/jobs/data_exports/create_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def setup
attachment2 = attachments(:attachment2)
expected_files_in_zip = ["#{project.puid}/#{sample.puid}/#{attachment1.puid}/#{attachment1.file.filename}",
"#{project.puid}/#{sample.puid}/#{attachment2.puid}/#{attachment2.file.filename}",
'manifest.json']
'manifest.json',
'manifest.txt']
DataExports::CreateJob.perform_now(@data_export2)
export_file = ActiveStorage::Blob.service.path_for(@data_export2.file.key)
Zip::File.open(export_file) do |zip_file|
Expand Down Expand Up @@ -82,6 +83,46 @@ def setup
assert_equal expected_manifest.to_json, @data_export2.manifest
end

test 'content of sample export simple manifest' do
sample = samples(:sample1)
project = projects(:project1)
attachment1 = attachments(:attachment1)
attachment2 = attachments(:attachment2)
expected_files_in_zip = ["#{project.puid}/#{sample.puid}/#{attachment1.puid}/#{attachment1.file.filename}",
"#{project.puid}/#{sample.puid}/#{attachment2.puid}/#{attachment2.file.filename}",
'manifest.json',
'manifest.txt']
actual_simple_manifest = ''

DataExports::CreateJob.perform_now(@data_export2)
export_file = ActiveStorage::Blob.service.path_for(@data_export2.file.key)
Zip::File.open(export_file) do |zip_file|
zip_file.each do |entry|
assert expected_files_in_zip.include?(entry.to_s)
actual_simple_manifest = entry.get_input_stream.read.force_encoding('utf-8') if entry.to_s == 'manifest.txt'
expected_files_in_zip.delete(entry.to_s)
end
end
assert expected_files_in_zip.empty?

expected_simple_manifest = [
'Samples Export',
Date.current.strftime('%Y-%m-%d').to_s,
'',
'contents:',
'9eb3e2b7-6e20-5324-b6a3-49f44dc33fd8/',
'└─ INXT_PRJ_AAAAAAAAAA/ (Project 1)',
' └─ INXT_SAM_AAAAAAAAAA/ (Project 1 Sample 1)',
' ├─ INXT_ATT_AAAAAAAAAA/',
' │ └─ test_file.fastq',
' └─ INXT_ATT_AAAAAAAAAB/',
' └─ test_file_A.fastq',
''
].join("\n")

assert_equal expected_simple_manifest, actual_simple_manifest
end

test 'content of sample export including paired files' do
sample_b = samples(:sampleB)

Expand All @@ -108,7 +149,8 @@ def setup
"#{project.puid}/#{sample_b.puid}/#{attachment_d.puid}/#{attachment_d.file.filename}",
"#{project.puid}/#{sample_b.puid}/#{attachment_e.puid}/#{attachment_e.file.filename}",
"#{project.puid}/#{sample_b.puid}/#{attachment_f.puid}/#{attachment_f.file.filename}",
'manifest.json'
'manifest.json',
'manifest.txt'
]

expected_manifest = {
Expand Down Expand Up @@ -257,6 +299,7 @@ def setup

expected_files_in_zip = ["#{sample.puid}/#{samples_workflow_execution.outputs[0].filename}",
'manifest.json',
'manifest.txt',
workflow_execution.outputs[0].filename.to_s]
DataExports::CreateJob.perform_now(@data_export6)
export_file = ActiveStorage::Blob.service.path_for(@data_export6.file.key)
Expand Down Expand Up @@ -367,7 +410,8 @@ def setup
[
"#{project.puid}/#{sample22.puid}/#{text_attachment.puid}/#{text_attachment.file.filename}",
"#{project.puid}/#{sample22.puid}/#{fasta_attachment.puid}/#{fasta_attachment.file.filename}",
'manifest.json'
'manifest.json',
'manifest.txt'
]

expected_manifest = {
Expand Down

0 comments on commit b3859ea

Please sign in to comment.