Class: Billing
- Inherits:
-
Object
- Object
- Billing
- Defined in:
- service/showback.rb
Overview
Class for billing through Timeline using different billers
Constant Summary collapse
- BILLERS =
Should be generated automaticaly in the future
[ # Should be generated automaticaly in the future CapacityBiller, DiskBiller, SnapshotsBiller, TrafficBiller ]
Instance Attribute Summary collapse
-
#bill ⇒ Object
readonly
Returns the value of attribute bill.
-
#timeline ⇒ Object
readonly
Returns the value of attribute timeline.
Instance Method Summary collapse
-
#initialize(vm, stime, etime) ⇒ Billing
constructor
Collects existing billers(yet static), filters them by Biller#check_biller and inits Timeline.
-
#make_bill ⇒ Object
Generate monstous Hash with debits for each and every event.
-
#receipt ⇒ Object
Just Adding total to Bill.
-
#total ⇒ Object
Calculates total cost.
Constructor Details
#initialize(vm, stime, etime) ⇒ Billing
Collects existing billers(yet static), filters them by Biller#check_biller and inits Timeline
100 101 102 103 104 105 106 107 108 109 110 |
# File 'service/showback.rb', line 100 def initialize vm, stime, etime @vm = vm @billers = BILLERS.map { | bill | bill.new(@vm) } # Filtering billers to exclude Billers which won't bill anything. # For example there is no Price for Snapshots, so there is no point of billing snapshots for given VM @billers.select! { |bill| bill.check_biller } # Compiling VM Timeline with all of the Events(Records) @timeline = Timeline.new vm, stime, etime @timeline.compile end |
Instance Attribute Details
#bill ⇒ Object (readonly)
Returns the value of attribute bill.
93 94 95 |
# File 'service/showback.rb', line 93 def bill @bill end |
#timeline ⇒ Object (readonly)
Returns the value of attribute timeline.
93 94 95 |
# File 'service/showback.rb', line 93 def timeline @timeline end |
Instance Method Details
#make_bill ⇒ Object
Generate monstous Hash with debits for each and every event
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'service/showback.rb', line 113 def make_bill state, @bill = {}, [] # Every Record can and will modify VM state at current time, and then Biller will add data to bill basing on current state and time to next event @timeline.timeline.each_cons(2) do | curr, con | delta = con.ts - curr.ts curr.mod state bill_rec = { time: con.ts } @billers.each do | biller | bill_rec.merge! biller.bill(bill: bill_rec, state: state, delta: delta, record: curr) end @bill << bill_rec end @bill end |
#receipt ⇒ Object
Just Adding total to Bill
129 130 131 132 133 |
# File 'service/showback.rb', line 129 def receipt @bill.map! do | el | el.merge total: el.without(:time).values.sum end end |
#total ⇒ Object
Calculates total cost
136 137 138 |
# File 'service/showback.rb', line 136 def total @bill.inject(0) { |r, el| r + el[:total] } end |