Method: CloudCostTracker::Billing::AccountBillingPolicy#bill_for

Defined in:
lib/cloud_cost_tracker/billing/account_billing_policy.rb

#bill_for(resources) ⇒ Object

Defines the default method for billing all resources



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloud_cost_tracker/billing/account_billing_policy.rb', line 34

def bill_for(resources)
  return if resources.empty?
  setup_resource_billing_agents(resources)
  delay         = @account[:delay].to_i
  bill_end      = Time.now
  bill_start    = @account[:preceeding_update_time] || (bill_end - delay)
  bill_duration = bill_end - bill_start
  # calculate the hourly and total cost for all resources, by type
  @agents.keys.each do |resource_class|
    collection = resources.select {|r| r.class == resource_class}
    collection_name = collection.first.collection.class.name.split('::').last
    @log.info "Computing costs for #{collection.size}"+
      " #{collection_name} in account #{@account[:name]}"
    collection.each do |resource|
      @log.debug "Computing costs for #{resource.tracker_description}"+
        " in account #{@account[:name]}"
      @agents[resource.class].each do |billing_agent|
        @total_cost[resource][billing_agent] = billing_agent.
          get_cost_for_duration(resource, bill_duration).round(PRECISION)
        @hourly_cost[resource][billing_agent] = billing_agent.
          get_cost_for_duration(resource, SECONDS_PER_HOUR).round(PRECISION)
      @log.debug "Computed costs for #{resource.tracker_description}"+
        " in account #{@account[:name]}"
      end
    end
    @log.info "Computed costs for #{collection.size}"+
      " #{collection_name} in account #{@account[:name]}"
  end
  write_records_for(resources, bill_start, bill_end)
end