<commit_before>module ManageIQ
  module Providers
    class Openstack::InfraManager::RefreshParser < EmsRefresh::Parsers::Infra
      include Vmdb::Logging

      include ManageIQ::Providers::Openstack::RefreshParserCommon::HelperMethods
      include ManageIQ::Providers::Openstack::RefreshParserCommon::Images
      include ManageIQ::Providers::Openstack::RefreshParserCommon::Objects
      include ManageIQ::Providers::Openstack::RefreshParserCommon::OrchestrationStacks

      def self.ems_inv_to_hashes(ems, options = nil)
        new(ems, options).ems_inv_to_hashes
      end

      def initialize(ems, _options = nil)
        @ems               = ems
        @connection        = ems.connect
        @data              = {}
        @data_index        = {}
        @host_hash_by_name = {}
        @resource_to_stack = {}

        @known_flavors = Set.new

        @os_handle                  = ems.openstack_handle
        @compute_service            = @connection # for consistency
        @baremetal_service          = @os_handle.detect_baremetal_service
        @orchestration_service      = @os_handle.detect_orchestration_service
        @image_service              = @os_handle.detect_image_service
        @storage_service            = @os_handle.detect_storage_service
      end

      def ems_inv_to_hashes
        log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data" \
                     " for EMS name: [#{@ems.name}] id: [#{@ems.id}]"
        $fog_log.info("#{log_header}...")

        get_object_store
        # get_object_store needs to run before load hosts
        load_hosts
        load_orchestration_stacks
        # Cluster processing needs to run after host and stacks processing
        get_clusters
        get_images

        $fog_log.info("#{log_header}...Complete")
        @data
      end

      private

      def all_server_resources
        return @all_server_resources if @all_server_resources
        resources = []
        stacks.each do |stack|
          # Filtering just OS::Nova::Server, which is important to us for getting Purpose of the node
          # (compute, controller, etc.).
          resources += all_stack_server_resources(stack).select { |x| x["resource_type"] == 'OS::Nova::Server' }
        end
        @all_server_resources = resources
      end

      def all_stack_server_resources(stack)
        # TODO(lsmola) loading this from already obtained nested stack hierarchy will be more effective. This is one
        # extra API call. But we will need to change order of loading, so we have all resources first.
        # Nested depth 50 just for sure, although nobody should nest templates that much
        @orchestration_service.list_resources(:stack => stack, :nested_depth => 50).body['resources']
      end

      def servers
        @servers ||= @connection.servers_for_accessible_tenants
      end

      def hosts
        @hosts ||= @baremetal_service.nodes.details
      end

      def clouds
        @ems.provider.try(:cloud_ems)
      end

      def cloud_ems_hosts_attributes
        hosts_attributes = []
        return hosts_attributes unless clouds

        clouds.each do |cloud_ems|
          compute_hosts = nil
          begin
            cloud_ems.with_provider_connection do |connection|
              compute_hosts = connection.hosts.select { |x| x.service_name == "compute" }
            end<commit_msg>[Fog Deprecation] nodes.details<commit_after>module ManageIQ
  module Providers
    class Openstack::InfraManager::RefreshParser < EmsRefresh::Parsers::Infra
      include Vmdb::Logging

      include ManageIQ::Providers::Openstack::RefreshParserCommon::HelperMethods
      include ManageIQ::Providers::Openstack::RefreshParserCommon::Images
      include ManageIQ::Providers::Openstack::RefreshParserCommon::Objects
      include ManageIQ::Providers::Openstack::RefreshParserCommon::OrchestrationStacks

      def self.ems_inv_to_hashes(ems, options = nil)
        new(ems, options).ems_inv_to_hashes
      end

      def initialize(ems, _options = nil)
        @ems               = ems
        @connection        = ems.connect
        @data              = {}
        @data_index        = {}
        @host_hash_by_name = {}
        @resource_to_stack = {}

        @known_flavors = Set.new

        @os_handle                  = ems.openstack_handle
        @compute_service            = @connection # for consistency
        @baremetal_service          = @os_handle.detect_baremetal_service
        @orchestration_service      = @os_handle.detect_orchestration_service
        @image_service              = @os_handle.detect_image_service
        @storage_service            = @os_handle.detect_storage_service
      end

      def ems_inv_to_hashes
        log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data" \
                     " for EMS name: [#{@ems.name}] id: [#{@ems.id}]"
        $fog_log.info("#{log_header}...")

        get_object_store
        # get_object_store needs to run before load hosts
        load_hosts
        load_orchestration_stacks
        # Cluster processing needs to run after host and stacks processing
        get_clusters
        get_images

        $fog_log.info("#{log_header}...Complete")
        @data
      end

      private

      def all_server_resources
        return @all_server_resources if @all_server_resources
        resources = []
        stacks.each do |stack|
          # Filtering just OS::Nova::Server, which is important to us for getting Purpose of the node
          # (compute, controller, etc.).
          resources += all_stack_server_resources(stack).select { |x| x["resource_type"] == 'OS::Nova::Server' }
        end
        @all_server_resources = resources
      end

      def all_stack_server_resources(stack)
        # TODO(lsmola) loading this from already obtained nested stack hierarchy will be more effective. This is one
        # extra API call. But we will need to change order of loading, so we have all resources first.
        # Nested depth 50 just for sure, although nobody should nest templates that much
        @orchestration_service.list_resources(:stack => stack, :nested_depth => 50).body['resources']
      end

      def servers
        @servers ||= @connection.servers_for_accessible_tenants
      end

      def hosts
        @hosts ||= @baremetal_service.nodes.all
      end

      def clouds
        @ems.provider.try(:cloud_ems)
      end

      def cloud_ems_hosts_attributes
        hosts_attributes = []
        return hosts_attributes unless clouds

        clouds.each do |cloud_ems|
          compute_hosts = nil
          begin
            cloud_ems.with_provider_connection do |connection|
              compute_hosts = connection.hosts.select { |x| x.service_name == "compute" }
            end
{'fraction_non_alphanumeric': 0.08225806451612903, 'fraction_numerical': 0.0011730205278592375, 'mean_word_length': 2.390159045725646, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6721848', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1848, 'n_words': 437}