Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #120 from ione-cloud/pre-piad-billing-reducer
Browse files Browse the repository at this point in the history
Pre-Paid Billing Reducer
  • Loading branch information
slntopp authored May 3, 2021
2 parents 4176ce4 + c00201b commit 7b0598f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
['USERS_DEFAULT_LANG', "en_US", "Default locale for new users", 1, "str"],
['VCENTER_CPU_LIMIT_FREQ_PER_CORE', "{\"default\":2000}", "Frequency per Core limit for different Nodes(don't remove default)", 1, "object"],
['VCENTER_DRIVES_IOPS', "{\"HDD\":350,\"SSD\":1000}", "IOPs limits for Drive types", 1, "object"],
['VNETS_TEMPLATES', "{}", "VNs Types to VNs Templates mapping(types must be upper case)", 1, "object"]
['VNETS_TEMPLATES', "{}", "VNs Types to VNs Templates mapping(types must be upper case)", 1, "object"],
['PRE_PAID_REDUCE_FACTOR', "{\"0\": 1}", "Reduce factor for Pre-Paid VMs depending on billing period", 1, "object"]
]
required.each do | record |
begin
Expand Down
13 changes: 11 additions & 2 deletions lib/std++/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def debug_out

# @!group Dev Tools

# Replaces string keys with symbol keys
# Replaces string keys with Symbol keys
# @return [Hash]
def to_sym!
self.keys.each do |key|
Expand All @@ -28,7 +28,7 @@ def to_sym!
self
end

# Replaces all keys with string keys
# Replaces all keys with String keys
# @return [Hash]
def to_s!
self.keys.each do |key|
Expand All @@ -37,6 +37,15 @@ def to_s!
self
end

# Converts all keys to Integer
# @return [Hash]
def keys_to_i!
self.keys.each do |key|
self[key.to_i] = self.delete key if key.class != Integer
end
self
end

# Returns array of values with given keys
# @param [Array] keys - Array of values
# @return [Array]
Expand Down
20 changes: 18 additions & 2 deletions service/objects/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ def calculate_showback stime_req, etime_req, _group_by_day = false
}
elsif bp.include? 'PRE' then
curr = self['/VM/STIME'].to_i
delta = bp.split('_')[1].to_i * 86400
period = bp.split('_')[1].to_i
delta = period * 86400

total = 0

Expand All @@ -412,10 +413,25 @@ def calculate_showback stime_req, etime_req, _group_by_day = false
curr += delta
end

reduce_factor = 1
reduce_factors = IONe::Settings['PRE_PAID_REDUCE_FACTOR'].keys_to_i!.sort.to_h

reduce_factors.each do | period_key, factor |
if period >= period_key then
reduce_factor = factor
else
break
end
end
reduce_factor = reduce_factor.to_f

return {
id: id, name: name,
TOTAL: total
total_billed: total, reduce_factor: reduce_factor,
TOTAL: total * reduce_factor
}
else
raise ShowbackError, ["Unknown BILLING_PERIOD!", bp]
end
end

Expand Down
2 changes: 2 additions & 0 deletions service/objects/vn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def ar_pool
pool = to_hash['VNET']['AR_POOL']['AR']
if pool.class == Hash then
return [pool]
elsif pool.nil? then
return []
else
return pool
end
Expand Down

0 comments on commit 7b0598f

Please sign in to comment.