Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgriffiniii committed Apr 28, 2023
1 parent 9a0f1cb commit e9531da
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,72 +443,87 @@ 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.find_or_initialize_by(work: self)
end

def select_current_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)
# Remove the snapshots for S3 file resources which have been deleted
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 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

removed
snapshot_filename == s3_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

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 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

s_filename == s3_filename
end
snapshot = selected.last
# Build or find persisted UploadSnapshot models for this Work
# @return [UploadSnapshot]
def reload_snapshots
persisted = select_current_snapshots
invalid_snapshots = persisted - past_snapshots

if snapshot.checksum != s3_file.checksum
work_changes = []

# 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
# 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: persisted, 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 e9531da

Please sign in to comment.