#!/usr/bin/env python2

import os, subprocess

def listtofile(listing, file):
	fd = open(file, 'r')
	for i in listing:
		fd.write(i+"\n")
	fd.close()

def makeGraphs(graphs, graphtitle = "Title", xlabel = "Numbers calculated", ylabel = "Time needed for calculation"):
	gendir = "stats/generated/"
	subprocess.call(["mkdir", "-p", gendir])
	p = subprocess.Popen(['gnuplot'],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
	# now do the plots
	for graphfile in graphs:
		# set output 'filename'
		p.stdin.write("set terminal png font arial 8 #size 600,300\n")
		p.stdin.write("set grid\n")
		p.stdin.write("set datafile separator ';'\n")
		p.stdin.write("set title 'Timing of each "+graphtitle+"'\n")
		p.stdin.write("set xlabel '"+xlabel+"'\n")
		p.stdin.write("set ylabel '"+ylabel+"'\n")
		p.stdin.write("set output '"+gendir+graphfile+"'\n")
		# plot 'filename' using 1:2 title 'algorithm' with lines, 'filename2' using 1:2 title 'algo2' with lines
		plots = []
		for title, file, cols in graphs[graphfile]:
			plots.append("'"+gendir+file+"' using "+str(cols)+" title '"+str(title)+"' with lines")
		plotcommand = ", ".join(plots)
		#print gendir+graphfile
		p.stdin.write("plot "+plotcommand+"\n")
	p.communicate()[0]
	p.stdin.close()

statsdir = 'stats/raw/'
runs = os.listdir(statsdir)
#print runs

bigrunlist = []
for i in runs:
	runfiles = os.listdir(statsdir+i)
#	print runfiles
	for file in runfiles:
#		print "File: %s%s/%s" % (statsdir, i, file)
		splitfilename = file.split('_')
		dict = {}
		dict['no'] = int(splitfilename[0].lstrip('no'))
		dict['nnp'] = int(splitfilename[1].lstrip('nnp'))
		dict['n'] = int(splitfilename[2].lstrip('n'))
		fd = open(statsdir+i+'/'+file, "r")
		fd_text = fd.read()
		fd.close()
		fd_lines = fd_text.split("\n")
		for line in fd_lines:
			if line == "":
				continue
			elements = line.split(" ")
			if len(elements) < 2:
				continue
			if elements[1] != "timings:":
				continue
			for elem in elements:
				(key, value) = elem.split(":", 1)
				if key == 'prep':
					dict['prep'] = float(value)
				elif key == 'algo':
					dict['algo'] = float(value)
				elif key == 'postp':
					dict['postp'] = float(value)
				elif key == 'end':
					dict['end'] = float(value)
			bigrunlist.append(dict)
nos = {}
nnps = {}
ns = {}
foolist = {}
for i in bigrunlist:
	if not (i['no'], i['nnp'], i['n']) in foolist:
		foolist[(i['no'], i['nnp'], i['n'])] = [i['end']]
	else:
		foolist[(i['no'], i['nnp'], i['n'])].append(i['end'])
	if not i['no'] in nos:
		nos[i['no']] = {}
	if not i['nnp'] in nnps:
		nnps[i['nnp']] = {}
	if not i['n'] in ns:
		ns[i['n']] = {}
#print "NOs: %s" % nos
#print "NNPs: %s" % nnps
#print "Ns: %s" % ns
uniquelist = []
for key in foolist:
	minval = min(foolist[key])
	maxval = max(foolist[key])
	avgval = sum(foolist[key])/len(foolist[key])
	uniquelist.append((key[0], key[1], key[2], minval))
uniquelist.sort()
alg_dict = {}
sched_dict = {}
for i in uniquelist:
	if i[0] not in alg_dict:
		alg_dict[i[0]] = {}
	if i[1] not in alg_dict[i[0]]:
		alg_dict[i[0]][i[1]] = []
	alg_dict[i[0]][i[1]].append(str(i[2])+";"+str(i[3]))
	if i[1] not in sched_dict:
		sched_dict[i[1]] = {}
	if i[0] not in sched_dict[i[1]]:
		sched_dict[i[1]][i[0]] = []
	sched_dict[i[1]][i[0]].append(str(i[2])+";"+str(i[3]))
	#print "od["+i[0]+"]["+i[1]+"] = "+str(i[2])+":"+str(i[3])

graphs_alg = {}
graphs_sched = {}
gendir = "stats/generated/"
subprocess.call(["mkdir", "-p", gendir])
for alg in alg_dict:
	pngfile = 'no.'+str(alg)+'.png'
	graphs_alg[pngfile] = []
	for sched in alg_dict[alg]:
		datfile = 'data.no'+str(alg)+'.nnp'+str(sched)+'.dat'
		subprocess.call(["rm", "-f", gendir+datfile])
		fh = open(gendir+datfile, 'a+')
		graphs_alg[pngfile].append((sched, datfile, '1:2'))
		for dot in alg_dict[alg][sched]:
			fh.write(dot+"\n")
		fh.close()
for sched in sched_dict:
	pngfile = 'nnp.'+str(sched)+'.png'
	graphs_sched[pngfile] = []
	for alg in sched_dict[sched]:
		datfile = 'data.no'+str(alg)+'.nnp'+str(sched)+'.dat'
		graphs_sched[pngfile].append((alg, datfile, '1:2'))

#graphs['test1.png'] = [('linetitle1', 'prefix.hillis.dat', '1:2')]

makeGraphs(graphs_alg, "no")
makeGraphs(graphs_sched, "nnp")
