From a268033ef5ad2c2c9c674e8213322251ffbdd907 Mon Sep 17 00:00:00 2001 From: Mik Date: Thu, 29 Apr 2021 16:02:25 +0200 Subject: [PATCH 1/6] base --- service/objects/vm.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service/objects/vm.rb b/service/objects/vm.rb index 114ebdc4..04822898 100644 --- a/service/objects/vm.rb +++ b/service/objects/vm.rb @@ -414,7 +414,8 @@ def calculate_showback stime_req, etime_req, _group_by_day = false return { id: id, name: name, - TOTAL: total + total_billed: total, reduce_factor: reduce_factor, + TOTAL: total * reduce_factor } end end From 3ea473305ebe188ebe28bf5fd808f765d81728cd Mon Sep 17 00:00:00 2001 From: Mik Date: Thu, 29 Apr 2021 18:37:51 +0200 Subject: [PATCH 2/6] handle unknown billing period --- service/objects/vm.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/service/objects/vm.rb b/service/objects/vm.rb index 04822898..30a5a40c 100644 --- a/service/objects/vm.rb +++ b/service/objects/vm.rb @@ -417,6 +417,8 @@ def calculate_showback stime_req, etime_req, _group_by_day = false total_billed: total, reduce_factor: reduce_factor, TOTAL: total * reduce_factor } + else + raise ShowbackError, ["Unknown BILLING_PERIOD!", bp] end end From 4de7cac7f5d64ca4d30908c30331d636c6b8e450 Mon Sep 17 00:00:00 2001 From: Mik Date: Thu, 29 Apr 2021 19:16:36 +0200 Subject: [PATCH 3/6] doc --- lib/std++/main.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std++/main.rb b/lib/std++/main.rb index dc9ce18b..d124ce96 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| From 16e0b5f0f4b8c89a97df2d4bd8cadd6b297c45cb Mon Sep 17 00:00:00 2001 From: Mik Date: Thu, 29 Apr 2021 19:16:53 +0200 Subject: [PATCH 4/6] Hash#keys_to_i! method --- lib/std++/main.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/std++/main.rb b/lib/std++/main.rb index d124ce96..7ac2a4e6 100644 --- a/lib/std++/main.rb +++ b/lib/std++/main.rb @@ -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] From 0594dda06762abc23dc5e43e256e1899e9e4edfa Mon Sep 17 00:00:00 2001 From: Mik Date: Thu, 29 Apr 2021 19:32:28 +0200 Subject: [PATCH 5/6] PRE_PAID_REDUCE_FACTOR settings and functionality --- core/settings.rb | 3 ++- service/objects/vm.rb | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) 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/service/objects/vm.rb b/service/objects/vm.rb index 30a5a40c..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,6 +413,18 @@ 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_billed: total, reduce_factor: reduce_factor, From c00201bef08bb47344d25706299cdd620002a8e6 Mon Sep 17 00:00:00 2001 From: Mik Date: Fri, 30 Apr 2021 18:18:20 +0200 Subject: [PATCH 6/6] handle no ars network --- service/objects/vn.rb | 2 ++ 1 file changed, 2 insertions(+) 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