<commit_before>module Api
  class Initializer
    #
    # Custom normalization on these attribute types.
    # Converted to normalized_attributes hash at init, much faster access.
    #
    ATTR_TYPES = {
      :time      => %w(expires_on),
      :url       => %w(href),
      :resource  => %w(image_href),
      :encrypted => %w(password) |
                    ::MiqRequestWorkflow.all_encrypted_options_fields.map(&:to_s) |
                    ::Vmdb::Settings::PASSWORD_FIELDS.map(&:to_s)
    }

    def go
      init_env
      gen_attr_type_hash
    end

    def log_kv(key, val, pref = "")
      $api_log.info("#{pref}  #{key.to_s.ljust([24, key.to_s.length].max, ' ')}: #{val}")
    end

    #
    # Initializing REST API environment, called once @ startup
    #
    def init_env
      $api_log.info("Initializing Environment for #{Api::Settings.base[:name]}")
      log_config
    end

    def log_config
      $api_log.info("")
      $api_log.info("Static Configuration")
      Api::Settings.base.each { |key, val| log_kv(key, val) }

      $api_log.info("")
      $api_log.info("Dynamic Configuration")
      Api.user_token_service.api_config.each { |key, val| log_kv(key, val) }
    end

    #
    # Let's create our attribute type hashes.
    # Accessed as normalized_attributes[<name>], much faster than array include?
    #
    def gen_attr_type_hash
      ATTR_TYPES.each { |type, attrs| attrs.each { |a| Api.normalized_attributes[type][a] = true } }
      gen_time_attr_type_hash
    end

    #
    # Let's dynamically get the :date and :datetime attributes from the Classes we care about.
    #
    def gen_time_attr_type_hash
      Api::Settings.collections.each do |_, cspec|
        next if cspec[:klass].blank?
        klass = cspec[:klass].constantize
        klass.columns_hash.collect do |name, typeobj|
          Api.normalized_attributes[:time][name] = true if %w(date datetime).include?(typeobj.type.to_s)
        end
      end
    end
  end
end
<commit_msg>Move ATTR_TYPES into a method so it's not evaluated at load time<commit_after>module Api
  class Initializer
    def go
      init_env
      gen_attr_type_hash
    end

    def log_kv(key, val, pref = "")
      $api_log.info("#{pref}  #{key.to_s.ljust([24, key.to_s.length].max, ' ')}: #{val}")
    end

    #
    # Initializing REST API environment, called once @ startup
    #
    def init_env
      $api_log.info("Initializing Environment for #{Api::Settings.base[:name]}")
      log_config
    end

    def log_config
      $api_log.info("")
      $api_log.info("Static Configuration")
      Api::Settings.base.each { |key, val| log_kv(key, val) }

      $api_log.info("")
      $api_log.info("Dynamic Configuration")
      Api.user_token_service.api_config.each { |key, val| log_kv(key, val) }
    end

    #
    # Let's create our attribute type hashes.
    # Accessed as normalized_attributes[<name>], much faster than array include?
    #
    def gen_attr_type_hash
      attr_types.each { |type, attrs| attrs.each { |a| Api.normalized_attributes[type][a] = true } }
      gen_time_attr_type_hash
    end

    #
    # Let's dynamically get the :date and :datetime attributes from the Classes we care about.
    #
    def gen_time_attr_type_hash
      Api::Settings.collections.each do |_, cspec|
        next if cspec[:klass].blank?
        klass = cspec[:klass].constantize
        klass.columns_hash.collect do |name, typeobj|
          Api.normalized_attributes[:time][name] = true if %w(date datetime).include?(typeobj.type.to_s)
        end
      end
    end

    private

    #
    # Custom normalization on these attribute types.
    # Converted to normalized_attributes hash at init, much faster access.
    #
    def attr_types
      @attr_types ||= {
        :time      => %w(expires_on),
        :url       => %w(href),
        :resource  => %w(image_href),
        :encrypted => %w(password) |
                      ::MiqRequestWorkflow.all_encrypted_options_fields.map(&:to_s) |
                      ::Vmdb::Settings::PASSWORD_FIELDS.map(&:to_s)
      }
    end
  end
end

{'fraction_non_alphanumeric': 0.11770475723393821, 'fraction_numerical': 0.000980872976949485, 'mean_word_length': 2.5624454148471614, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '5794219', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1335, 'n_words': 297}