~ubuntu-branches/ubuntu/oneiric/bugzilla/oneiric

« back to all changes in this revision

Viewing changes to summarize_time.cgi

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Bossek
  • Date: 2008-06-27 22:34:34 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080627223434-0ib57vstn43bb4a3
Tags: 3.0.4.1-1
* Update of French, Russian and German translations. (closes: #488251)
* Added Bulgarian and Belarusian translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -wT
2
 
# -*- Mode: perl; indent-tabs-mode: nil -*-
3
 
#
4
 
# The contents of this file are subject to the Mozilla Public
5
 
# License Version 1.1 (the "License"); you may not use this file
6
 
# except in compliance with the License. You may obtain a copy of
7
 
# the License at http://www.mozilla.org/MPL/
8
 
#
9
 
# Software distributed under the License is distributed on an "AS
10
 
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
 
# implied. See the License for the specific language governing
12
 
# rights and limitations under the License.
13
 
#
14
 
# The Original Code is the Bugzilla Bug Tracking System.
15
 
#
16
 
# Contributor(s): Christian Reis <kiko@async.com.br>
17
 
#                 Shane H. W. Travis <travis@sedsystems.ca>
18
 
#
19
 
use strict;
20
 
 
21
 
use lib qw(.);
22
 
 
23
 
use Date::Parse;         # strptime
24
 
use Date::Format;        # strftime
25
 
 
26
 
use Bugzilla;
27
 
use Bugzilla::Constants; # LOGIN_*
28
 
use Bugzilla::Bug;       # EmitDependList
29
 
use Bugzilla::Util;      # trim
30
 
use Bugzilla::Error;
31
 
use Bugzilla::User;      # Bugzilla->user->in_group
32
 
 
33
 
my $template = Bugzilla->template;
34
 
my $vars = {};
35
 
 
36
 
#
37
 
# Date handling
38
 
#
39
 
 
40
 
sub date_adjust_down {
41
 
   
42
 
    my ($year, $month, $day) = @_;
43
 
 
44
 
    if ($day == 0) {
45
 
        $month -= 1;
46
 
        $day = 31;
47
 
        # Proper day adjustment is done later.
48
 
 
49
 
        if ($month == 0) {
50
 
            $year -= 1;
51
 
            $month = 12;
52
 
        }
53
 
    }
54
 
 
55
 
    if (($month == 2) && ($day > 28)) {
56
 
        if ($year % 4 == 0 && $year % 100 != 0) {
57
 
            $day = 29;
58
 
        } else {
59
 
            $day = 28;
60
 
        }
61
 
    }
62
 
 
63
 
    if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
64
 
        ($day == 31) ) 
65
 
    {
66
 
        $day = 30;
67
 
    }
68
 
    return ($year, $month, $day);
69
 
}
70
 
 
71
 
sub date_adjust_up {
72
 
    my ($year, $month, $day) = @_;
73
 
 
74
 
    if ($day > 31) {
75
 
        $month += 1;
76
 
        $day    = 1;
77
 
 
78
 
        if ($month == 13) {
79
 
            $month = 1;
80
 
            $year += 1;
81
 
        }
82
 
    }
83
 
 
84
 
    if ($month == 2 && $day > 28) {
85
 
        if ($year % 4 != 0 || $year % 100 == 0 || $day > 29) {
86
 
            $month = 3;
87
 
            $day = 1;
88
 
        }
89
 
    }
90
 
 
91
 
    if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
92
 
        ($day == 31) )
93
 
    {
94
 
        $month += 1; 
95
 
        $day    = 1;
96
 
    }
97
 
 
98
 
    return ($year, $month, $day);
99
 
}
100
 
 
101
 
