Skip to content

Commit

Permalink
code refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
pennja committed Sep 5, 2024
1 parent e1e4886 commit cbd26ad
Showing 1 changed file with 56 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def initialize(submission_ids:,
@failures = []
end

def run
bundle_by_user ? process_by_user : process_individual_submissions
cleanup_tmp_files
parent_dir
end

private

def submissions
@submissions ||= Form526Submission.where(id: submission_ids)
end
Expand All @@ -28,50 +36,64 @@ def submissions_by_uuid
@submissions_by_uuid ||= group_submissions_by_uuid
end

# allows for deduplicating submissions per user on the next layer
# Group submissions by user_uuid for easier bundling
def group_submissions_by_uuid
{}.tap do |collection|
submissions.pluck(:user_uuid).uniq.each do |uuid|
collection[uuid] = Form526Submission.where(user_uuid: uuid, id: submission_ids).pluck(:id)
end
submissions.group_by(&:user_uuid).transform_values do |user_submissions|
user_submissions.map(&:id)
end
end

def run
if bundle_by_user
submissions_by_uuid.each do |uuid, submission_ids|
Rails.logger.info(
"Processing for user: #{uuid} with #{submission_ids&.count} submission(s)",
{ uuid:, submission_ids: }
)
UserSubmissionDumpBuilder.new(uuid:, submission_ids:, parent_dir:).run
rescue => e
raise unless run_quiet

Rails.logger.error("User failure: #{uuid}", { uuid:, error: e })
failures << { uuid:, error: e }
end
else
submissions.each_with_index do |sub, idx|
Rails.logger.info(
"Processing submission: #{sub.id} (non-grouped) # #{idx + 1} of #{submissions.count} total submissions", {
submission_id: sub.id, submission_count: submissions.count
}
)
DumpSubmissionToPdf.new(submission_id: sub.id, parent_dir:, quiet_pdf_failures:, quiet_upload_failures:).run
rescue => e
raise unless run_quiet

failures << { submission_id: id, error: e }
end
def process_by_user
submissions_by_uuid.each do |uuid, submission_ids|
log_info("Processing for user: #{uuid} with #{submission_ids.size} submission(s)", uuid:, submission_ids:)
process_user_submissions(uuid, submission_ids)
end
parent_dir
end

def clear_tmp
def process_individual_submissions
submissions.each_with_index do |sub, idx|
log_info("Processing submission: #{sub.id} (non-grouped) ##{idx + 1} of #{submissions.count} total submissions",
submission_id: sub.id, submission_count: submissions.count)
process_submission(sub.id)
end
end

def process_user_submissions(uuid, submission_ids)
UserSubmissionDumpBuilder.new(uuid:, submission_ids:, parent_dir:).run
rescue => e
handle_error("User failure: #{uuid}", e, uuid:)
end

def process_submission(submission_id)
DumpSubmissionToPdf.new(
submission_id:,
parent_dir:,
quiet_pdf_failures:,
quiet_upload_failures:
).run
rescue => e
handle_error("Submission failure: #{submission_id}", e, submission_id:)
end

def handle_error(message, error, context)
raise unless run_quiet

log_error(message, error, context)
failures << { context => error }
end

def cleanup_tmp_files
system('rm -f tmp/* > /dev/null 2>&1')
end

def log_info(message, **details)
Rails.logger.info(message, details)
end

def log_error(message, error, **details)
Rails.logger.error(message, details.merge(error: error.message, backtrace: error.backtrace.first(5)))
end

def s3_resource
@s3_resource ||= Reports::Uploader.new_s3_resource
end
Expand Down

0 comments on commit cbd26ad

Please sign in to comment.