<commit_before>class ManageIQ::Providers::Amazon::Inventory::Collector::TargetCollection < ManageIQ::Providers::Amazon::Inventory::Collector
  def initialize(_manager, _target)
    super
    parse_targets!
    infer_related_ems_refs!

    # Reset the target cache, so we can access new targets inside
    target.manager_refs_by_association_reset
  end

  def references(collection)
    target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a).try(:compact) || []
  end

  def name_references(collection)
    target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a).try(:compact) || []
  end

  def instances
    return [] if references(:vms).blank?
    return @instances_hashes if @instances_hashes

    multi_query(references(:vms)) do |refs|
      @instances_hashes = hash_collection.new(
        aws_ec2.instances(:filters => [{:name => 'instance-id', :values => refs}])
      ).all
    end
  end

  def availability_zones
    return [] if references(:availability_zones).blank?

    multi_query(references(:availability_zones)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_availability_zones(
          :filters => [{:name => 'zone-name', :values => refs}]
        ).flat_map(&:availability_zones)
      ).all
    end
  end

  def key_pairs
    return [] if name_references(:key_pairs).blank?

    multi_query(name_references(:key_pairs)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_key_pairs(
          :filters => [{:name => 'key-name', :values => refs}]
        ).flat_map(&:key_pairs)
      ).all
    end
  end

  def referenced_images
    return [] if references(:miq_templates).blank?

    multi_query(references(:miq_templates)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_images(:filters => [{:name => 'image-id', :values => refs}]).flat_map(&:images)
      ).all
    end
  end

  def stacks
    return [] if references(:orchestration_stacks).blank?

    # TODO(lsmola) we can filter only one stack, so that means too many requests, lets try to figure out why
    # CLoudFormations API doesn't support a standard filter
    result = references(:orchestration_stacks).map do |stack_ref|
      begin
        aws_cloud_formation.client.describe_stacks(:stack_name => stack_ref).flat_map(&:stacks)
      rescue Aws::CloudFormation::Errors::ValidationError => _e
        # A missing stack throws and exception like this, we want to ignore it and just don't list it
      end
    end.flatten.compact

    hash_collection.new(result)
  end

  def cloud_networks
    return [] if references(:cloud_networks).blank?

    multi_query(references(:cloud_networks)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_vpcs(:filters => [{:name => 'vpc-id', :values => refs}]).flat_map(&:vpcs)
      ).all
    end
  end

  def cloud_subnets
    return [] if references(:cloud_subnets).blank?

    multi_query(references(:cloud_subnets)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_subnets(:filters => [{:name => 'subnet-id', :values => refs}]).flat_map(&:subnets)
      ).all
    end
  end

  def security_groups
    return [] if references(:security_groups).blank?

    multi_query(references(:security_groups)) do |refs|
      hash_collection.new(
        aws_ec2.security_groups(:filters => [{:name => 'group-id', :values => refs}])
      ).all
    end
  end

  def network_ports
    return [] if references(:network_ports).blank?
    return @network_ports_hashes if @network_ports_hashes

    @network_ports_hashes = multi_query(references(:network_ports)) do |refs|
      hash_collection.new(aws_ec2.client.describe_network_interfaces(
        :filters => [{:name => 'network-interface-id', :values => refs}]
      ).flat_map(&:network_interfaces)).all
    end
  end

  def load_balancers
    return [] if references(:load_balancers).blank?

    result = []
    references(:load_balancers).each do |load_balancers_ref|
      begin
        result += aws_elb.client.describe_load_balancers(
          :load_balancer_names => [load_balancers_ref]
        ).flat_map(&:load_balancer_descriptions)
      rescue ::Aws::ElasticLoadBalancing::Errors::LoadBalancerNotFound => _e
        # TODO(lsmola) maybe it will be faster to fetch all LBs and filter them?
        # Ignore LB that was not found, it will be deleted from our DB
      end
    end

    hash_collection.new(result)
  end

  def floating_ips
    return [] if references(:floating_ips).blank?


    multi_query(references(:floating_ips)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_addresses(:filters => [{:name => 'allocation-id', :values => refs}]).flat_map(&:addresses) +
        aws_ec2.client.describe_addresses(:filters => [{:name => 'public-ip', :values => refs}]).flat_map(&:addresses)
      ).all
    end
  end

  def cloud_volumes<commit_msg>Rubocop fix.<commit_after>class ManageIQ::Providers::Amazon::Inventory::Collector::TargetCollection < ManageIQ::Providers::Amazon::Inventory::Collector
  def initialize(_manager, _target)
    super
    parse_targets!
    infer_related_ems_refs!

    # Reset the target cache, so we can access new targets inside
    target.manager_refs_by_association_reset
  end

  def references(collection)
    target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a).try(:compact) || []
  end

  def name_references(collection)
    target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a).try(:compact) || []
  end

  def instances
    return [] if references(:vms).blank?
    return @instances_hashes if @instances_hashes

    multi_query(references(:vms)) do |refs|
      @instances_hashes = hash_collection.new(
        aws_ec2.instances(:filters => [{:name => 'instance-id', :values => refs}])
      ).all
    end
  end

  def availability_zones
    return [] if references(:availability_zones).blank?

    multi_query(references(:availability_zones)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_availability_zones(
          :filters => [{:name => 'zone-name', :values => refs}]
        ).flat_map(&:availability_zones)
      ).all
    end
  end

  def key_pairs
    return [] if name_references(:key_pairs).blank?

    multi_query(name_references(:key_pairs)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_key_pairs(
          :filters => [{:name => 'key-name', :values => refs}]
        ).flat_map(&:key_pairs)
      ).all
    end
  end

  def referenced_images
    return [] if references(:miq_templates).blank?

    multi_query(references(:miq_templates)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_images(:filters => [{:name => 'image-id', :values => refs}]).flat_map(&:images)
      ).all
    end
  end

  def stacks
    return [] if references(:orchestration_stacks).blank?

    # TODO(lsmola) we can filter only one stack, so that means too many requests, lets try to figure out why
    # CLoudFormations API doesn't support a standard filter
    result = references(:orchestration_stacks).map do |stack_ref|
      begin
        aws_cloud_formation.client.describe_stacks(:stack_name => stack_ref).flat_map(&:stacks)
      rescue Aws::CloudFormation::Errors::ValidationError => _e
        # A missing stack throws and exception like this, we want to ignore it and just don't list it
      end
    end.flatten.compact

    hash_collection.new(result)
  end

  def cloud_networks
    return [] if references(:cloud_networks).blank?

    multi_query(references(:cloud_networks)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_vpcs(:filters => [{:name => 'vpc-id', :values => refs}]).flat_map(&:vpcs)
      ).all
    end
  end

  def cloud_subnets
    return [] if references(:cloud_subnets).blank?

    multi_query(references(:cloud_subnets)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_subnets(:filters => [{:name => 'subnet-id', :values => refs}]).flat_map(&:subnets)
      ).all
    end
  end

  def security_groups
    return [] if references(:security_groups).blank?

    multi_query(references(:security_groups)) do |refs|
      hash_collection.new(
        aws_ec2.security_groups(:filters => [{:name => 'group-id', :values => refs}])
      ).all
    end
  end

  def network_ports
    return [] if references(:network_ports).blank?
    return @network_ports_hashes if @network_ports_hashes

    @network_ports_hashes = multi_query(references(:network_ports)) do |refs|
      hash_collection.new(aws_ec2.client.describe_network_interfaces(
        :filters => [{:name => 'network-interface-id', :values => refs}]
      ).flat_map(&:network_interfaces)).all
    end
  end

  def load_balancers
    return [] if references(:load_balancers).blank?

    result = []
    references(:load_balancers).each do |load_balancers_ref|
      begin
        result += aws_elb.client.describe_load_balancers(
          :load_balancer_names => [load_balancers_ref]
        ).flat_map(&:load_balancer_descriptions)
      rescue ::Aws::ElasticLoadBalancing::Errors::LoadBalancerNotFound => _e
        # TODO(lsmola) maybe it will be faster to fetch all LBs and filter them?
        # Ignore LB that was not found, it will be deleted from our DB
      end
    end

    hash_collection.new(result)
  end

  def floating_ips
    return [] if references(:floating_ips).blank?

    multi_query(references(:floating_ips)) do |refs|
      hash_collection.new(
        aws_ec2.client.describe_addresses(:filters => [{:name => 'allocation-id', :values => refs}]).flat_map(&:addresses) +
        aws_ec2.client.describe_addresses(:filters => [{:name => 'public-ip', :values => refs}]).flat_map(&:addresses)
      ).all
    end
  end

  def cloud_volumes
{'fraction_non_alphanumeric': 0.12446483180428135, 'fraction_numerical': 0.0020387359836901123, 'mean_word_length': 3.610432330827068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '536179', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3075, 'n_words': 638}