sub check_dates {
102
 
    my ($start_date, $end_date) = @_;
103
 
    if ($start_date) {
104
 
        if (!str2time($start_date)) {
105
 
            ThrowUserError("illegal_date", {'date' => $start_date});
106
 
        }
107
 
        # This code may strike you as funny. It's actually a workaround
108
 
        # for an "issue" in str2time. If you enter the date 2004-06-31,
109
 
        # even though it's a bogus date (there *are* only 30 days in
110
 
        # June), it will parse and return 2004-07-01. To make this
111
 
        # less painful to the end-user, I do the "normalization" here,
112
 
        # but it might be "surprising" and warrant a warning in the end.
113
 
        $start_date = time2str("%Y-%m-%d", str2time($start_date));
114
 
    } 
115
 
    if ($end_date) {
116
 
        if (!str2time($end_date)) {
117
 
            ThrowUserError("illegal_date", {'date' => $end_date});
118
 
        }
119
 
        # see related comment above.
120
 
        $end_date = time2str("%Y-%m-%d", str2time($end_date));
121
 
    }
122
 
    return ($start_date, $end_date);
123
 
}
124
 
 
125
 
sub split_by_month {
126
 
    # Takes start and end dates and splits them into a list of
127
 
    # monthly-spaced 2-lists of dates.
128
 
    my ($start_date, $end_date) = @_;
129
 
 
130
 
    # We assume at this point that the dates are provided and sane
131
 
    my (undef, undef, undef, $sd, $sm, $sy, undef) = strptime($start_date);
132
 
    my (undef, undef, undef, $ed, $em, $ey, undef) = strptime($end_date);
133
 
 
134
 
    # Find out how many months fit between the two dates so we know
135
 
    # how many times we loop.
136
 
    my $yd = $ey - $sy;
137
 
    my $md = 12 * $yd + $em - $sm;
138
 
    # If the end day is smaller than the start day, last interval is not a whole month.
139
 
    if ($sd > $ed) {
140
 
        $md -= 1;
141
 
    }
142
 
 
143
 
    my (@months, $sub_start, $sub_end);
144
 
    # This +1 and +1900 are a result of strptime's bizarre semantics
145
 
    my $year = $sy + 1900;
146
 
    my $month = $sm + 1;
147
 
 
148
 
    # Keep the original date, when the date will be changed in the adjust_date.
149
 
    my $sd_tmp = $sd;
150
 
    my $month_tmp = $month;
151
 
    my $year_tmp = $year;
152
 
 
153
 
    # This section handles only the whole months.
154
 
    for (my $i=0; $i < $md; $i++) {
155
 
        # Start of interval is adjusted up: 31.2. -> 1.3.
156
 
        ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_up($year, $month, $sd);
157
 
        $sub_start = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp); 
158
 
        $month += 1;
159
 
        if ($month == 13) {
160
 
            $month = 1;
161
 
            $year += 1;
162
 
        }
163
 
        # End of interval is adjusted down: 31.2 -> 28.2.
164
 
        ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_down($year, $month, $sd - 1);
165
 
        $sub_end = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp);
166
 
        push @months, [$sub_start, $sub_end];
167
 
    }
168
 
    
169
 
    # This section handles the last (unfinished) month. 
170
 
    $sub_end = sprintf("%04d-%02d-%02d", $ey + 1900, $em + 1, $ed);
171
 
    ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_up($year, $month, $sd);
172
 
    $sub_start = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp);
173
 
    push @months, [$sub_start, $sub_end];
174
 
 
175
 
    return @months;
176
 
}
177
 
 
178
 
