Skip to content

Commit

Permalink
Ensuring that only a single WorkActivity with multiple events will be…
Browse files Browse the repository at this point in the history
… created for file additions, replacements, and deletions
  • Loading branch information
jrgriffiniii committed May 1, 2023
1 parent 9a0f1cb commit ed8f025
Showing 1 changed file with 84 additions and 51 deletions.
135 changes: 84 additions & 51 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,72 +443,105 @@ def s3_query_service
@s3_query_service ||= S3QueryService.new(self, !approved?)
end

def reload_snapshots
results = s3_files.map do |s3_file|
UploadSnapshot.find_or_initialize_by(url: s3_file.url, filename: s3_file.filename, work: self)
end
def past_snapshots
UploadSnapshot.where(work: self)
end

# Remove the snapshots for S3 file resources which have been deleted
def valid_snapshots
s3_resource_urls = s3_files.map(&:url)
s3_resource_filenames = s3_files.map(&:filename)

# remove the snapshots for s3 file resources which have been deleted
persisted = results.reject do |snapshot|
removed = !s3_resource_urls.include?(snapshot.url) && !s3_resource_filenames.include?(snapshot.filename)
past_snapshots.select do |snapshot|
s3_resource_urls.include?(snapshot.url) || s3_resource_filenames.include?(snapshot.filename)
end
end

if removed
snapshot.destroy
changes = {
action: "removed"
}
WorkActivity.add_work_activity(id, changes.to_json, nil, activity_type: WorkActivity::FILE_CHANGES)
end
def new_s3_files
s3_files.reject do |s3_file|
UploadSnapshot.find_by(work: self, url: s3_file.url, filename: s3_file.filename)
end
end

removed
def new_snapshots
new_s3_files.map do |s3_file|
UploadSnapshot.new(work: self, url: s3_file.url, filename: s3_file.filename)
end
end

changes = []
s3_files.map do |s3_file|
s3_match = s3_file.filename.match(/_\d+_/)
s3_filename = if s3_match
s3_file.filename.gsub(/_\d+\.([0-9a-zA-Z]+)$/, ".\\1")
else
s3_file.filename
end
def current_snapshots
valid_snapshots + new_snapshots
end

selected = persisted.select do |s|
# checking for version substrings
snapshot_match = s.filename.match(/_\d+_/)
s_filename = if snapshot_match
s.filename.gsub(/_\d+\.([0-9a-zA-Z]+)$/, ".\\1")
else
s.filename
end
def invalid_snapshots
current_snapshots - past_snapshots
end

s_filename == s3_filename
end
snapshot = selected.last
def self.select_matching_snapshots(snapshots:, s3_filename:)
snapshots.select do |snapshot|
# Checking for version substrings
matching_filename = snapshot.filename.match(/_\d+_/)
snapshot_filename = if matching_filename
snapshot.filename.gsub(/_\d+\.([0-9a-zA-Z]+)$/, ".\\1")
else
snapshot.filename
end

if snapshot.checksum != s3_file.checksum
snapshot_filename == s3_filename
end
end

# cases where the s3 resources are replaced
snapshot.checksum = s3_file.checksum
changes << if !snapshot.persisted?
{
action: "added"
}
else
{
action: "replaced"
}
end
snapshot.save
end
def self.select_matching_filename(s3_file:)
# Handling the foo.bin, foo_1.bin, foo_2.bin file naming scheme
s3_match = s3_file.filename.match(/_\d+_/)
if s3_match
s3_file.filename.gsub(/_\d+\.([0-9a-zA-Z]+)$/, ".\\1")
else
s3_file.filename
end
end

# Build or find persisted UploadSnapshot models for this Work
# @return [UploadSnapshot]
def reload_snapshots
work_changes = []

# Remove the existing snapshots and ensure that the WorkActivity is updated
invalid_snapshots.each do |invalid|
invalid.destroy

snapshot.reload
changes = {
action: "removed"
}
work_changes << changes
end
unless changes.empty?
WorkActivity.add_work_activity(id, changes.to_json, nil, activity_type: WorkActivity::FILE_CHANGES)

s3_files.each do |s3_file|
s3_filename = self.class.select_matching_filename(s3_file: s3_file)
matching_snapshots = self.class.select_matching_snapshots(snapshots: current_snapshots, s3_filename: s3_filename)

current_snapshot = matching_snapshots.last

# Compare the checksum values for the updated S3 file and current snapshot
next if current_snapshot.checksum == s3_file.checksum

current_snapshot.checksum = s3_file.checksum
changes = if !current_snapshot.persisted?
{
action: "added"
}
else
{
action: "replaced"
}
end
work_changes << changes
current_snapshot.save
current_snapshot.reload
end

# Create WorkActivity models with the set of changes
WorkActivity.add_work_activity(id, work_changes.to_json, nil, activity_type: WorkActivity::FILE_CHANGES) unless work_changes.empty?
end

protected
Expand Down

0 comments on commit ed8f025

Please sign in to comment.