From 9154b3cc571c2461010573f84b91fcf84830cf5a Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@fe.uni-lj.si>
Date: Wed, 16 Mar 2016 08:18:50 +0000
Subject: [PATCH] Improved reporting. Added per process ID possibilities

---
 python/trisurf/trisurf.py |   60 ++++++++++++++++++++++++++----
 python/tsmgr              |   53 ++++++++++++++++++++------
 2 files changed, 93 insertions(+), 20 deletions(-)

diff --git a/python/trisurf/trisurf.py b/python/trisurf/trisurf.py
index 2d67a74..30c06bd 100644
--- a/python/trisurf/trisurf.py
+++ b/python/trisurf/trisurf.py
@@ -24,7 +24,29 @@
 
 '''
 
+class FileContent:
+	def __init__(self,filename):
+		self.filename=filename
+		self.data=""
+		self.readfile()
 
+	def readfile(self):
+		try:
+			with open (self.filename, "r") as myfile:
+				self.data=myfile.read().replace('\n', '')
+		except:
+			pass
+
+
+	def writefile(self, data, mode='w'):
+		with open (self.filename, mode) as myfile:
+			myfile.write(data)
+
+	def getText(self):
+		return self.data
+
+	def __str__(self):
+		return self.getText()
 
 class Tape:
 	'''Has all the info on the tape'''
@@ -187,18 +209,26 @@
 		self.tape=Tape()
 		self.tape.setTape(tapetxt.text)
 
-	def getStatus(self):
+	def getPID(self):
 		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
 		self.Dir.makeifnotexist()
-#		self.Dir.goto()
 		try:
 			fp = open(os.path.join(self.Dir.fullpath(),'.lock'))
 		except IOError as e:
 			return 0 #file probably does not exist. e==2??
-		pid=fp.readline();
-		fp.close();
+		pid=fp.readline()
+		fp.close()
+		return pid
+
+	def getStatus(self):
+		pid=self.getPID()
+		if(pid==0):
+			return 0
 		if(psutil.pid_exists(int(pid))):
-			return 1
+			if psutil.Process(int(pid)).name=="trisurf":
+				return 1
+			else:
+				return 0
 		else:
 			return 0
 
@@ -233,12 +263,26 @@
 	def getStatistics(self, statfile="statistics.csv"):
 		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
 		self.statistics=Statistics(self.Dir.fullpath(), statfile)
-		if(self.statistics.fileOK):
-			report=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate)))+"\t"+str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000)+" ETA\t"+"STATUS"
+		self.Comment=FileContent(os.path.join(self.Dir.fullpath(),".comment"))
+		pid=self.getPID();
+		if(self.getStatus()):
+			statustxt="Running"
 		else:
-			report="N/A\tN/A\t"+"STATUS"
+			statustxt="Stopped"
+			pid=""
+
+		if(self.statistics.fileOK):
+#			report=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate)))+"\t"+str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000)+" ETA\t"+"STATUS"
+			report=[time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate))),str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000), statustxt, pid, str(self.Dir.fullpath()), self.Comment.getText()]
+		else:
+			report=["N/A","N/A\t",statustxt, pid, str(self.Dir.fullpath()), self.Comment.getText()]
 		return report
 
+	def writeComment(self, data):
+		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
+		self.Comment=FileContent(os.path.join(self.Dir.fullpath(),".comment"))
+		self.Comment.writefile(data,mode='w')
+
 	def __str__(self):
 		if(self.getStatus()==0):
 			str=" not running."
diff --git a/python/tsmgr b/python/tsmgr
index 52e3fdc..4b2b13f 100755
--- a/python/tsmgr
+++ b/python/tsmgr
@@ -1,6 +1,7 @@
 #!/usr/bin/python3
 import sys, getopt
 from trisurf import trisurf
+import tabulate
 
 # -- configuration of the multiple/single run --
 
@@ -14,30 +15,58 @@
 
 
 #obligatory: combine all runs
-Runs=[run1,run2];
+Runs=[run1,run2]
+
+
+
+#------------------ NO NEED TO TOUCH THE CODE BELOW ------------------------------
 
 # -- reading command line switches and acting accordingly --
-argv=sys.argv[1:];
+argv=sys.argv[1:]
+processno=0
 try:
-	opts, args = getopt.getopt(argv,"hrs")
+	opts, args = getopt.getopt(argv,"n:hrsc:")
 except getopt.GetoptError:
-	print('tsmgr [-h] [-r] [-s]')
+	print('tsmgr [-n process number] [-h] [-r] [-s] [-c comment text]')
 	sys.exit(2)
 for opt, arg in opts:
 	if opt == '-h':
-		print ('tsmgr [-h] [-r] [-s]')
+		print ('tsmgr [-n process number] [-h] [-r] [-s] [-c comment text]')
 		sys.exit()
 	elif opt == '-r':
-		for run in Runs:
+		if processno:
+			localRuns=[Runs[processno-1]]
+		else:
+			localRuns=Runs
+		for run in localRuns:
 			run.start()
 	elif opt == '-s':
-		for run in Runs:
-			reportstr=run.getStatistics()
-			print(reportstr)
-			#print(run)
-			#print(run.statistics)
+		report=[]
+		i=1
+		if processno:
+			localRuns=[Runs[processno-1]]
+		else:
+			localRuns=Runs
+		for run in localRuns:
+			line=run.getStatistics()
+			line.insert(0,i)
+			report.append(line)
+			i=i+1
+			#print(reportstr)
+		print ("\n\nTrisurf running processes report\n")
+		print (tabulate.tabulate(report,headers=["Run no.", "Run start time", "ETA", "Status", "PID", "Path", "Comment"], tablefmt='fancy_grid'))
+	elif opt == '-n':
+		processno=int(arg)
+		if processno<1 or processno>len(Runs) :
+			processno=0
+	elif opt == '-c':
+		comment = arg
+		if processno:
+			Runs[processno-1].writeComment(arg)
+
+		
 	else:
-		print('tsmgr [-h] [-r] [-s]')
+		print('tsmgr [-n process number] [-h] [-r] [-s] [-c comment text]')
 		sys.exit(2)
 
 

--
Gitblit v1.9.3