From: martyntaylor <[email protected]>

---
 src/app/models/quota.rb                |   10 +++++++
 src/app/services/data_service.rb       |   41 ++++++++++++++++++++++++++++---
 src/spec/services/data_service_spec.rb |   33 ++++++++++++++++++++++---
 3 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/src/app/models/quota.rb b/src/app/models/quota.rb
index 324a384..acced11 100644
--- a/src/app/models/quota.rb
+++ b/src/app/models/quota.rb
@@ -25,6 +25,16 @@ class Quota < ActiveRecord::Base
 
   NO_LIMIT = nil
 
+  RESOURCE_RUNNING_INSTANCES = "running_instances"
+  RESOURCE_RUNNING_MEMORY = "running_memory"
+  RESOURCE_RUNNING_CPUS = "running_cpus"
+  RESOURCE_TOTAL_INSTANCES = "total_instances"
+  RESOURCE_TOTAL_STORAGE = "total_storage"
+  RESOURCE_OVERALL = "total_storage"
+
+  RESOURCE_NAMES = [ RESOURCE_RUNNING_INSTANCES, RESOURCE_RUNNING_MEMORY, 
RESOURCE_RUNNING_CPUS,
+                RESOURCE_TOTAL_INSTANCES, RESOURCE_TOTAL_STORAGE ]
+
   has_one :pool
   has_one :cloud_account
 
diff --git a/src/app/services/data_service.rb b/src/app/services/data_service.rb
index 6465f2e..776fef7 100644
--- a/src/app/services/data_service.rb
+++ b/src/app/services/data_service.rb
@@ -60,12 +60,45 @@ class DataService
     return QoSDataPoint.new(time, average_time, maximum_time, minimum_time)
   end
 
-  # Returns the Used and Maximum Number of Instances in Quota
-  def self.quota_utilisation(parent)
+  # Returns the Used and Maximum Resource Usage
+  def self.quota_utilisation(parent, resource_name)
     quota = parent.quota
-    if quota
-      return QuotaUsagePoint.new(quota.total_instances, 
quota.maximum_total_instances)
+
+    case resource_name
+      when Quota::RESOURCE_RUNNING_INSTANCES
+        return QuotaUsagePoint.new(quota.running_instances, 
quota.maximum_running_instances)
+      when Quota::RESOURCE_RUNNING_MEMORY
+        return QuotaUsagePoint.new(quota.running_memory.to_f, 
quota.maximum_running_memory.to_f)
+      when Quota::RESOURCE_RUNNING_CPUS
+        return QuotaUsagePoint.new(quota.running_cpus.to_f, 
quota.maximum_running_cpus.to_f)
+      when Quota::RESOURCE_TOTAL_INSTANCES
+        return QuotaUsagePoint.new(quota.total_instances, 
quota.maximum_total_instances)
+      when Quota::RESOURCE_TOTAL_STORAGE
+        return QuotaUsagePoint.new(quota.total_storage.to_f, 
quota.maximum_total_storage.to_f)
+      when Quota::RESOURCE_OVERALL
+        return self.overall_usage(parent)
+      else
+        return nil
+    end
+  end
+
+  def self.overall_usage(parent)
+    usage_points = []
+    Quota::RESOURCE_NAMES.each do |resource_name|
+      usage_points << quota_utilisation(parent, resource_name)
+    end
+
+    worst_case = nil
+    usage_points.each do |usage_point|
+      if worst_case
+        if ((worst_case.max / 100) * worst_case.used) < ((usage_point.max / 
100) * usage_point.used)
+          worst_case = usage_point
+        end
+      else
+        worst_case = usage_point
+      end
     end
+    return worst_case
   end
 
   def self.total_quota_utilisation(provider)
diff --git a/src/spec/services/data_service_spec.rb 
b/src/spec/services/data_service_spec.rb
index f69f8e8..56bbc41 100644
--- a/src/spec/services/data_service_spec.rb
+++ b/src/spec/services/data_service_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
 
 describe DataService do
 
-  it "should calculate the quota usage for a provider with a numbner of cloud 
accounts" do
+  it "should calculate the total instance quota usage for a provider with a 
numbner of cloud accounts" do
     client = mock('DeltaCloud', :null_object => true)
     provider = Factory.build(:mock_provider)
     provider.stub!(:connect).and_return(client)
@@ -30,7 +30,7 @@ describe DataService do
 
   end
 
-  it "should calculate the total number of instances and maximum numbder of 
instances of a cloud account" do
+  it "should calculate the total number of instances and maximum number of 
instances of a cloud account" do
     client = mock('DeltaCloud', :null_object => true)
     provider = Factory.build(:mock_provider)
     provider.stub!(:connect).and_return(client)
@@ -40,11 +40,36 @@ describe DataService do
     cloud_account.stub!(:valid_credentials?).and_return(true)
     cloud_account.save!
 
-    quota = Factory(:quota, :maximum_total_instances => 50, :total_instances 
=> 20)
+    quota = Factory(:quota,
+                    :maximum_running_instances => 40,
+                    :maximum_running_memory => 10240,
+                    :maximum_running_cpus => 10,
+                    :maximum_total_instances => 50,
+                    :maximum_total_storage => 500,
+                    :running_instances => 20,
+                    :running_memory => 4096,
+                    :running_cpus => 7,
+                    :total_instances => 20,
+                    :total_storage => 499)
     cloud_account.quota_id = quota.id
 
-    data_point = DataService.quota_utilisation(cloud_account)
+    data_point = DataService.quota_utilisation(cloud_account, 
Quota::RESOURCE_RUNNING_INSTANCES)
+    data_point.should == DataService::QuotaUsagePoint.new(20, 40)
+
+    data_point = DataService.quota_utilisation(cloud_account, 
Quota::RESOURCE_RUNNING_MEMORY)
+    data_point.should == DataService::QuotaUsagePoint.new(4096, 10240)
+
+    data_point = DataService.quota_utilisation(cloud_account, 
Quota::RESOURCE_RUNNING_CPUS)
+    data_point.should == DataService::QuotaUsagePoint.new(7, 10)
+
+    data_point = DataService.quota_utilisation(cloud_account, 
Quota::RESOURCE_TOTAL_INSTANCES)
     data_point.should == DataService::QuotaUsagePoint.new(20, 50)
+
+    data_point = DataService.quota_utilisation(cloud_account, 
Quota::RESOURCE_TOTAL_STORAGE)
+    data_point.should == DataService::QuotaUsagePoint.new(499, 500)
+
+    data_point = DataService.quota_utilisation(cloud_account, 
Quota::RESOURCE_OVERALL)
+    data_point.should == DataService::QuotaUsagePoint.new(499, 500)
   end
 
   it "should calculate the average, max and min task submission times" do
-- 
1.6.6.1

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to