2
# -*- Mode: perl; indent-tabs-mode: nil -*-
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/
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.
14
# The Original Code is the Bugzilla Bug Tracking System.
16
# Contributor(s): Christian Reis <kiko@async.com.br>
17
# Shane H. W. Travis <travis@sedsystems.ca>
23
use Date::Parse; # strptime
24
use Date::Format; # strftime
27
use Bugzilla::Constants; # LOGIN_*
28
use Bugzilla::Bug; # EmitDependList
29
use Bugzilla::Util; # trim
31
use Bugzilla::User; # Bugzilla->user->in_group
33
my $template = Bugzilla->template;
40
sub date_adjust_down {
42
my ($year, $month, $day) = @_;
47
# Proper day adjustment is done later.
55
if (($month == 2) && ($day > 28)) {
56
if ($year % 4 == 0 && $year % 100 != 0) {
63
if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
68
return ($year, $month, $day);
72
my ($year, $month, $day) = @_;
84
if ($month == 2 && $day > 28) {
85
if ($year % 4 != 0 || $year % 100 == 0 || $day > 29) {
91
if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
98
return ($year, $month, $day);
102
my ($start_date, $end_date) = @_;
104
if (!str2time($start_date)) {
105
ThrowUserError("illegal_date", {'date' => $start_date});
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));
116
if (!str2time($end_date)) {
117
ThrowUserError("illegal_date", {'date' => $end_date});
119
# see related comment above.
120
$end_date = time2str("%Y-%m-%d", str2time($end_date));
122
return ($start_date, $end_date);
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) = @_;
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);
134
# Find out how many months fit between the two dates so we know
135
# how many times we loop.
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.
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;
148
# Keep the original date, when the date will be changed in the adjust_date.
150
my $month_tmp = $month;
151
my $year_tmp = $year;
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);
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];
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];
178
sub include_tt_details {
179
my ($res, $bugids, $start_date, $end_date) = @_;
182
my $dbh = Bugzilla->dbh;
183
my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
184
my $buglist = join ", ", @{$bugids};
186
my $q = qq{SELECT bugs.bug_id, profiles.login_name, bugs.deadline,
187
bugs.estimated_time, bugs.remaining_time
190
ON longdescs.bug_id = bugs.bug_id
192
ON longdescs.who = profiles.userid
193
WHERE longdescs.bug_id in ($buglist) $date_bits};
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];
207
my ($start_date, $end_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;
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);
223
$date_bits .= " AND longdescs.bug_when < ?";
224
push @date_values, $end_date;
226
return ($date_bits, \@date_values);
233
sub get_blocker_ids_unique {
236
get_blocker_ids_deep($bug_id, \@ret);
238
foreach my $blocker (@ret) {
239
$unique{$blocker} = $blocker
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);
254
# Queries and data structure assembly
257
sub query_work_by_buglist {
258
my ($bugids, $start_date, $end_date) = @_;
259
my $dbh = Bugzilla->dbh;
261
my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
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};
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,
276
ON longdescs.who = profiles.userid
278
ON bugs.bug_id = longdescs.bug_id
279
WHERE longdescs.bug_id IN ($buglist)
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});
289
sub get_work_by_owners {
290
my $sth = query_work_by_buglist(@_);
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
300
my $login_name = $row->[1];
301
push @{$res{$login_name}}, { total_time => $row->[0],
303
short_desc => $row->[3],
304
bug_status => $row->[4] };
310
sub get_work_by_bugs {
311
my $sth = query_work_by_buglist(@_);
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
319
push @{$res{$bug}}, { total_time => $row->[0],
320
login_name => $row->[1], };
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};
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 ,
342
ON longdescs.bug_id = bugs.bug_id
343
WHERE longdescs.bug_id in ($buglist)};
344
my $sth = $dbh->prepare($q);
346
while (my $row = $sth->fetch) {
347
$res{$row->[0]} = [$row->[1], $row->[2]];
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,
359
ON bugs.bug_id = longdescs.bug_id
360
WHERE longdescs.bug_id IN ($buglist)
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]];
373
delete $res{$row->[1]};
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 :-(
392
return sort { @a = split(";", $a);
394
$a[0] <=> $b[0] } @{$list};
398
# Template code starts here
401
Bugzilla->login(LOGIN_REQUIRED);
403
my $cgi = Bugzilla->cgi;
405
Bugzilla->switch_to_shadow_db();
407
Bugzilla->user->in_group(Bugzilla->params->{"timetrackinggroup"})
408
|| ThrowUserError("auth_failure", {group => "time-tracking",
410
object => "timetracking_summaries"});
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;
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"));
425
my ($start_date, $end_date);
426
if ($do_report && @ids) {
429
# Dependency mode requires a single bug and grabs dependents.
431
if (scalar(@bugs) != 1) {
432
ThrowCodeError("bad_arg", { argument=>"id",
433
function=>"summarize_time"});
435
@bugs = get_blocker_ids_unique($bugs[0]);
436
@bugs = grep { Bugzilla->user->can_see_bug($_) } @bugs;
439
$start_date = trim $cgi->param('start_date');
440
$end_date = trim $cgi->param('end_date');
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);
448
($start_date, $end_date) = check_dates($start_date, $end_date);
452
my $res = include_tt_details(\%detail_data, \@bugs, $start_date, $end_date);
454
$vars->{'detail_data'} = $res;
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));
462
my (@parts, $part_data, @part_list);
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.
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
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
477
@parts = split_by_month($start_date,
478
$end_date || time2str("%Y-%m-%d", time()));
480
@parts = ([$start_date, $end_date]);
484
# For each of the separate divisions, grab the relevant summaries
485
foreach my $part (@parts) {
486
my ($sub_start, $sub_end) = @{$part};
488
if ($group_by eq "owner") {
489
$part_data = get_work_by_owners(\@bugs, $sub_start, $sub_end);
491
$part_data = get_work_by_bugs(\@bugs, $sub_start, $sub_end);
494
# $part_data must be a reference to a hash
495
$part_data = \%empty_hash;
497
push @part_list, $part_data;
500
if ($inactive && @bugs) {
501
$vars->{'null'} = get_inactive_bugs(\@bugs, $start_date, $end_date);
503
$vars->{'null'} = \%empty_hash;
506
$vars->{'part_list'} = \@part_list;
507
$vars->{'parts'} = \@parts;
509
} elsif ($cgi->cookie("time-summary-dates")) {
510
($start_date, $end_date) = split ";", $cgi->cookie('time-summary-dates');
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;
525
my $format = $template->get_format("bug/summarize-time", undef, $ctype);
527
# Get the proper content-type
528
print $cgi->header(-type=> $format->{'ctype'});
529
$template->process("$format->{'template'}", $vars)
530
|| ThrowTemplateError($template->error());