diff --git a/core/settings.rb b/core/settings.rb index 70b3eccc..544ea748 100644 --- a/core/settings.rb +++ b/core/settings.rb @@ -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 diff --git a/lib/std++/main.rb b/lib/std++/main.rb index dc9ce18b..7ac2a4e6 100644 --- a/lib/std++/main.rb +++ b/lib/std++/main.rb @@ -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| @@ -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| @@ -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] diff --git a/service/objects/vm.rb b/service/objects/vm.rb index 114ebdc4..11655f56 100644 --- a/service/objects/vm.rb +++ b/service/objects/vm.rb @@ -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 @@ -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 diff --git a/service/objects/vn.rb b/service/objects/vn.rb index b650cf3e..6d5c0797 100644 --- a/service/objects/vn.rb +++ b/service/objects/vn.rb @@ -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