Skip to content

Commit

Permalink
Refs #37137 - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Feb 20, 2024
1 parent 5ec9f81 commit 4dfde98
Showing 1 changed file with 65 additions and 23 deletions.
88 changes: 65 additions & 23 deletions app/models/katello/concerns/host_managed_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,36 +326,77 @@ def import_enabled_repositories(repos)
end

def import_module_streams(module_streams)
# create_or_find_by avoids race conditions during concurrent registrations but clogs postgres logs with harmless errors.
# So we'll use create_or_find_by! during registration and first_or_create! otherwise.
registered_time = subscription_facet&.registered_at
use_create_or_find_by = registered_time.nil? || registered_time > 1.minute.ago
streams = {}
module_streams.each do |module_stream|
if use_create_or_find_by
stream = AvailableModuleStream.create_or_find_by!(name: module_stream["name"],
context: module_stream["context"],
stream: module_stream["stream"])
else
stream = AvailableModuleStream.where(name: module_stream["name"],
context: module_stream["context"],
stream: module_stream["stream"]).first_or_create!
end
streams[stream.id] = module_stream
end
sync_available_module_stream_associations(streams)
end

def sync_available_module_stream_associations(new_available_module_streams)
Rails.logger.debug "INSERT_ALL-------------------------------------"
Rails.logger.debug module_streams.first(3)
streams = module_streams.map do |module_stream|
{
name: module_stream["name"],
stream: module_stream["stream"],
context: module_stream["context"]
}
end
result = AvailableModuleStream.upsert_all(
streams,
unique_by: %w[name stream context],
returning: %w[id name stream context]
)
Rails.logger.debug "RESULT-------------------------------------"
Rails.logger.debug result.to_a
# original module_streams looks like this
# {"name"=>"389-ds", "stream"=>"1.4", "version"=>"8030020201203210520", "context"=>"e114a9e7", "arch"=>"x86_64", "profiles"=>[], "installed_profiles"=>[], "status"=>"default", "active"=>false}

# now we transform it into this with Hash#hash
# { 4156191055189065161 => {"name"=>"389-ds", "stream"=>"1.4", "version"=>"8030020201203210520", "context"=>"e114a9e7", "arch"=>"x86_64", "profiles"=>[], "installed_profiles"=>[], "status"=>"default", "active"=>false} }
indexed_module_streams = module_streams.index_by { |module_stream| module_stream.slice("name", "stream", "context").hash }

# result.to_a looks like this
# [{"id"=>1, "name"=>"389-ds", "stream"=>"1.4", "context"=>"e114a9e7"}, {"id"=>2, "name"=>"389-ds", "stream"=>"1.4", "context"=>"e114a9e7"}]
# now we transform it into this with Hash#hash
# { 4156191055189065161 => {"id"=>1, "name"=>"389-ds", "stream"=>"1.4", "context"=>"e114a9e7"}, 4156191055189065162 => {"id"=>2, "name"=>"389-ds", "stream"=>"1.4", "context"=>"e114a9e7"} }
indexed_result = result.to_a.index_by { |row| row.slice("name", "stream", "context").hash }
Rails.logger.debug "INDEXED_RESULT-------------------------------------"
Rails.logger.debug indexed_result

untransformed_keys = indexed_module_streams.keys
Rails.logger.debug "UNTRANSFORMED_KEYS-------------------------------------"
Rails.logger.debug untransformed_keys
# now we transform the keys of indexed_module_streams to the ids from indexed_result
# { 1 => {"name"=>"389-ds", "stream"=>"1.4", "version"=>"8030020201203210520", "context"=>"e114a9e7", "arch"=>"x86_64", "profiles"=>[], "installed_profiles"=>[], "status"=>"default", "active"=>false} }
indexed_module_streams.transform_keys! { |k| indexed_result[k]&.[]("id") }

Rails.logger.debug "TRANSFORMED KEYS-------------------------------------"
Rails.logger.debug indexed_module_streams.keys
# remove the keys that were not transformed
indexed_module_streams.except!(untransformed_keys)
indexed_module_streams.except!(nil)
Rails.logger.debug "AFTER REMOVING UNTRANSFORMED KEYS-------------------------------------"
Rails.logger.debug indexed_module_streams.keys # now all keys are valid AvailableModuleStream ids
Rails.logger.debug "INDEXED_MODULE_STREAMS-------------------------------------"
Rails.logger.debug indexed_module_streams

sync_available_module_stream_associations(indexed_module_streams, module_streams)
end

def sync_available_module_stream_associations(new_available_module_streams, original_metadata)
upgradable_streams = self.host_available_module_streams.where(:available_module_stream_id => new_available_module_streams.keys)
old_associated_ids = self.available_module_stream_ids
delete_ids = old_associated_ids - new_available_module_streams.keys
delete_ids = []
self.available_module_streams.each do |ams|
found = original_metadata.find { |module_stream| ams.name == module_stream["name"] && ams.stream == module_stream["stream"] && ams.context == module_stream["context"] }
Rails.logger.info found
delete_ids << ams.id if found.nil?
end
Rails.logger.info "DELETE_IDS-------------------------------------"
Rails.logger.info delete_ids

if delete_ids.any?
self.host_available_module_streams.where(:available_module_stream_id => delete_ids).delete_all
end

new_ids = new_available_module_streams.keys - old_associated_ids
Rails.logger.info "NEW_IDS-------------------------------------"
Rails.logger.info new_ids

new_ids.each do |new_id|
module_stream = new_available_module_streams[new_id]
status = module_stream["status"]
Expand All @@ -368,7 +409,8 @@ def sync_available_module_stream_associations(new_available_module_streams)
installed_profiles: module_stream["installed_profiles"],
status: status)
end

Rails.logger.info "UPGRADABLE_STREAMS-------------------------------------"
Rails.logger.info upgradable_streams
upgradable_streams.each do |hams|
module_stream = new_available_module_streams[hams.available_module_stream_id]
shared_keys = hams.attributes.keys & module_stream.keys
Expand Down

0 comments on commit 4dfde98

Please sign in to comment.