sub include_tt_details {
179
 
    my ($res, $bugids, $start_date, $end_date) = @_;
180
 
 
181
 
 
182
 
    my $dbh = Bugzilla->dbh;
183
 
    my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
184
 
    my $buglist = join ", ", @{$bugids};
185
 
 
186
 
    my $q = qq{SELECT bugs.bug_id, profiles.login_name, bugs.deadline,
187
 
                      bugs.estimated_time, bugs.remaining_time
188
 
               FROM   longdescs
189
 
               INNER JOIN bugs
190
 
                  ON longdescs.bug_id = bugs.bug_id
191
 
               INNER JOIN profiles
192
 
                  ON longdescs.who = profiles.userid
193
 
               WHERE  longdescs.bug_id in ($buglist) $date_bits};
194
 
 
195
 
    my %res = %{$res};
196
 
    my $sth = $dbh->prepare($q);
197
 
    $sth->execute(@{$date_values});
198
 
    while (my $row = $sth->fetch) {
199
 
        $res{$row->[0]}{"deadline"} = $row->[2];
200
 
        $res{$row->[0]}{"estimated_time"} = $row->[3];
201
 
        $res{$row->[0]}{"remaining_time"} = $row->[4];
202
 
    }
203
 
    return \%res;
204
 
}
205
 
 
206
 
sub sqlize_dates {
207
 
    my ($start_date, $end_date) = @_;
208
 
    my $date_bits = "";
209
 
    my @date_values;
210
 
    if ($start_date) {
211
 
        # we've checked, trick_taint is fine
212
 
        trick_taint($start_date);
213
 
        $date_bits = " AND longdescs.bug_when > ?";
214
 
        push @date_values, $start_date;
215
 
    } 
216
 
    if ($end_date) {
217
 
        # we need to add one day to end_date to catch stuff done today
218
 
        # do not forget to adjust date if it was the last day of month
219
 
        my (undef, undef, undef, $ed, $em, $ey, undef) = strptime($end_date);
220
 
        ($ey, $em, $ed) = date_adjust_up($ey+1900, $em+1, $ed+1);
221
 
        $end_date = sprintf("%04d-%02d-%02d", $ey, $em, $ed);
222
 
 
223
 
        $date_bits .= " AND longdescs.bug_when < ?"; 
224
 
        push @date_values, $end_date;
225
 
    }
226
 
    return ($date_bits, \@date_values);
227
 
}
228
 
 
229
 
#
230
 
# Dependencies
231
 
#
232
 
 
233
 
sub get_blocker_ids_unique {
234
 
    my $bug_id = shift;
235
 
    my @ret = ($bug_id);
236
 
    get_blocker_ids_deep($bug_id, \@ret);
237
 
    my %unique;
238
 
    foreach my $blocker (@ret) {
239
 
        $unique{$blocker} = $blocker
240
 
    }
241
 
    return keys %unique;
242
 
}
243
 
 
244
 
sub get_blocker_ids_deep {
245
 
    my ($bug_id, $ret) = @_;
246
 
    my $deps = Bugzilla::Bug::EmitDependList("blocked", "dependson", $bug_id);
247
 
    push @{$ret}, @$deps;
248
 
    foreach $bug_id (@$deps) {
249
 
        get_blocker_ids_deep($bug_id, $ret);
250
 
    }
251
 
}
252
 
 
253
 
#
254
 
# Queries and data structure assembly
255
 
#
256
 
 
257
 
sub query_work_by_buglist {
258
 
    my ($bugids, $start_date, $end_date) = @_;
259
 
    my $dbh = Bugzilla->dbh;
260
 
 
261
 
    my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
262
 
 
263
 
    # $bugids is guaranteed to be non-empty because at least one bug is
264
 
    # always provided to this page.
265
 
    my $buglist = join ", ", @{$bugids};
266
 
 
267
 
    # Returns the total time worked on each bug *per developer*, with
268
 
    # bug descriptions and developer address
269
 
    my $q = qq{SELECT sum(longdescs.work_time) as total_time,
270
 
                      profiles.login_name, 
271
 
                      longdescs.bug_id,
272
 
                      bugs.short_desc,
273
 
                      bugs.bug_status
274
 
               FROM   longdescs
275
 
               INNER JOIN profiles
276
 
                   ON longdescs.who = profiles.userid
277
 
               INNER JOIN bugs
278
 
                   ON bugs.bug_id = longdescs.bug_id
279
 
               WHERE  longdescs.bug_id IN ($buglist)
280
 
                      $date_bits } .
281
 
            $dbh->sql_group_by('longdescs.bug_id, profiles.login_name',
282
 
                'bugs.short_desc, bugs.bug_status, longdescs.bug_when') . qq{
283
 
               ORDER BY longdescs.bug_when};
284
 
    my $sth = $dbh->prepare($q);
285
 
    $sth->execute(@{$date_values});
286
 
    return $sth;
287
 
}
288
 
 
289
 
