From 22f10df8d9332f5ff61e7bd32c91121f9c193fb9 Mon Sep 17 00:00:00 2001 From: Zahi Akiva Date: Tue, 18 Jul 2017 11:29:07 +0300 Subject: [PATCH] Convert Container quotas to numeric values --- app/models/container_quota_item.rb | 22 ++++++++++++++++++++++ spec/models/container_quota_item_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 spec/models/container_quota_item_spec.rb diff --git a/app/models/container_quota_item.rb b/app/models/container_quota_item.rb index 413613ccd95..c09e4b6ab9e 100644 --- a/app/models/container_quota_item.rb +++ b/app/models/container_quota_item.rb @@ -1,3 +1,25 @@ class ContainerQuotaItem < ApplicationRecord belongs_to :container_quota + + virtual_column :quota_desired_display, :type => :string + virtual_column :quota_enforced_display, :type => :string + virtual_column :quota_observed_display, :type => :string + + def quota_desired_display + quota_display(quota_desired) + end + + def quota_enforced_display + quota_display(quota_enforced) + end + + def quota_observed_display + quota_display(quota_observed) + end + + private + + def quota_display(quota) + (quota % 1).zero? ? quota.to_i.to_s : quota.to_s + end end diff --git a/spec/models/container_quota_item_spec.rb b/spec/models/container_quota_item_spec.rb new file mode 100644 index 00000000000..dff2892afa2 --- /dev/null +++ b/spec/models/container_quota_item_spec.rb @@ -0,0 +1,23 @@ +describe ContainerQuotaItem do + it "converts float to string" do + quota_item = ContainerQuotaItem.new(:resource => 'cpu', + :quota_desired => 4.2, + :quota_enforced => 1.99, + :quota_observed => 0.01) + + expect(quota_item).to have_attributes(:quota_desired_display => '4.2', + :quota_enforced_display => '1.99', + :quota_observed_display => '0.01') + end + + it "rounds float to int" do + quota_item = ContainerQuotaItem.new(:resource => 'pods', + :quota_desired => 42.0, + :quota_enforced => 1.0, + :quota_observed => 0.0) + + expect(quota_item).to have_attributes(:quota_desired_display => '42', + :quota_enforced_display => '1', + :quota_observed_display => '0') + end +end