deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 6/8] Mock: Cleanup in how we create mock 'metrics'
Date Mon, 18 Feb 2013 13:41:16 GMT
From: Michal Fojtik <mfojtik@redhat.com>

Moved the code to the Metric model to keep MockDriver
clean.

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/deltacloud/models/metric.rb | 45 ++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/server/lib/deltacloud/models/metric.rb b/server/lib/deltacloud/models/metric.rb
index 7970565..1f7f422 100644
--- a/server/lib/deltacloud/models/metric.rb
+++ b/server/lib/deltacloud/models/metric.rb
@@ -18,6 +18,18 @@ class Metric < BaseModel
   attr_accessor :entity
   attr_accessor :properties
 
+  MOCK_METRICS_NAMES = [
+    'cpuUtilization',
+    'diskReadRequestCount',
+    'diskReadSector',
+    'diskWriteRequestCount',
+    'diskWriteSector',
+    'nicInputByte',
+    'nicInputPacket',
+    'nicOutputByte',
+    'nicOutputPacket'
+  ]
+
   def unknown?
     self.entity == :unknown
   end
@@ -51,6 +63,39 @@ class Metric < BaseModel
         :values => values
       }
     end
+
+    def generate_mock_values!
+      generator = lambda { |name, kind|
+        v = {
+          :min => (1+(rand(49))),
+          :max => (50+(rand(50)))
+        }
+        (name == 'cpuUtilization') ? v[kind].to_f/100 : v[kind]
+      }
+      @values = (0..5).map do |v_id|
+        {
+          :minimum => min = generator.call(@name, :min),
+          :maximum => max = generator.call(@name, :max),
+          :average => (min+max)/2,
+          :timestamp => (Time.now-v_id).to_i,
+          :unit => unit_for(@name)
+        }
+      end
+    end
+
+    private
+
+    def unit_for(name)
+      case name
+        when /Utilization/ then 'Percent'
+        when /Byte/ then 'Bytes'
+        when /Sector/ then 'Count'
+        when /Count/ then 'Count'
+        when /Packet/ then 'Count'
+        else 'None'
+      end
+    end
+
   end
 
 end
-- 
1.8.1.2


Mime
View raw message