<commit_before>class ManagerRefresh::InventoryCollectionDefault::CloudManager < ManagerRefresh::InventoryCollectionDefault
  class << self
    def vms(extra_attributes = {})
      attributes = {
        :model_class          => ::ManageIQ::Providers::CloudManager::Vm,
        :association          => :vms,
        :delete_method        => :disconnect_inv,
        :attributes_blacklist => [:genealogy_parent]
      }

      attributes.merge!(extra_attributes)
    end

    def miq_templates(extra_attributes = {})
      attributes = {
        :model_class          => ::ManageIQ::Providers::CloudManager::Template,
        :association          => :miq_templates,
        :delete_method        => :disconnect_inv,
        :attributes_blacklist => [:genealogy_parent]
      }

      attributes.merge!(extra_attributes)
    end

    def availability_zones(extra_attributes = {})
      attributes = {
        :model_class => ::AvailabilityZone,
        :association => :availability_zones,
      }

      attributes.merge!(extra_attributes)
    end

    def flavors(extra_attributes = {})
      attributes = {
        :model_class => ::Flavor,
        :association => :flavors,
      }

      attributes.merge!(extra_attributes)
    end

    def key_pairs(extra_attributes = {})
      attributes = {
        :model_class => ::ManageIQ::Providers::CloudManager::AuthKeyPair,
        :manager_ref => [:name],
        :association => :key_pairs
      }

      attributes.merge!(extra_attributes)
    end

    def hardwares(extra_attributes = {})
      attributes = {
        :model_class => ::Hardware,
        :manager_ref => [:vm_or_template],
        :association => :hardwares
      }

      attributes[:custom_manager_uuid] = lambda do |hardware|
        [hardware.vm_or_template.ems_ref]
      end

      attributes[:custom_db_finder] = lambda do |inventory_collection, selection, _projection|
        relation = inventory_collection.parent.send(inventory_collection.association)
                                       .includes(:vm_or_template)
                                       .references(:vm_or_template)
        relation = relation.where(:vms => {:ems_ref => selection[:vm_or_template]}) unless selection.blank?
        relation
      end

      attributes.merge!(extra_attributes)
    end

    def disks(extra_attributes = {})
      attributes = {
        :model_class => ::Disk,
        :manager_ref => [:hardware, :device_name],
        :association => :disks
      }

      if extra_attributes[:strategy] == :local_db_cache_all
        attributes[:custom_manager_uuid] = lambda do |disk|
          [disk.hardware.vm_or_template.ems_ref, disk.device_name]
        end
      end

      attributes.merge!(extra_attributes)
    end

    def networks(extra_attributes = {})
      attributes = {
        :model_class => ::Network,
        :manager_ref => [:hardware, :description],
        :association => :networks
      }

      if extra_attributes[:strategy] == :local_db_cache_all
        attributes[:custom_manager_uuid] = lambda do |network|
          [network.hardware.vm_or_template.ems_ref, network.description]
        end
      end

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks(extra_attributes = {})
      attributes = {
        :model_class          => ::ManageIQ::Providers::CloudManager::OrchestrationStack,
        :association          => :orchestration_stacks,
        :attributes_blacklist => [:parent]
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks_resources(extra_attributes = {})
      attributes = {
        :model_class => ::OrchestrationStackResource,
        :association => :orchestration_stacks_resources,
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks_outputs(extra_attributes = {})
      attributes = {
        :model_class => ::OrchestrationStackOutput,
        :association => :orchestration_stacks_outputs,
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks_parameters(extra_attributes = {})
      attributes = {
        :model_class => ::OrchestrationStackParameter,
        :association => :orchestration_stacks_parameters,
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_templates(extra_attributes = {})
      # TODO(lsmola) do refactoring, we shouldn't need this custom saving block
      orchestration_template_save_block = lambda do |_ems, inventory_collection|
        hashes = inventory_collection.data.map(&:attributes)

        templates = ::OrchestrationTemplate.find_or_create_by_contents(hashes)
        inventory_collection.data.zip(templates).each do |inventory_object, template|
          inventory_object.id = template.id
        end
      end

      attributes = {
        :model_class       => ::OrchestrationTemplateCfn,
        :association       => :orchestration_templates,
        :custom_save_block => orchestration_template_save_block
      }<commit_msg>Use correct model class for saving OrchestrationTemplate<commit_after>class ManagerRefresh::InventoryCollectionDefault::CloudManager < ManagerRefresh::InventoryCollectionDefault
  class << self
    def vms(extra_attributes = {})
      attributes = {
        :model_class          => ::ManageIQ::Providers::CloudManager::Vm,
        :association          => :vms,
        :delete_method        => :disconnect_inv,
        :attributes_blacklist => [:genealogy_parent]
      }

      attributes.merge!(extra_attributes)
    end

    def miq_templates(extra_attributes = {})
      attributes = {
        :model_class          => ::ManageIQ::Providers::CloudManager::Template,
        :association          => :miq_templates,
        :delete_method        => :disconnect_inv,
        :attributes_blacklist => [:genealogy_parent]
      }

      attributes.merge!(extra_attributes)
    end

    def availability_zones(extra_attributes = {})
      attributes = {
        :model_class => ::AvailabilityZone,
        :association => :availability_zones,
      }

      attributes.merge!(extra_attributes)
    end

    def flavors(extra_attributes = {})
      attributes = {
        :model_class => ::Flavor,
        :association => :flavors,
      }

      attributes.merge!(extra_attributes)
    end

    def key_pairs(extra_attributes = {})
      attributes = {
        :model_class => ::ManageIQ::Providers::CloudManager::AuthKeyPair,
        :manager_ref => [:name],
        :association => :key_pairs
      }

      attributes.merge!(extra_attributes)
    end

    def hardwares(extra_attributes = {})
      attributes = {
        :model_class => ::Hardware,
        :manager_ref => [:vm_or_template],
        :association => :hardwares
      }

      attributes[:custom_manager_uuid] = lambda do |hardware|
        [hardware.vm_or_template.ems_ref]
      end

      attributes[:custom_db_finder] = lambda do |inventory_collection, selection, _projection|
        relation = inventory_collection.parent.send(inventory_collection.association)
                                       .includes(:vm_or_template)
                                       .references(:vm_or_template)
        relation = relation.where(:vms => {:ems_ref => selection[:vm_or_template]}) unless selection.blank?
        relation
      end

      attributes.merge!(extra_attributes)
    end

    def disks(extra_attributes = {})
      attributes = {
        :model_class => ::Disk,
        :manager_ref => [:hardware, :device_name],
        :association => :disks
      }

      if extra_attributes[:strategy] == :local_db_cache_all
        attributes[:custom_manager_uuid] = lambda do |disk|
          [disk.hardware.vm_or_template.ems_ref, disk.device_name]
        end
      end

      attributes.merge!(extra_attributes)
    end

    def networks(extra_attributes = {})
      attributes = {
        :model_class => ::Network,
        :manager_ref => [:hardware, :description],
        :association => :networks
      }

      if extra_attributes[:strategy] == :local_db_cache_all
        attributes[:custom_manager_uuid] = lambda do |network|
          [network.hardware.vm_or_template.ems_ref, network.description]
        end
      end

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks(extra_attributes = {})
      attributes = {
        :model_class          => ::ManageIQ::Providers::CloudManager::OrchestrationStack,
        :association          => :orchestration_stacks,
        :attributes_blacklist => [:parent]
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks_resources(extra_attributes = {})
      attributes = {
        :model_class => ::OrchestrationStackResource,
        :association => :orchestration_stacks_resources,
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks_outputs(extra_attributes = {})
      attributes = {
        :model_class => ::OrchestrationStackOutput,
        :association => :orchestration_stacks_outputs,
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_stacks_parameters(extra_attributes = {})
      attributes = {
        :model_class => ::OrchestrationStackParameter,
        :association => :orchestration_stacks_parameters,
      }

      attributes.merge!(extra_attributes)
    end

    def orchestration_templates(extra_attributes = {})
      # TODO(lsmola) do refactoring, we shouldn't need this custom saving block
      orchestration_template_save_block = lambda do |_ems, inventory_collection|
        hashes = inventory_collection.data.map(&:attributes)

        templates = inventory_collection.model_class.find_or_create_by_contents(hashes)
        inventory_collection.data.zip(templates).each do |inventory_object, template|
          inventory_object.id = template.id
        end
      end

      attributes = {
        :model_class       => ::OrchestrationTemplate,
        :association       => :orchestration_templates,
        :custom_save_block => orchestration_template_save_block
      }
{'fraction_non_alphanumeric': 0.10112136548575965, 'fraction_numerical': 0.0, 'mean_word_length': 2.675419401896426, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3404473', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2783, 'n_words': 436}