sub get_work_by_owners {
290
 
    my $sth = query_work_by_buglist(@_);
291
 
    my %res;
292
 
    while (my $row = $sth->fetch) {
293
 
        # XXX: Why do we need to check if the total time is positive
294
 
        # instead of using SQL to do that?  Simply because MySQL 3.x's
295
 
        # GROUP BY doesn't work correctly with aggregates. This is
296
 
        # really annoying, but I've spent a long time trying to wrestle
297
 
        # with it and it just doesn't seem to work. Should work OK in
298
 
        # 4.x, though.
299
 
        if ($row->[0] > 0) {
300
 
            my $login_name = $row->[1];
301
 
            push @{$res{$login_name}}, { total_time => $row->[0],
302
 
                                         bug_id     => $row->[2],
303
 
                                         short_desc => $row->[3],
304
 
                                         bug_status => $row->[4] };
305
 
        }
306
 
    }
307
 
    return \%res;
308
 
}
309
 
 
310
 
sub get_work_by_bugs {
311
 
    my $sth = query_work_by_buglist(@_);
312
 
    my %res;
313
 
    while (my $row = $sth->fetch) {
314
 
        # Perl doesn't let me use arrays as keys :-(
315
 
        # merge in ID, status and summary
316
 
        my $bug = join ";", ($row->[2], $row->[4], $row->[3]);
317
 
        # XXX: see comment in get_work_by_owners
318
 
        if ($row->[0] > 0) {
319
 
            push @{$res{$bug}}, { total_time => $row->[0],
320
 
                                  login_name => $row->[1], };
321
 
        }
322
 
    }
323
 
    return \%res;
324
 
}
325
 
 
326
 
sub get_inactive_bugs {
327
 
    my ($bugids, $start_date, $end_date) = @_;
328
 
    my $dbh = Bugzilla->dbh;
329
 
    my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
330
 
    my $buglist = join ", ", @{$bugids};
331
 
 
332
 
    my %res;
333
 
    # This sucks. I need to make sure that even bugs that *don't* show
334
 
    # up in the longdescs query (because no comments were filed during
335
 
    # the specified period) but *are* dependent on the parent bug show
336
 
    # up in the results if they have no work done; that's why I prefill
337
 
    # them in %res here and then remove them below.
338
 
    my $q = qq{SELECT DISTINCT bugs.bug_id, bugs.short_desc ,
339
 
                               bugs.bug_status
340
 
               FROM   longdescs
341
 
               INNER JOIN bugs
342
 
                    ON longdescs.bug_id = bugs.bug_id
343
 
               WHERE  longdescs.bug_id in ($buglist)};
344
 
    my $sth = $dbh->prepare($q);
345
 
    $sth->execute();
346
 
    while (my $row = $sth->fetch) {
347
 
        $res{$row->[0]} = [$row->[1], $row->[2]];
348
 
    }
349
 
 
350
 
    # Returns the total time worked on each bug, with description. This
351
 
    # query differs a bit from one in the query_work_by_buglist and I
352
 
    # avoided complicating that one just to make it more general.
353
 
    $q = qq{SELECT sum(longdescs.work_time) as total_time,
354
 
                   longdescs.bug_id,
355
 
                   bugs.short_desc,
356
 
                   bugs.bug_status
357
 
            FROM   longdescs
358
 
            INNER JOIN bugs
359
 
                ON bugs.bug_id = longdescs.bug_id 
360
 
            WHERE  longdescs.bug_id IN ($buglist)
361
 
                   $date_bits } .
362
 
         $dbh->sql_group_by('longdescs.bug_id',
363
 
                            'bugs.short_desc, bugs.bug_status,
364
 
                             longdescs.bug_when') . qq{
365
 
            ORDER BY longdescs.bug_when};
366
 
    $sth = $dbh->prepare($q);
367
 
    $sth->execute(@{$date_values});
368
 
    while (my $row = $sth->fetch) {
369
 
        # XXX: see comment in get_work_by_owners
370
 
        if ($row->[0] == 0) {
371
 
            $res{$row->[1]} = [$row->[2], $row->[3]];
372
 
        } else {
373
 
            delete $res{$row->[1]};
374
 
        }
375
 
    }
376
 
    return \%res;
377
 
}
378
 
 
379
 
