<commit_before>module CloudCostTracker

  # Tracks the resources in one or more Fog accounts in an ActiveRecord database
  class Tracker

    # each CloudCostTracker is a thin wrapper around it's @tracker,
    require 'fog_tracker'     # which is a FogTracker:Tracker
    extend Forwardable        # most public methods are delegated
    def_delegators :@tracker,:start,:stop,:query,:[],:update,
            :running?,:types_for_account,:logger

    attr_reader :accounts

    # Creates an object for tracking Fog accounts in an ActiveRecord database
    # @param [Hash] accounts a Hash of account information
    #    (see accounts.yml.example)
    # @param [Hash] options optional additional parameters:
    #  - :delay (Integer) - Override time between polling of accounts
    #    (should take a single Exception as its only required parameter)
    #  - :logger - a Ruby Logger-compatible object
    def initialize(accounts = {}, options={})
      @delay    = options[:delay]
      @log      = options[:logger] || FogTracker.default_logger
      @running  = false
      @accounts = setup_fog_tracker(accounts)
    end

    private

    # Creates a FogTracker::Tracker that calls bill_for_account
    # each time an account is refreshed
    def setup_fog_tracker(accounts)
      @tracker = FogTracker::Tracker.new(accounts,
        {:delay => @delay, :logger => @log,
          :callback => Proc.new do |resources|
            checked_resources = sanity_check(resources)
            code_resources(checked_resources)
            bill_for_resources(checked_resources)
          end
        }
      )
      @tracker.accounts
    end


    # Make sure returned resources are billable and codeable
    # @param [Array<Fog::Model>] resources the resources to check
    # @return [Array<Fog::Model>] the resources deemed OK
    def sanity_check(resources)
      resources.select do |resource|
        if resource.identity == nil
          @log.warn "Found a Fog::Model with no identity. Removing it..."
          @log.debug "BAD Fog::Model instance: #{resource.inspect}"
          false   # return false from select to indicate resource is bad
        else
          true    # return true from select to indicate resource is OK
        end
      end
    end

    # Adds billing codes for all resources
    def code_resources(resources)
      return if resources.empty?<commit_msg>Improve debug message for bad Fog models<commit_after>module CloudCostTracker

  # Tracks the resources in one or more Fog accounts in an ActiveRecord database
  class Tracker

    # each CloudCostTracker is a thin wrapper around it's @tracker,
    require 'fog_tracker'     # which is a FogTracker:Tracker
    extend Forwardable        # most public methods are delegated
    def_delegators :@tracker,:start,:stop,:query,:[],:update,
            :running?,:types_for_account,:logger

    attr_reader :accounts

    # Creates an object for tracking Fog accounts in an ActiveRecord database
    # @param [Hash] accounts a Hash of account information
    #    (see accounts.yml.example)
    # @param [Hash] options optional additional parameters:
    #  - :delay (Integer) - Override time between polling of accounts
    #    (should take a single Exception as its only required parameter)
    #  - :logger - a Ruby Logger-compatible object
    def initialize(accounts = {}, options={})
      @delay    = options[:delay]
      @log      = options[:logger] || FogTracker.default_logger
      @running  = false
      @accounts = setup_fog_tracker(accounts)
    end

    private

    # Creates a FogTracker::Tracker that calls bill_for_account
    # each time an account is refreshed
    def setup_fog_tracker(accounts)
      @tracker = FogTracker::Tracker.new(accounts,
        {:delay => @delay, :logger => @log,
          :callback => Proc.new do |resources|
            checked_resources = sanity_check(resources)
            code_resources(checked_resources)
            bill_for_resources(checked_resources)
          end
        }
      )
      @tracker.accounts
    end


    # Make sure returned resources are billable and codeable
    # @param [Array<Fog::Model>] resources the resources to check
    # @return [Array<Fog::Model>] the resources deemed OK
    def sanity_check(resources)
      resources.select do |resource|
        if resource.identity == nil
          @log.warn "Found a #{resource.class} with no identity. Removing it..."
          @log.debug "BAD #{resource.class} instance: #{resource.inspect}"
          false   # return false from select to indicate resource is bad
        else
          true    # return true from select to indicate resource is OK
        end
      end
    end

    # Adds billing codes for all resources
    def code_resources(resources)
      return if resources.empty?
{'fraction_non_alphanumeric': 0.07841501564129301, 'fraction_numerical': 0.0, 'mean_word_length': 2.7615686274509805, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5265784', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1268, 'n_words': 439}