Class: CFO::Report
- Inherits:
-
Object
- Object
- CFO::Report
- 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
-
#for_account(account_names) ⇒ Object
Sets a constraint based on account name.
-
#for_billing_type(billing_types) ⇒ Object
Sets a constraint based on billing type.
-
#for_provider(providers) ⇒ Object
Sets a constraint based on provider name.
-
#for_resource(resource_ids) ⇒ Object
Sets a constraint based on resource IDs.
-
#for_service(services) ⇒ Object
Sets a constraint based on cloud computing service name.
-
#for_type(resource_types) ⇒ Object
Sets a constraint based on resource type.
-
#initialize(constraint_array = [], groupings = [], options = {}) ⇒ Report
constructor
Creates a object representing a REST Resource for an account.
-
#to_csv ⇒ String
A CSV representation of the desired cost report.
-
#to_hash ⇒ Hash
A Hash representation of the desired cost report.
-
#to_json ⇒ String
A JSON report on the desired costs.
Constructor Details
#initialize(constraint_array = [], groupings = [], options = {}) ⇒ Report
Creates a object representing a REST Resource for an account.
19 20 21 22 23 |
# File 'lib/cfo/resources/report.rb', line 19 def initialize(constraint_array = [], groupings = [], = {}) @constraints = constraint_array @groupings = groupings @log = [:logger] || FogTracker.default_logger(nil) end |
Instance Method Details
#for_account(account_names) ⇒ Object
Sets a constraint based on account name.
96 |
# File 'lib/cfo/resources/report.rb', line 96 def for_account(account_names) ; {:account => account_names} end |
#for_billing_type(billing_types) ⇒ Object
Sets a constraint based on billing type.
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.
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.
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.
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.
92 |
# File 'lib/cfo/resources/report.rb', line 92 def for_type(resource_types) ; {:resource_type => resource_types} end |
#to_csv ⇒ String
Returns 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_hash ⇒ Hash
Returns 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_json ⇒ String
Returns a JSON report on the desired costs.
84 |
# File 'lib/cfo/resources/report.rb', line 84 def to_json ; to_hash.to_json end |