#
380
 
# Misc
381
 
#
382
 
 
383
 
sub sort_bug_keys {
384
 
    # XXX a hack is the mother of all evils. The fact that we store keys
385
 
    # joined by semi-colons in the workdata-by-bug structure forces us to
386
 
    # write this evil comparison function to ensure we can process the
387
 
    # data timely -- just pushing it through a numerical sort makes TT
388
 
    # hang while generating output :-(
389
 
    my $list = shift;
390
 
    my @a;
391
 
    my @b;
392
 
    return sort { @a = split(";", $a); 
393
 
                  @b = split(";", $b); 
394
 
                  $a[0] <=> $b[0] } @{$list};
395
 
}
396
 
 
397
 
#
398
 
# Template code starts here
399
 
#
400
 
 
401
 
Bugzilla->login(LOGIN_REQUIRED);
402
 
 
403
 
my $cgi = Bugzilla->cgi;
404
 
 
405
 
Bugzilla->switch_to_shadow_db();
406
 
 
407
 
Bugzilla->user->in_group(Bugzilla->params->{"timetrackinggroup"})
408
 
    || ThrowUserError("auth_failure", {group  => "time-tracking",
409
 
                                       action => "access",
410
 
                                       object => "timetracking_summaries"});
411
 
 
412
 
my @ids = split(",", $cgi->param('id'));
413
 
map { ValidateBugID($_) } @ids;
414
 
@ids = map { detaint_natural($_) && $_ } @ids;
415
 
@ids = grep { Bugzilla->user->can_see_bug($_) } @ids;
416
 
 
417
 
my $group_by = $cgi->param('group_by') || "number";
418
 
my $monthly = $cgi->param('monthly');
419
 
my $detailed = $cgi->param('detailed');
420
 
my $do_report = $cgi->param('do_report');
421
 
my $inactive = $cgi->param('inactive');
422
 
my $do_depends = $cgi->param('do_depends');
423
 
my $ctype = scalar($cgi->param("ctype"));
424
 
 
425
 
my ($start_date, $end_date);
426
 
