Class: CloudCostTracker::Billing::BillingRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/cloud_cost_tracker/models/billing_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.most_recent_like(billing_record) ⇒ BillingRecord?

Returns the most recent BillingRecord for the same resource as the billing_record, or nil of there are none. Note that if billing_record is already in the database, this method can return billing_record itself!

Parameters:

Returns:

  • (BillingRecord, nil)

    the latest “matching” BillingRecord



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloud_cost_tracker/models/billing_record.rb', line 20

def self.most_recent_like(billing_record)
  results = BillingRecord.where(
    :provider       => billing_record.provider,
    :service        => billing_record.service,
    :account        => billing_record.,
    :resource_id    => billing_record.resource_id,
    :resource_type  => billing_record.resource_type,
    :billing_type   => billing_record.billing_type,
  ).order(:stop_time).reverse_order.limit(1)
  results.empty? ? nil : results.first
end

Instance Method Details

#merge_with(other_billing_record) ⇒ Object

Changes the stop time of this BillingRecord to be that of the other_billing_record, then recalculates the total



48
49
50
51
52
53
# File 'lib/cloud_cost_tracker/models/billing_record.rb', line 48

def merge_with(other_billing_record)
  self.stop_time = other_billing_record.stop_time
  total = ((stop_time - start_time) * cost_per_hour) / 3600
  self.total_cost = total
  ActiveRecord::Base.connection_pool.with_connection {save!}
end

#overlaps_with(other_billing_record, min_proximity = 0) ⇒ Object

Returns true if this BillingRecord’s duration overlaps (inclusively) with that of other_billing_record. The minimum amount of time that can be between the records and still allow them to “overlap” can be increased



35
36
37
38
39
40
41
42
43
44
# File 'lib/cloud_cost_tracker/models/billing_record.rb', line 35

def overlaps_with(other_billing_record, min_proximity = 0)
  return true if start_time == other_billing_record.start_time
  first, second = nil, nil  # Which record started first?
  if start_time < other_billing_record.start_time
    first, second = self, other_billing_record
  else
    first, second = other_billing_record, self
  end
  second.start_time - first.stop_time <= min_proximity
end

#set_codes(codes) ⇒ Object

Creates BillngCodes as necessary from codes, an Array of String pairs, and associates this BillingRecord with those BillingCodes.

Parameters:

  • codes (Array <Array <String>>)

    the String pairs to use as codes



58
59
60
61
62
63
64
65
66
# File 'lib/cloud_cost_tracker/models/billing_record.rb', line 58

def set_codes(codes)
  codes.each do |key, value|
    self.billing_codes << ( # Find the matching BillingCode or make a new one
      ActiveRecord::Base.connection_pool.with_connection do
        BillingCode.where(:key => key, :value => value).first_or_create!
      end
    )
  end
end

#to_hashHash

Returns a Hash representation of this BillingRecord.

Returns:

  • (Hash)

    a Hash representation of this BillingRecord.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloud_cost_tracker/models/billing_record.rb', line 69

def to_hash
  {
    'id'            => id,
    'provider'      => provider,
    'service'       => service,
    'account'       => ,
    'resource_id'   => resource_id,
    'resource_type' => resource_type,
    'billing_type'  => billing_type,
    'start_time'    => start_time,
    'stop_time'     => stop_time,
    'cost_per_hour' => cost_per_hour,
    'total_cost'    => total_cost,
    'billing_codes' => billing_codes.map {|c| [c.key, c.value]}
  }
end