Class: Billing

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#billObject (readonly)

Returns the value of attribute bill.



93
94
95
# File 'service/showback.rb', line 93

def bill
  @bill
end

#timelineObject (readonly)

Returns the value of attribute timeline.



93
94
95
# File 'service/showback.rb', line 93

def timeline
  @timeline
end

Instance Method Details

#make_billObject

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

#receiptObject

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

#totalObject

Calculates total cost



136
137
138
# File 'service/showback.rb', line 136

def total
  @bill.inject(0) { |r, el| r + el[:total] }
end