~mabac/launchpad-work-items-tracker/fix-none-whiteboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/python
#
# Create a blueprint tracking chart from a blueprint database.
#
# Copyright (C) 2010, 2011 Canonical Ltd.
# License: GPL-3

import optparse, datetime, sys
import report_tools

from pychart import *

def date_to_ordinal(s):
    '''Turn yyyy-mm-dd strings to ordinals'''
    return report_tools.date_to_python(s).toordinal()


def ordinal_to_date(ordinal):
    '''Turn an ordinal date into a string'''
    d = datetime.date.fromordinal(int(ordinal))
    return d.strftime('%Y-%m-%d')

def format_date(ordinal):
    d = datetime.date.fromordinal(int(ordinal))
    return '/a60{}' + d.strftime('%b %d, %y')

def do_chart(data, start_date, end_date, trend_start, title, filename, only_weekdays, inverted):
    #set up default values
    format = 'svg'
    height = 450
    width = 1000
    legend_x = 700
    legend_y = 200
    title_x = 300
    title_y = 350

    if inverted:
        legend_x=200

    # Tell pychart to use colors
    theme.use_color = True
    theme.default_font_size = 12
    theme.reinitialize()

    # turn into pychart data model and calculate maximum number of WIs
    max_items = 1 # start at 1 to avoid zero div
    lastactive = 0
    pcdata = []

    for date in xrange(date_to_ordinal(start_date), date_to_ordinal(end_date)+1):
        if (not only_weekdays or datetime.date.fromordinal(date).weekday() < 5):
            end_date = ordinal_to_date(date)
            i = data.get(ordinal_to_date(date), {})
            count = i.get('Completed', 0) + i.get('Planned', 0) + i.get('Blocked', 0) + i.get('In Progress', 0)
            if max_items < count:
                max_items = count
            pcdata.append((date, i.get('Planned', 0),0,
                           i.get('Blocked', 0),0,
                           i.get('In Progress', 0),0,
                           i.get('Completed',0),0, count))
            if count > 0:
                lastactive = len(pcdata) - 1

    # add some extra space to look nicer
    max_items = int(max_items * 1.05)

    x_interval = len(pcdata)/20
    if max_items > 500:
        y_interval = max_items/200*10
    elif max_items < 20:
        y_interval = 1
    else:
        y_interval = max_items/20

    # create the chart object
    chart_object.set_defaults(area.T, size=(width, height),
            y_range=(0, None), x_coord=category_coord.T(pcdata, 0))

    # tell the chart object it will use a bar chart, and will
    # use the data list for it's model
    chart_object.set_defaults(bar_plot.T, data=pcdata)

    # create the chart area
    # tell it to start at coords 0,0
    # tell it the labels, and the tics, etc..
    # HACK: to prevent 0 div
    if max_items == 0:
        max_items = 1
    ar = area.T(legend=legend.T(loc=(legend_x,legend_y)), loc=(0,0),
            x_axis=axis.X(label='Date', tic_interval=x_interval,format=format_date),
            y_axis=axis.Y(label='Blueprints', tic_interval=y_interval),
            y_range=(0, max_items))

    #initialize the blar_plot fill styles
    bar_plot.fill_styles.reset()

    # create each set of data to plot
    # note that index zero is the label col
    # for each column of data, tell it what to use for the legend and
    # what color to make the bar, no lines, and
    # what plot to stack on

    tlabel = ''

    if inverted:
        plot1 = bar_plot.T(label='Completed' + tlabel, hcol=7)
        plot1.fill_style = fill_style.Plain(bgcolor=color.seagreen)

        plot3 = bar_plot.T(label='In Progress' + tlabel, hcol=5, stack_on = plot1)
        plot3.fill_style = fill_style.Plain(bgcolor=color.gray65)

        plot5 = bar_plot.T(label='Blocked' + tlabel, hcol=3, stack_on = plot3)
        plot5.fill_style = fill_style.Plain(bgcolor=color.red1)

        plot7 = bar_plot.T(label='Planned' + tlabel, hcol=1, stack_on = plot5)
        plot7.fill_style = fill_style.Plain(bgcolor=color.darkorange1)
    else:
        plot1 = bar_plot.T(label='Planned' + tlabel, hcol=1)
        plot1.fill_style = fill_style.Plain(bgcolor=color.darkorange1)

        plot3 = bar_plot.T(label='Blocked' + tlabel, hcol=3, stack_on = plot1)
        plot3.fill_style = fill_style.Plain(bgcolor=color.red1)

        plot5 = bar_plot.T(label='In Progress' + tlabel, hcol=5, stack_on = plot3)
        plot5.fill_style = fill_style.Plain(bgcolor=color.gray65)

        plot7 = bar_plot.T(label='Completed' + tlabel, hcol=7, stack_on = plot5)
        plot7.fill_style = fill_style.Plain(bgcolor=color.seagreen)


    plot1.line_style = None
    plot3.line_style = None
    plot5.line_style = None
    plot7.line_style = None

    plot11 = bar_plot.T(label='total', hcol=9)
    plot11.fill_style = None
    plot11.line_style = line_style.gray30

    # create the canvas with the specified filename and file format
    can = canvas.init(filename,format)

    # add the data to the area and  draw it
    ar.add_plot(plot1, plot3, plot5, plot7)
    ar.draw()

    # title
    tb = text_box.T(loc=(title_x, title_y), text=title, line_style=None)
    tb.fill_style = None
    tb.draw()

