Class: CloudCostTracker::Billing::Storage::AWS::DirectoryBillingPolicy

Inherits:
ResourceBillingPolicy show all
Defined in:
lib/cloud_cost_tracker/billing/storage/aws/directories.rb

Overview

The default billing policy for Amazon S3 buckets

Constant Summary collapse

CENTS_PER_GB_PER_MONTH =

The YAML pricing data is read from config/billing

YAML.load(File.read File.join(
CONSTANTS_DIR, 'storage-aws-directories.yml'))

Constants included from CloudCostTracker

VERSION

Instance Method Summary collapse

Methods inherited from ResourceBillingPolicy

#initialize, #write_billing_record_for

Methods included from CloudCostTracker

account_coding_class, connect_to_database, create_billing_agents, create_coding_agents, env, read_accounts

Constructor Details

This class inherits a constructor from CloudCostTracker::Billing::ResourceBillingPolicy

Instance Method Details

#billing_typeObject



41
# File 'lib/cloud_cost_tracker/billing/storage/aws/directories.rb', line 41

def billing_type ; "S3 Bucket storage" end

#get_cost_for_duration(s3_bucket, duration) ⇒ Object

Returns the runtime cost for a given S3 bucket over some duration (in seconds)



13
14
15
16
# File 'lib/cloud_cost_tracker/billing/storage/aws/directories.rb', line 13

def get_cost_for_duration(s3_bucket, duration)
  CENTS_PER_GB_PER_MONTH[zone(s3_bucket)] * total_size(s3_bucket) *
  duration / SECONDS_PER_MONTH
end

#setup(resources) ⇒ Object

Remembers each bucket size, because iterating over S3 objects is slow, and get_cost_for_duration is called twice



37
38
39
# File 'lib/cloud_cost_tracker/billing/storage/aws/directories.rb', line 37

def setup(resources)
  @bucket_size = Hash.new
end

#total_size(bucket) ⇒ Object

Returns the total size of all a bucket’s objecs, in GB



24
25
26
27
28
29
30
31
32
33
# File 'lib/cloud_cost_tracker/billing/storage/aws/directories.rb', line 24

def total_size(bucket)
  # check for saved value
  return @bucket_size[bucket] if @bucket_size[bucket]
  @log.debug "Computing size for #{bucket.tracker_description}"
  total_bytes = 0
  bucket.files.each {|object| total_bytes += object.content_length}
  @log.debug "total bytes = #{total_bytes}"
  # save the total size for later
  @bucket_size[bucket] = total_bytes / BYTES_PER_GB.to_f
end

#zone(resource) ⇒ Object

Chops the availability zone letter from the region



19
20
21
# File 'lib/cloud_cost_tracker/billing/storage/aws/directories.rb', line 19

def zone(resource)
  'us-east-1'
end