#!/usr/bin/python
#
# Script for determining distribution/statistics of returned WUs for
# one project.
#
from os.path import abspath
from os import popen
from glob import glob
fahtoolspath=abspath("../modules/fah_adaptive_sampling/FAHTools/")
server2dir=abspath(".")
import sys
from optparse import OptionParser
from ProjectConfigurationFile import *
from FAHServer import *

hours=3600
days=hours*24

parser=OptionParser()
parser.add_option( "-p", "--project", dest="project" )
parser.add_option( "-r", "--run", default=-1, dest="run" )
parser.add_option( "-c", "--clone", default=-1, dest="clone" )
parser.add_option( "-g", "--gen", default=-1, dest="gen" )
parser.add_option( "--server2dir", default=server2dir, dest="server2dir" )
parser.add_option( "--ppd", default=0, dest="ppd" )
parser.add_option( "--deadline", default=0, dest="deadline" )
parser.add_option( "--timeout", default=0, dest="timeout" )
parser.add_option( "-v", "--verbose", default=False, action="store_true", dest="verbose" )
(flags,args)=parser.parse_args()
project=flags.project
run=int(flags.run)
clone=int(flags.clone)
gen=int(flags.gen)
server2dir=flags.server2dir
ppd=float(flags.ppd)
deadline=float(flags.deadline)
timeout=float(flags.timeout)
verbose=flags.verbose

# have to have a project. 
if not project :
	print "Usage: ./benchmark.py -p <project number>"
	print "You can specify a particular run, clone, or gen"
	print "as well (default is do all)."
	sys.exit()

server=FAHServer( server2dir )
availableProjects = server.projects

try: 
	conf=ProjectConfigurationFile( server.projects[ project ] )
except KeyError:
	print "Project number %s not found in '%s/project.conf'" % ( project, server2dir )
	sys.exit() 

if conf.nruns < run :
	print "Only found %d runs, doing all" % conf.nruns
	run = -1
if conf.nclones < clone :
	print "Only found %d clones, doing all" % conf.nclones
	clone = -1

# build run, clone lists
if run==-1 : runlist=range( conf.nruns )
else: runlist=[run]
if clone==-1 : clonelist=range( conf.nclones )
else: clonelist=[clone]

# build the wu list
wulist = []
if verbose :
	print conf.datapath
	print "runs:" , runlist
	print "clones:", clonelist
	print "gen:" , gen

for run in runlist :
	for clone in clonelist :
		rcdir = "%s/RUN%d/CLONE%d/" % ( conf.datapath, run, clone )
		if gen == -1 :
			wulist.extend( glob( "%s/frame*log" % rcdir ) )
		else :
			newgen=glob( "%s/frame%d.log" % ( rcdir, gen ) )
			if len( newgen ) > 0:
				wulist.extend( newgen )	

nwu=len( wulist )
print "Found %d work units meeting criteria" % nwu
if nwu == 0: sys.exit()
print "parsing for time ..."
nodesec=0.0
realsec=0.0
for wu in wulist :
	# the argument to grep will result in ignoring weird log files
	GREP = popen( "grep Time: %s --binary-files=without-match" % wu )
	timedata=GREP.readlines()
	GREP.close()

	# some .log files come in parts if people stopped the WU partway through
	for record in timedata :
		( n, r )=record.split()[1:3]
		try:
			nodesec += float( n )
			realsec += float( r )
		except ValueError:
			print record
	
sec = nodesec/nwu
secr = realsec/nwu
hrs = sec/hours
hrsr = secr/hours
dys = sec/days
dysr = secr/days

print "Total time =\t%10.6f seconds\t%10.6f seconds real" % ( nodesec, realsec ) 
print "Timing =\t%10.6f seconds/WU\t%10.6f seconds/WU real" % ( sec, secr )
print "\t\t%10.6f hours/WU\t%10.6f hours/WU real" % ( hrs, hrsr )
print "\t\t%10.6f days/WU\t%10.6f days/WU real" % ( dys, dysr )

credit = ppd*dys
deadline = deadline*dys
timeout = timeout*dys

s = ""
if credit > 0 : s += "POINTS = %6.1f\t\t" % credit 
if deadline > 0 : s += "DEADLINE = %6.1f\t" % deadline
if timeout > 0 : s += "TIMEOUT = %6.1f" % timeout 

print s