#
# main
#

# argv parsing
optparser = optparse.OptionParser()
optparser.add_option('-d', '--database',
    help='Path to database', dest='database', metavar='PATH')
optparser.add_option('-t', '--team',
        help='Restrict report to a particular team', dest='team')
optparser.add_option('-m', '--milestone',
        help='Restrict report to a particular milestone', dest='milestone')
optparser.add_option('-o', '--output',
        help='Output file', dest='output')
optparser.add_option('--trend-start', type='int',
        help='Explicitly set start of trend line', dest='trendstart')
optparser.add_option('-u', '--user', 
        help='Run for this user', dest='user')
optparser.add_option('--only-weekdays', action='store_true',
        help='Skip Saturdays and Sundays in the resulting graph', dest='only_weekdays')
optparser.add_option('--inverted', action='store_true',
        help='Generate an inverted burndown chart', dest='inverted')
optparser.add_option('-s', '--start-date', 
        help='Explicitly set the start date of the burndown data', dest='start_date')
optparser.add_option('-e', '--end-date', 
        help='Explicitly set the end date of the burndown data', dest='end_date')
optparser.add_option('--no-foreign', action='store_true', default=False,
        help='Do not show foreign totals separate', dest='noforeign')
optparser.add_option('--group',
        help='Run for this group', dest='group')
optparser.add_option('--date',
        help='Run for this date', dest='date')

(opts, args) = optparser.parse_args()
if not opts.database:
    optparser.error('No database given')
if not opts.output:
    optparser.error('No output file given')

if opts.user and  opts.team:
    optparser.error('team and user options are mutually exclusive')
if opts.user and opts.group:
    optparser.error('user and group options are mutually exclusive')
if opts.team and opts.group:
    optparser.error('team and group options are mutually exclusive')
if opts.milestone and opts.date:
    optparser.error('milestone and date options are mutually exclusive')

# The typing allows polymorphic behavior
if opts.user:
    opts.team = report_tools.user_string(opts.user)
elif opts.team:
    opts.team = report_tools.team_string(opts.team)

store = report_tools.get_store(opts.database)

milestone_collection = None
if opts.milestone:
    milestone_collection = report_tools.get_milestone(store, opts.milestone)
elif opts.date:
    milestone_collection = report_tools.MilestoneGroup(
        report_tools.date_to_python(opts.date))


# get date -> state -> count mapping
data = report_tools.blueprints_over_time(store)

if len(data) == 0:
    print 'WARNING: no blueprints, not generating chart (team: %s, group: %s, due date: %s)' % (
        opts.team or 'all', opts.group or 'none', milestone_collection and milestone_collection.display_name or 'none')
    sys.exit(0)

# calculate start/end date if no dates are given
if opts.start_date is None:
    start_date = sorted(data.keys())[0]
else:
    start_date=opts.start_date

if opts.end_date is None:
    if milestone_collection is not None:
        end_date = milestone_collection.due_date_str
    else:
        end_date=report_tools.milestone_due_date(store)
else:
    end_date=opts.end_date

if not start_date or not end_date or date_to_ordinal(start_date) > date_to_ordinal(end_date):
    print 'WARNING: empty date range, not generating chart (team: %s, group: %s, due date: %s)' % (
        opts.team or 'all', opts.group or 'none', milestone_collection and milestone_collection.display_name or 'none')
    sys.exit(0)

# title
title = '/20all quarters'

if milestone_collection is not None:
    title += ' (%s)' % milestone_collection.name

do_chart(data, start_date, end_date, opts.trendstart, title, opts.output, opts.only_weekdays, opts.inverted)