#!/usr/bin/python
# 
"""plot a graph for bugs"""
import string, sys, os
#from gagz.bugz import *
from gagz.utils import *
from player import *
from konst import *

def plotBugsGraph(player = None):
    if player:
        u = player
    else:
        u = Player()
    f = open(u.plotfile, 'w')
    f.write("""set xlabel "days back from today"
set ylabel "bugs"
set xrange [0:7] reverse
set timestamp "%m/%d/%y %H:%M" # bottom norotate 0,0 "Helvetica"
set border 3
set key left box
set xtics nomirror
set ytics nomirror
set terminal jpeg large size 480,360 x999999 x111111 xff0000\
    x0000aa xbb0000 x007700
""")

    f.write('set title "bugs history for\\n%s"\n' % u.name)
    # gotta make sure glidepath is last...
    plotlines = ['untargeted', 'mozilla0.9.9', 'glidepath']
    f.write('plot ')
    leng = len(plotlines)
    for i in range(leng):
        f.write('"-" t "%s" with lines' % plotlines[i])
        f.write(IfElse(leng-1==i,'\n',','))
    for i in range(leng):
        try:
            if i == leng-1:
                gp = u.glidepath()
                # just two points for a straight line...
                for x in [0,1]: f.write('%d %s\n' % (7*x , gp[x]))
            else:
                h = string.split('%s' % u.history[plotlines[i]])
                for x in range(len(h)):
                    f.write('%f %s\n' % ((float(x)/konst.kUpdateFreq), h[x]))
        except KeyError: pass
    f.write('e\n')
    f.close()
    os.system('gnuplot %s > %s' % (u.plotfile, u.chartfile))

if __name__ == '__main__':
    plotBugsGraph(sys.argv[1])
