Class: CloudCostTracker::Billing::ResourceBillingPolicy

Inherits:
Object
  • Object
show all
Includes:
CloudCostTracker
Defined in:
lib/cloud_cost_tracker/billing/resource_billing_policy.rb

Overview

Implements the logic for billing a single resource. All Billing Policies should inherit from this class, and define #get_cost_for_duration

Constant Summary

Constants included from CloudCostTracker

VERSION

Instance Method Summary collapse

Methods included from CloudCostTracker

account_coding_class, connect_to_database, create_billing_agents, create_coding_agents, env, read_accounts

Constructor Details

#initialize(options = {}) ⇒ ResourceBillingPolicy

Don’t override this method - use #setup instead for one-time behavior

Parameters:

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

    optional parameters:

    • :logger - a Ruby Logger-compatible object



30
31
32
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 30

def initialize(options={})
  @log = options[:logger] || FogTracker.default_logger
end

Instance Method Details

#billing_typeString

Returns the default billing type for this policy. Override this to set a human-readable name for the policy. Defaults to the last part of the subclass name.

Returns:

  • (String)

    a description of the costs incurred under this policy.



50
51
52
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 50

def billing_type
  self.class.name.split('::').last  #(defaluts to class name)
end

#get_cost_for_duration(resource, duration) ⇒ Float

Returns the cost for a particular resource over some duration in seconds.

ALL BILLING POLICY SUBCLASSES SHOULD OVERRIDE THIS METHOD

Parameters:

  • resource (Fog::Model)

    the resource to be billed

  • duration (Integer)

    the number of seconds for this billing period.

Returns:

  • (Float)

    The amount the resource cost over duration seconds.



44
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 44

def get_cost_for_duration(resource, duration) ; 1.0 end

#setup(resources) ⇒ Object

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



37
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 37

def setup(resources) ; end

#write_billing_record_for(resource, hourly_rate, total, start_time, end_time) ⇒ Object

Creates or Updates a BillingRecord for this BillingPolicy’s resource. Don’t override this – it’s called once for each resource by the AccountBillingPolicy.

Parameters:

  • resource (Fog::Model)

    the resource for the record to be written

  • hourly_rate (Float)

    the resource’s hourly rate for this period

  • total (Float)

    the resource’s total cost for this period

  • start_time (Time)

    the start time for any new BillingRecords

  • end_time (Time)

    the start time for any new BillingRecords



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 62

def write_billing_record_for(resource, hourly_rate, total,
  start_time, end_time)
           = resource.
  resource_type   = resource.class.name.split('::').last
  return if total == 0.0  # Write no record if the cost is zero
  new_record = BillingRecord.new(
    :provider       => [:provider],
    :service        => [:service],
    :account        => [:name],
    :resource_id    => resource.identity,
    :resource_type  => resource_type,
    :billing_type   => billing_type,
    :start_time     => start_time,
    :stop_time      => end_time,
    :cost_per_hour  => hourly_rate,
    :total_cost     => total
  )
  new_record.set_codes(resource.billing_codes)
  # Combine BillingRecords within maximim_gap of one another
  write_or_merge_with_existing(new_record, [:delay].to_i)
end