~mabac/launchpad-work-items-tracker/center-progress-bars

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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/python
#
# Create a burndown chart from a work item 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,noforeign):
    #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('done', 0) + i.get('todo', 0) + i.get('blocked', 0) + i.get('inprogress', 0) + i.get('postponed', 0)
            if max_items < count:
                max_items = count
            if noforeign:
                # This total is always higher
                pcdata.append((date, i.get('todo', 0),0,
                            i.get('blocked', 0),0,
                            i.get('inprogress', 0),0,
                            i.get('done',0),0,
                            i.get('postponed',0),0, count))
            else:
                pcdata.append((date, i.get('todo_teamonly', 0), i.get('todo', 0),
                    i.get('blocked_teamonly', 0), i.get('blocked', 0),
                    i.get('inprogress_teamonly', 0), i.get('inprogress', 0),
                    i.get('done_teamonly', 0), i.get('done', 0),
                    i.get('postponed_teamonly', 0), i.get('postponed', 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='Work Items', 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

    # Suppressed when running w/o foreign numbers
    if noforeign:
        tlabel = ''
    else:
        tlabel = ' (team)'

    if inverted:
        plot1 = bar_plot.T(label='done' + tlabel, hcol=7)
        plot1.fill_style = fill_style.Plain(bgcolor=color.seagreen)
        plot2 = bar_plot.T(label='done (foreign)', hcol=8)
        plot2.fill_style = fill_style.Plain(bgcolor=color.limegreen)

        plot3 = bar_plot.T(label='inprogress' + tlabel, hcol=5, stack_on = plot2)
        plot3.fill_style = fill_style.Plain(bgcolor=color.gray65)
        plot4 = bar_plot.T(label='inprogress (foreign)', hcol=6, stack_on = plot2)
        plot4.fill_style = fill_style.Plain(bgcolor=color.gray74)

        plot5 = bar_plot.T(label='blocked' + tlabel, hcol=3, stack_on = plot4)
        plot5.fill_style = fill_style.Plain(bgcolor=color.red1)
        plot6 = bar_plot.T(label='blocked (foreign)', hcol=4, stack_on = plot4)
        plot6.fill_style = fill_style.Plain(bgcolor=color.red3)

        plot7 = bar_plot.T(label='todo' + tlabel, hcol=1, stack_on = plot6)
        plot7.fill_style = fill_style.Plain(bgcolor=color.darkorange1)
        plot8 = bar_plot.T(label='todo (foreign)', hcol=2, stack_on = plot6)
        plot8.fill_style = fill_style.Plain(bgcolor=color.orangered1)
    else:
        plot1 = bar_plot.T(label='todo' + tlabel, hcol=1)
        plot1.fill_style = fill_style.Plain(bgcolor=color.darkorange1)
        plot2 = bar_plot.T(label='todo (foreign)', hcol=2)
        plot2.fill_style = fill_style.Plain(bgcolor=color.orangered1)

        plot3 = bar_plot.T(label='blocked' + tlabel, hcol=3, stack_on = plot2)
        plot3.fill_style = fill_style.Plain(bgcolor=color.red1)
        plot4 = bar_plot.T(label='blocked (foreign)', hcol=4, stack_on = plot2)
        plot4.fill_style = fill_style.Plain(bgcolor=color.red3)

        plot5 = bar_plot.T(label='inprogress' + tlabel, hcol=5, stack_on = plot4)
        plot5.fill_style = fill_style.Plain(bgcolor=color.gray65)
        plot6 = bar_plot.T(label='inprogress (foreign)', hcol=6, stack_on = plot4)
        plot6.fill_style = fill_style.Plain(bgcolor=color.gray74)

        plot7 = bar_plot.T(label='done' + tlabel, hcol=7, stack_on = plot6)
        plot7.fill_style = fill_style.Plain(bgcolor=color.seagreen)
        plot8 = bar_plot.T(label='done (foreign)', hcol=8, stack_on = plot6)
        plot8.fill_style = fill_style.Plain(bgcolor=color.limegreen)


    plot1.line_style = None
    plot2.line_style = None
    plot3.line_style = None
    plot4.line_style = None
    plot5.line_style = None
    plot6.line_style = None
    plot7.line_style = None
    plot8.line_style = None


    plot9 = bar_plot.T(label='postponed' + tlabel, hcol=9, stack_on = plot8)
    plot9.fill_style = fill_style.Plain(bgcolor=color.purple)
    plot9.line_style = None
    plot10 = bar_plot.T(label="postponed (foreign)  ", hcol=10, stack_on = plot8)
    plot10.fill_style = fill_style.Plain(bgcolor=color.mediumorchid)
    plot10.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
    if noforeign:
        plot3.stack_on = plot1
        plot5.stack_on = plot3
        plot7.stack_on = plot5
        plot9.stack_on = plot7
        ar.add_plot(plot1, plot3, plot5, plot7, plot9)
    else:
        ar.add_plot(plot2, plot1, plot4, plot3, plot6, plot5, plot8, plot7, plot10, plot9)

    ar.draw()

    # create and arrow for the trend line
    trend_line = arrow.T(head_style=1)
    # remove heads so arrow becomes a line
    trend_line.head_len = 0
    trend_line.thickness = 0
    trend_line.line_style.width = 3

    # set up the trend line position and draw it
    if inverted:
        if lastactive > 1:
            trend_line.draw([(0, 0), (ar.x_pos(pcdata[lastactive-1][0]),ar.y_pos(pcdata[lastactive-1][8]))])
            trend_line.line_style = line_style.black_dash1
        trend_line.line_style.width = 3
        trend_line.draw([(ar.x_pos(pcdata[lastactive-1][0]),ar.y_pos(pcdata[lastactive-1][8])), (ar.x_pos(date_to_ordinal(end_date)),ar.y_pos(pcdata[lastactive][2]+pcdata[lastactive][4]+pcdata[lastactive][6]+pcdata[lastactive][8]))])
    else:
        if not trend_start:
            if noforeign:
                trend_start = pcdata[0][1] + pcdata[0][3] + pcdata[0][5]
            else:
                trend_start = pcdata[0][2] + pcdata[0][4] + pcdata[0][6]
        trend_line.draw([(0, ar.y_pos(trend_start)), (ar.x_pos(date_to_ordinal(end_date)), 0)])

    # 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.workitems_over_time(store, team=opts.team, group=opts.group, milestone_collection=milestone_collection)

if len(data) == 0:
    print 'WARNING: no work items, 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
if opts.team:
    title = '/20' + opts.team
    noforeign = opts.noforeign
elif opts.group:
    title = "/20" + opts.group
    noforeign = True
else:
    title = '/20all teams'
    # It never makese sense in all teams mode
    noforeign = True

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, noforeign)