if ($do_report && @ids) {
427
 
    my @bugs = @ids;
428
 
 
429
 
    # Dependency mode requires a single bug and grabs dependents.
430
 
    if ($do_depends) {
431
 
        if (scalar(@bugs) != 1) {
432
 
            ThrowCodeError("bad_arg", { argument=>"id",
433
 
                                        function=>"summarize_time"});
434
 
        }
435
 
        @bugs = get_blocker_ids_unique($bugs[0]);
436
 
        @bugs = grep { Bugzilla->user->can_see_bug($_) } @bugs;
437
 
    }
438
 
 
439
 
    $start_date = trim $cgi->param('start_date');
440
 
    $end_date = trim $cgi->param('end_date');
441
 
 
442
 
    # Swap dates in case the user put an end_date before the start_date
443
 
    if ($start_date && $end_date && 
444
 
        str2time($start_date) > str2time($end_date)) {
445
 
        $vars->{'warn_swap_dates'} = 1;
446
 
        ($start_date, $end_date) = ($end_date, $start_date);
447
 
    }
448
 
    ($start_date, $end_date) = check_dates($start_date, $end_date);
449
 
 
450
 
    if ($detailed) {
451
 
        my %detail_data;
452
 
        my $res = include_tt_details(\%detail_data, \@bugs, $start_date, $end_date);
453
 
 
454
 
        $vars->{'detail_data'} = $res;
455
 
    }
456
 
  
457
 
    # Store dates ia session cookie the dates so re-visiting the page
458
 
    # for other bugs keeps them around.
459
 
    $cgi->send_cookie(-name => 'time-summary-dates',
460
 
                      -value => join ";", ($start_date, $end_date));
461
 
 
462
 
    my (@parts, $part_data, @part_list);
463
 
 
464
 
    # Break dates apart into months if necessary; if not, we use the
465
 
    # same @parts list to allow us to use a common codepath.
466
 
    if ($monthly) {
467
 
        # unfortunately it's not too easy to guess a start date, since
468
 
        # it depends on what bugs we're looking at. We risk bothering
469
 
        # the user here. XXX: perhaps run a query to see what the
470
 
        # earliest activity in longdescs for all bugs and use that as a
471
 
        # start date.
472
 
        $start_date || ThrowUserError("illegal_date", {'date' => $start_date});
473
 
        # we can, however, provide a default end date. Note that this
474
 
        # differs in semantics from the open-ended queries we use when
475
 
        # start/end_date aren't provided -- and clock skews will make
476
 
        # this evident!
477
 
        @parts = split_by_month($start_date, 
478
 
                                $end_date || time2str("%Y-%m-%d", time()));
479
 
    } else {
480
 
        @parts = ([$start_date, $end_date]);
481
 
    }
482
 
 
483
 
    my %empty_hash;
484
 
    # For each of the separate divisions, grab the relevant summaries 
485
 
    foreach my $part (@parts) {
486
 
        my ($sub_start, $sub_end) = @{$part};
487
 
        if (@bugs) {
488
 
            if ($group_by eq "owner") {
489
 
                $part_data = get_work_by_owners(\@bugs, $sub_start, $sub_end);
490
 
            } else {
491
 
                $part_data = get_work_by_bugs(\@bugs, $sub_start, $sub_end);
492
 
            }
493
 
        } else {
494
 
            # $part_data must be a reference to a hash
495
 
            $part_data = \%empty_hash; 
496
 
        }
497
 
        push @part_list, $part_data;
498
 
    }
499
 
 
500
 
    if ($inactive && @bugs) {
501
 
        $vars->{'null'} = get_inactive_bugs(\@bugs, $start_date, $end_date);
502
 
    } else {
503
 
        $vars->{'null'} = \%empty_hash;
504
 
    }
505
 
 
506
 
    $vars->{'part_list'} = \@part_list;
507
 
    $vars->{'parts'} = \@parts;
508
 
 
509
 
} elsif ($cgi->cookie("time-summary-dates")) {
510
 
    ($start_date, $end_date) = split ";", $cgi->cookie('time-summary-dates');
511
 
}
512
 
 
513
 
$vars->{'ids'} = \@ids;
514
 
$vars->{'start_date'} = $start_date;
515
 
$vars->{'end_date'} = $end_date;
516
 
$vars->{'group_by'} = $group_by;
517
 
$vars->{'monthly'} = $monthly;
518
 
$vars->{'detailed'} = $detailed;
519
 
$vars->{'inactive'} = $inactive;
520
 
$vars->{'do_report'} = $do_report;
521
 
$vars->{'do_depends'} = $do_depends;
522
 
$vars->{'check_time'} = \&check_time;
523
 
$vars->{'sort_bug_keys'} = \&sort_bug_keys;
524
 
 
525
 
my $format = $template->get_format("bug/summarize-time", undef, $ctype);
526
 
 
527
 
# Get the proper content-type
528
 
print $cgi->header(-type=> $format->{'ctype'});
529
 
$template->process("$format->{'template'}", $vars)
530
 
  || ThrowTemplateError($template->error());