Class: CloudCostTracker::Billing::AccountBillingPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_cost_tracker/billing/account_billing_policy.rb

Overview

Implements the logic for billing all resources in a single account. Initializes the necessary ResourceBillingPolicy objects, sorts the resources by policy, and calls get_cost_for_duration twice on each resource’s policy to compute the charges.

Instance Method Summary collapse

Constructor Details

#initialize(resources, options = {}) ⇒ AccountBillingPolicy

Creates an object that implements the default account billing policy

Parameters:

  • resources (Array <Fog::Model>)

    the resources to bill for

  • options (Hash) (defaults to: {})

    optional parameters:

    • :logger - a Ruby Logger-compatible object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cloud_cost_tracker/billing/account_billing_policy.rb', line 13

def initialize(resources, options={})
  @log = options[:logger] || FogTracker.default_logger
  setup_resource_billing_agents(resources)
  # Initialize data structures for caching costs
  # the hourly cost for each resource, and billing agent
  @hourly_cost  = Hash.new # @hourly_cost[resource][agent]
  # the total cost for each resource, and billing agent
  @total_cost   = Hash.new # @total_cost[resource][agent]
  resources.each do |resource|
    @hourly_cost[resource] ||= Hash.new
    @total_cost[resource]  ||= Hash.new
  end
  @account = resources.first. # Save account info
end

Instance Method Details

#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

#setup(resources) ⇒ Object

An initializer called by the framework once per billing cycle. Override this method if you need to perform high-latency operations, like network transactions, that should not be performed per-resource.



31
# File 'lib/cloud_cost_tracker/billing/account_billing_policy.rb', line 31

def setup(resources) ; end