Class: CFO::Report

Inherits:
Object
  • Object
show all
Includes:
CloudCostTracker::Billing
Defined in:
lib/cfo/resources/report.rb

Overview

Represents a JSON report, based on an Array of (Hash) constraints.

Instance Method Summary collapse

Constructor Details

#initialize(constraint_array = [], groupings = [], options = {}) ⇒ Report

Creates a object representing a REST Resource for an account.

Parameters:

  • constraint_array (Array<Hash>) (defaults to: [])

    a set of Hash constraints on the BillingRecords to be retrieved. Each hash key is a type of constraint on the type of BillingRecord information that is returned. The value is an array of Strings, which are generally just text to search for in some BillingRecord column, but are interpreted differently depending on the type of constraint.

  • groupings (Array<String>) (defaults to: [])

    a set of Strings for grouping the results. Strings that match BillingRecord attributes will group all records that share the same value for that attribute into one record. Other Strings will can match BillingCode keys, for grouping by Code.

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

    optional additional parameters:

    • :logger - a Ruby Logger-compatible object



19
20
21
22
23
# File 'lib/cfo/resources/report.rb', line 19

def initialize(constraint_array = [], groupings = [], options = {})
  @constraints = constraint_array
  @groupings = groupings
  @log = options[:logger] || FogTracker.default_logger(nil)
end

Instance Method Details

#for_account(account_names) ⇒ Object

Sets a constraint based on account name.

Parameters:

  • account_names (Array<String>)

    the values to match in the records.



96
# File 'lib/cfo/resources/report.rb', line 96

def () ;  {:account => } end

#for_billing_type(billing_types) ⇒ Object

Sets a constraint based on billing type.

Parameters:

  • billing_types (Array<String>)

    the values to match in the records



108
# File 'lib/cfo/resources/report.rb', line 108

def for_billing_type(billing_types) ; {:billing_type => billing_types} end

#for_provider(providers) ⇒ Object

Sets a constraint based on provider name.

Parameters:

  • providers (Array<String>)

    the values to match in the records.



100
# File 'lib/cfo/resources/report.rb', line 100

def for_provider(providers) ;     {:provider => providers} end

#for_resource(resource_ids) ⇒ Object

Sets a constraint based on resource IDs.

Parameters:

  • resource_ids (Array<String>)

    the values to match in the records.



88
# File 'lib/cfo/resources/report.rb', line 88

def for_resource(resource_ids) ;  {:resource_id => resource_ids} end

#for_service(services) ⇒ Object

Sets a constraint based on cloud computing service name.

Parameters:

  • services (Array<String>)

    the values to match in the records.



104
# File 'lib/cfo/resources/report.rb', line 104

def for_service(services) ;       {:service => services} end

#for_type(resource_types) ⇒ Object

Sets a constraint based on resource type.

Parameters:

  • resource_types (Array<String>)

    the values to match in the records.



92
# File 'lib/cfo/resources/report.rb', line 92

def for_type(resource_types) ;    {:resource_type => resource_types} end

#to_csvString

Returns a CSV representation of the desired cost report.

Returns:

  • (String)

    a CSV representation of the desired cost report.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cfo/resources/report.rb', line 45

def to_csv
  update_records            # Update @records from the database
  code_count = @records.inject({}) do |count, record|
    record.billing_codes.each do |code|
      count[code.key] = count[code.key] ? count[code.key] + 1 : 1
    end
    count
  end
  sorted_codes = code_count.keys.sort {|a,b| code_count[b] <=> code_count[a]}
  headers = {
    :start_time => 'Start Time',
    :stop_time => 'Stop Time',
    :account => 'Account Name',
    :provider => 'Provder',
    :service => 'Service',
    :billing_type => 'Billing Type',
    :total_cost => 'Total Cost',
    :cost_per_hour => 'Hourly Rate',
    :resource_id => 'Resource ID',
    :resource_type => 'Resource Type',
  }
  @log.info "Using headers: #{headers.values + sorted_codes}"
  csv = ((headers.values + sorted_codes).map{|h| "\"#{h}\""}).join(',') +"\n"
  @records.each do |record|
    record_data = headers.keys.map {|h| record.send h}
    record_data += sorted_codes.map do |code|
      billing_codes = record.billing_codes.select {|c| c.key == code}
      case billing_codes.count
      when 0 then ''
      when 1 then billing_codes.first.value
      else CFO::RecordGrouper::MULTIPLE_VALUES_VALUE
      end
    end
    csv += (record_data.map{|value| "\"#{value}\""}).join(',') +"\n"
  end
  csv
end

#to_hashHash

Returns a Hash representation of the desired cost report.

Returns:

  • (Hash)

    a Hash representation of the desired cost report.



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

def to_hash
  update_records            # Update @records from the database
  update_summary_attributes # Update summary attributes
  {
    'start_time'      => @start_time,
    'stop_time'       => @stop_time,
    'total_cost'      => @total_cost,
    'cost_per_hour'   => @hourly_rate.to_f,
    'billing_codes'   => @bill_codes,
    'billing_records' => @records.map do |r|
                            r.to_hash.merge(
                              :cost_per_hour  => r.cost_per_hour.to_f,
                              :total_cost     => r.total_cost.to_f,
                            )
                          end,
  }
end

#to_jsonString

Returns a JSON report on the desired costs.

Returns:

  • (String)

    a JSON report on the desired costs.



84
# File 'lib/cfo/resources/report.rb', line 84

def to_json ; to_hash.to_json end