From 0594dda06762abc23dc5e43e256e1899e9e4edfa Mon Sep 17 00:00:00 2001 From: Mik Date: Thu, 29 Apr 2021 19:32:28 +0200 Subject: [PATCH] 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,