Bläddra i källkod

refactored fixed minor bugs

steve 8 år sedan
förälder
incheckning
ebca0eab60
2 ändrade filer med 38 tillägg och 12 borttagningar
  1. 26 8
      src/monitor.py
  2. 12 4
      test/TestServerMonitor.py

+ 26 - 8
src/monitor.py

@@ -50,12 +50,14 @@ class Env(Analysis):
 		r = [ self.evaluate(id) for id in self.values] ;		
 		N = len(r)
 		n = sum(r)
-		return n/N
+		value = n/N
+		missing = [self.values[i] for i in range(0,N) if r[i] == 0]
+		return {"value":value,"missing":missing}
 
 class Sandbox(Analysis):
 	def __init__(self,conf):
 		Analysis.__init__(self)
-		self.sandbox_path = conf['path']
+		self.sandbox_path = conf['sandbox']
 		self.requirements_path = conf['requirements']
 	def get_requirements (self):
 		f = open(self.requirements_path)
@@ -78,9 +80,11 @@ class Sandbox(Analysis):
 	def composite(self):
 		required_modules= self.get_requirements()
 		sandbox_modules	= self.get_sandbox_requirements()
-		N = len(requirements)
+		N = len(required_modules)
 		n = len(Set(required_modules) - Set(sandbox_modules))
-		return n/N
+		value = 1 - (n/N)
+		missing = list(Set(required_modules) - Set(sandbox_modules))
+		return {"value":value,"missing":missing}
 
 """
 	This class performs the analysis of a list of processes and determines
@@ -113,16 +117,30 @@ class DetailProcess(Analysis):
 	def evaluate(self,name) :
 		cmd	= "ps -eo pmem,pcpu,vsize,comm|grep :app$"
 		handler = subprocess.Popen(cmd.replace(":app",name),shell=True,stdout=subprocess.PIPE)
-		ostream = handler.communicate()[0].split(' ')
-		return [float(value) for value in ostream if value.strip() not in ['',name]] +[name]
+		ostream = handler.communicate()[0].split('\n')
+		ostream = [ row.split(' ') for row in ostream if row != '']
 		
+		r = []
+		for row in ostream :
+			
+			row =  [float(value) for value in row if value.strip() not in ['',name]] +[name]
+			r.append(row)
+		return r
+	def format(self,row):
+		return {"memory_usage":row[0],"cpu_usage":row[1],"memory_available":row[2]/1000,"label":row[3]}
 	def composite(self):
 		#value = self.evaluate(self.name)
 		#row= {"memory_usage":value[0],"cpu_usage":value[1]}
 		#return row
-		ma = [self.evaluate(name) for name in self.names]
+		#ma = [self.evaluate(name) for name in self.names]
+		ma = []
+		for name in self.names:
+			matrix = self.evaluate(name)
+			ma += [self.format(row) for row in matrix]
+			
+		#return [{"memory_usage":row[0],"cpu_usage":row[1],"memory_available":row[2]/1000,"label":row[3]} for row in ma]
 		
-		return [{"memory_usage":row[0],"cpu_usage":row[1],"memory_available":row[2]/1000,"label":row[3]} for row in ma]
+		return ma
 """
 	This class will require
 """

+ 12 - 4
test/TestServerMonitor.py

@@ -1,7 +1,7 @@
 from __future__ import division
 import unittest
-from monitor import Env, DetailProcess, ProcessCounter
-
+from monitor import Env, DetailProcess, ProcessCounter, Sandbox
+import os
 class TestMonitorServer(unittest.TestCase):
 	
 	def test_Environment(self):
@@ -9,11 +9,13 @@ class TestMonitorServer(unittest.TestCase):
 			This test case is designed to test the existance of a resource set as an environment variable. This applies to files, folders (not values)
 		"""
 		p = Env(['PATH','HOME','SHELL'])
-		value = p.composite()
+		r = p.composite()
+		value = r['value']
+		
 		self.assertTrue(value > 0 and value == 2/3)
 		self.assertTrue(p.evaluate('PATH') == 0)
 	def test_RunningProcess(self):
-		p = DetailProcess(['rabbitmq-server','python'])
+		p = DetailProcess(['rabbitmq-server','python','apache2'])
 		r = p.composite()
 		print r
 		self.assertTrue(r)
@@ -21,7 +23,13 @@ class TestMonitorServer(unittest.TestCase):
 	def test_ProcessCount(self):
 		p= ProcessCounter(['apache2','VBoxClient','rabbitmq-server','foo'])
 		r = p.composite()
+		
 		self.assertTrue( sum(r.values()) > 0 )
 		self.assertTrue( r['foo'] == 0)
+	def test_VirtualEnv(self):
+		requirements_path = os.sep.join([os.environ['PYTHONPATH'],"..","requirements.txt"])
+		sandbox_path	= os.sep.join([os.environ['PYTHONPATH'],"..",'sandbox'])
+		p	= Sandbox({"sandbox":sandbox_path,"requirements":requirements_path})
+		print p.composite()
 if __name__ == '__main__' :
 	unittest.main()