Module: CFO::RecordGrouper

Includes:
CloudCostTracker::Billing
Defined in:
lib/cfo/record_grouper.rb

Overview

Encapsulates sevral algorithms for for changing one set of BillingRecords into another.

Constant Summary collapse

ATTR_4_GROUPING =

Maps the grouping Strings to BillingRecord attributes. Any strings not found as keys in this Hash are interpreted as BillingCode keys.

{
  'account' => :account, 'service' => :service, 'provider' => :provider,
  'type' => :resource_type, 'resource_id' => :resource_id,
  'billing_type' => :billing_type,
}
MULTIPLE_VALUES_VALUE =

Defines the value put assigned to composite record’s attibute when all its commponent records don’t share the same value.

'*'

Class Method Summary collapse

Class Method Details

.by(groupings, input_records) ⇒ Array<BillingRecord>

Returns a set of “composite” BillingRecords based on an Array of groupings. If a grouping is one of [‘account’, ‘provider’, ‘service’, ‘resource_id’, ‘resource_type’, ‘billing_type’], it is evaluated as a Record’s attribute. Otherwise, its treated as a BillingCode key, and evaluated to it’s value.

Parameters:

  • input_records (Array<BillingRecord>)

    the records to group together

  • groupings (Array<String>)

    the paramters to group records by

Returns:

  • (Array<BillingRecord>)

    the composite BillingRecords



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cfo/record_grouper.rb', line 27

def self.by(groupings, input_records)
  # index the results by a "composite key", made by evaluating the groupings
  results = Hash.new
  input_records.each do |record|
    # create the composite key for this record
    comp_key = composite_key_for(record, groupings)
    # if there is already composite record for this key,
    if comp_record = results[comp_key]
      # merge this record's data into the composite record
      results[comp_key] = combine_records(comp_record, record)
    else  # otherwise, create a new composite record
      # index the record into the results with the composite key
      results[comp_key] = record
    end
  end
  # Now output the results in composite key order
  results.keys.sort.map {|comp_key| results[comp_key]}
end