#!/usr/bin/env python
# Copyright 2007-2008  Canonical, Ltd
# Author: Kees Cook <kees@ubuntu.com>
# Author: Brian Murray <brian@ubuntu.com>
# License: GPLv2
import sys

sourcepackage = sys.argv[1]
sourcepackagedata = sys.argv[1] + '/' + sys.argv[1] + '.data' 

current = open('%s' % sourcepackagedata ).read().splitlines().pop().split(",")[0]

# this will just exit without passing anything to gnuplot if the latest stat check resulted in a launchpad connection timeout
if current.startswith('#'):
    sys.exit(1)
else:
    now = int(current)

print '''
set xdata time
set timefmt "%s"
set xtics rotate by 90
set xlabel "Date/Time" 0,-1
set ylabel "Number of Bugs"
set autoscale
set yrange [0:*]
set offsets 0, 0, 1, 1

set term svg
set grid
set key horizontal outside
'''
# set key horizontal - my favorite

# colours can be found by using gnuplot's test image for the correct term
# for svgs -
# 1 is red, 3, is blue, 5 is green, 11 is yellowish
statinfo = {
#    'total': { 'title':'Total', 'column':2 },
#    'new': { 'title':'New', 'column':3, 'colour':1 },
#    'incomplete': { 'title':'Incomplete', 'column':4, 'colour':9 },
#    'confirmed': { 'title':'Confirmed', 'column':5, 'colour':3 },
    'triaged': { 'title':'Triaged', 'column':6, 'colour':1 },
    'inprogress': { 'title':'In Progress', 'column':7, 'colour':11 },
    'fixcommitted': { 'title':'Fix Committed', 'column':8, 'colour':3 },
#    'fixreleased': { 'title':'Fix Released', 'column':9 },
#    'invalid': { 'title':'Invalid', 'column':10 },
#    'wontfix': { 'title':'''Won't Fix''', 'column':11 },
#    'undecided': { 'title':'Undecided', 'column':12 },
#    'wishlist': { 'title':'Wishlist', 'column':13 },
#    'low': { 'title':'Low', 'column':14 },
#    'medium': { 'title':'Medium', 'column':15 },
#    'high': { 'title':'High', 'column':16 },
#    'critical': { 'title':'Critical', 'column':17 },
    }

# using a list so the statinfo dictionary will be accessed in this order
# useful for having the legend/key sorted
statuses = [ 'triaged', 'inprogress', 'fixcommitted' ]

days = {
    'fullyear': { 'step': 360, 'span': 3600*24*365, 'format': '%b %Y' },
    'halfyear': { 'step': 180, 'span': 3600*24*180, 'format': '%b %Y' },
    '3month': { 'step': 90, 'span': 3600*24*90, 'format': '%d %b' },
    'month': { 'step': 30, 'span': 3600*24*30, 'format': '%d %b' },
    'week': { 'step': 6, 'span': 3600*24*7, 'format': '%d %b' },
    '1day': { 'step': 1, 'span': 3600*24, 'format': '%m/%d %H:%M' },
    }

for day in days.keys():
    dfile  = sourcepackagedata
    xmin = now - days[day]['span']
    dstep  = days[day]['step']

    print '''set format x "%s"''' % (days[day]['format'])
    print '''set output "%s/plots/%s-%s-fixing.svg"''' \
            % (sourcepackage, sourcepackage, day)
    print '''plot ["%d":]''' % (xmin),
    plots = []
    # for every stat plot them together in one graph
    for status in statuses:
#    for stat in statinfo.keys():
        title  = statinfo[status]['title']
        column = statinfo[status]['column']
        colour = statinfo[status]['colour']
        plots += ['''"%s" using 1:%d every %d title "%s" with linespoints pointtype 7 pointsize 0.6 linecolor %d, "< tail -n 1 %s" using 1:%d notitle with points pointtype 6''' \
              % (dfile, column, dstep, title, colour, dfile, column)]
    print ",".join(plots)
    print ''
