ddkilzer@apple.com | f573e63 | 2009-07-03 02:14:39 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl -wT |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 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> |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 18 | # Frédéric Buclin <LpSolit@gmail.com> |
| 19 | |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 20 | use strict; |
| 21 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 22 | use lib qw(. lib); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 23 | |
| 24 | use Date::Parse; # strptime |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 25 | |
ddkilzer@apple.com | f3615fc | 2009-07-03 02:13:41 +0000 | [diff] [blame] | 26 | use Bugzilla; |
| 27 | use Bugzilla::Constants; # LOGIN_* |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 28 | use Bugzilla::Bug; # EmitDependList |
| 29 | use Bugzilla::Util; # trim |
ddkilzer@apple.com | f3615fc | 2009-07-03 02:13:41 +0000 | [diff] [blame] | 30 | use Bugzilla::Error; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 31 | |
| 32 | # |
| 33 | # Date handling |
| 34 | # |
| 35 | |
| 36 | sub date_adjust_down { |
| 37 | |
| 38 | my ($year, $month, $day) = @_; |
| 39 | |
| 40 | if ($day == 0) { |
| 41 | $month -= 1; |
| 42 | $day = 31; |
| 43 | # Proper day adjustment is done later. |
| 44 | |
| 45 | if ($month == 0) { |
| 46 | $year -= 1; |
| 47 | $month = 12; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (($month == 2) && ($day > 28)) { |
| 52 | if ($year % 4 == 0 && $year % 100 != 0) { |
| 53 | $day = 29; |
| 54 | } else { |
| 55 | $day = 28; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (($month == 4 || $month == 6 || $month == 9 || $month == 11) && |
| 60 | ($day == 31) ) |
| 61 | { |
| 62 | $day = 30; |
| 63 | } |
| 64 | return ($year, $month, $day); |
| 65 | } |
| 66 | |
| 67 | sub date_adjust_up { |
| 68 | my ($year, $month, $day) = @_; |
| 69 | |
| 70 | if ($day > 31) { |
| 71 | $month += 1; |
| 72 | $day = 1; |
| 73 | |
| 74 | if ($month == 13) { |
| 75 | $month = 1; |
| 76 | $year += 1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if ($month == 2 && $day > 28) { |
| 81 | if ($year % 4 != 0 || $year % 100 == 0 || $day > 29) { |
| 82 | $month = 3; |
| 83 | $day = 1; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (($month == 4 || $month == 6 || $month == 9 || $month == 11) && |
| 88 | ($day == 31) ) |
| 89 | { |
| 90 | $month += 1; |
| 91 | $day = 1; |
| 92 | } |
| 93 | |
| 94 | return ($year, $month, $day); |
| 95 | } |
| 96 | |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 97 | sub split_by_month { |
| 98 | # Takes start and end dates and splits them into a list of |
| 99 | # monthly-spaced 2-lists of dates. |
| 100 | my ($start_date, $end_date) = @_; |
| 101 | |
| 102 | # We assume at this point that the dates are provided and sane |
| 103 | my (undef, undef, undef, $sd, $sm, $sy, undef) = strptime($start_date); |
| 104 | my (undef, undef, undef, $ed, $em, $ey, undef) = strptime($end_date); |
| 105 | |
| 106 | # Find out how many months fit between the two dates so we know |
| 107 | # how many times we loop. |
| 108 | my $yd = $ey - $sy; |
| 109 | my $md = 12 * $yd + $em - $sm; |
| 110 | # If the end day is smaller than the start day, last interval is not a whole month. |
| 111 | if ($sd > $ed) { |
| 112 | $md -= 1; |
| 113 | } |
| 114 | |
| 115 | my (@months, $sub_start, $sub_end); |
| 116 | # This +1 and +1900 are a result of strptime's bizarre semantics |
| 117 | my $year = $sy + 1900; |
| 118 | my $month = $sm + 1; |
| 119 | |
| 120 | # Keep the original date, when the date will be changed in the adjust_date. |
| 121 | my $sd_tmp = $sd; |
| 122 | my $month_tmp = $month; |
| 123 | my $year_tmp = $year; |
| 124 | |
| 125 | # This section handles only the whole months. |
| 126 | for (my $i=0; $i < $md; $i++) { |
| 127 | # Start of interval is adjusted up: 31.2. -> 1.3. |
| 128 | ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_up($year, $month, $sd); |
| 129 | $sub_start = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp); |
| 130 | $month += 1; |
| 131 | if ($month == 13) { |
| 132 | $month = 1; |
| 133 | $year += 1; |
| 134 | } |
| 135 | # End of interval is adjusted down: 31.2 -> 28.2. |
| 136 | ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_down($year, $month, $sd - 1); |
| 137 | $sub_end = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp); |
| 138 | push @months, [$sub_start, $sub_end]; |
| 139 | } |
| 140 | |
| 141 | # This section handles the last (unfinished) month. |
| 142 | $sub_end = sprintf("%04d-%02d-%02d", $ey + 1900, $em + 1, $ed); |
| 143 | ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_up($year, $month, $sd); |
| 144 | $sub_start = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp); |
| 145 | push @months, [$sub_start, $sub_end]; |
| 146 | |
| 147 | return @months; |
| 148 | } |
| 149 | |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 150 | sub sqlize_dates { |
| 151 | my ($start_date, $end_date) = @_; |
ddkilzer@apple.com | f3615fc | 2009-07-03 02:13:41 +0000 | [diff] [blame] | 152 | my $date_bits = ""; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 153 | my @date_values; |
| 154 | if ($start_date) { |
| 155 | # we've checked, trick_taint is fine |
| 156 | trick_taint($start_date); |
| 157 | $date_bits = " AND longdescs.bug_when > ?"; |
| 158 | push @date_values, $start_date; |
| 159 | } |
| 160 | if ($end_date) { |
| 161 | # we need to add one day to end_date to catch stuff done today |
| 162 | # do not forget to adjust date if it was the last day of month |
| 163 | my (undef, undef, undef, $ed, $em, $ey, undef) = strptime($end_date); |
| 164 | ($ey, $em, $ed) = date_adjust_up($ey+1900, $em+1, $ed+1); |
| 165 | $end_date = sprintf("%04d-%02d-%02d", $ey, $em, $ed); |
| 166 | |
| 167 | $date_bits .= " AND longdescs.bug_when < ?"; |
| 168 | push @date_values, $end_date; |
| 169 | } |
| 170 | return ($date_bits, \@date_values); |
| 171 | } |
| 172 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 173 | # Return all blockers of the current bug, recursively. |
| 174 | sub get_blocker_ids { |
| 175 | my ($bug_id, $unique) = @_; |
| 176 | $unique ||= {$bug_id => 1}; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 177 | my $deps = Bugzilla::Bug::EmitDependList("blocked", "dependson", $bug_id); |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 178 | my @unseen = grep { !$unique->{$_}++ } @$deps; |
| 179 | foreach $bug_id (@unseen) { |
| 180 | get_blocker_ids($bug_id, $unique); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 181 | } |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 182 | return keys %$unique; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 183 | } |
| 184 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 185 | # Return a hashref whose key is chosen by the user (bug ID or commenter) |
| 186 | # and value is a hash of the form {bug ID, commenter, time spent}. |
| 187 | # So you can either view it as the time spent by commenters on each bug |
| 188 | # or the time spent in bugs by each commenter. |
| 189 | sub get_list { |
| 190 | my ($bugids, $start_date, $end_date, $keyname) = @_; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 191 | my $dbh = Bugzilla->dbh; |
| 192 | |
| 193 | my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date); |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 194 | my $buglist = join(", ", @$bugids); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 195 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 196 | # Returns the total time worked on each bug *per developer*. |
| 197 | my $data = $dbh->selectall_arrayref( |
| 198 | qq{SELECT SUM(work_time) AS total_time, login_name, longdescs.bug_id |
| 199 | FROM longdescs |
| 200 | INNER JOIN profiles |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 201 | ON longdescs.who = profiles.userid |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 202 | INNER JOIN bugs |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 203 | ON bugs.bug_id = longdescs.bug_id |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 204 | WHERE longdescs.bug_id IN ($buglist) $date_bits } . |
| 205 | $dbh->sql_group_by('longdescs.bug_id, login_name', 'longdescs.bug_when') . |
| 206 | qq{ HAVING SUM(work_time) > 0}, {Slice => {}}, @$date_values); |
| 207 | |
| 208 | my %list; |
| 209 | # What this loop does is to push data having the same key in an array. |
| 210 | push(@{$list{ $_->{$keyname} }}, $_) foreach @$data; |
| 211 | return \%list; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 212 | } |
| 213 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 214 | # Return bugs which had no activity (a.k.a work_time = 0) during the given time range. |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 215 | sub get_inactive_bugs { |
| 216 | my ($bugids, $start_date, $end_date) = @_; |
| 217 | my $dbh = Bugzilla->dbh; |
| 218 | my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date); |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 219 | my $buglist = join(", ", @$bugids); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 220 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 221 | my $bugs = $dbh->selectcol_arrayref( |
| 222 | "SELECT bug_id |
| 223 | FROM bugs |
| 224 | WHERE bugs.bug_id IN ($buglist) |
| 225 | AND NOT EXISTS ( |
| 226 | SELECT 1 |
| 227 | FROM longdescs |
| 228 | WHERE bugs.bug_id = longdescs.bug_id |
| 229 | AND work_time > 0 $date_bits)", |
| 230 | undef, @$date_values); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 231 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 232 | return $bugs; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | # |
| 236 | # Template code starts here |
| 237 | # |
| 238 | |
| 239 | Bugzilla->login(LOGIN_REQUIRED); |
| 240 | |
| 241 | my $cgi = Bugzilla->cgi; |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 242 | my $user = Bugzilla->user; |
| 243 | my $template = Bugzilla->template; |
| 244 | my $vars = {}; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 245 | |
| 246 | Bugzilla->switch_to_shadow_db(); |
| 247 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 248 | $user->in_group(Bugzilla->params->{"timetrackinggroup"}) |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 249 | || ThrowUserError("auth_failure", {group => "time-tracking", |
| 250 | action => "access", |
| 251 | object => "timetracking_summaries"}); |
| 252 | |
| 253 | my @ids = split(",", $cgi->param('id')); |
| 254 | map { ValidateBugID($_) } @ids; |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 255 | scalar(@ids) || ThrowUserError('no_bugs_chosen', {action => 'view'}); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 256 | |
| 257 | my $group_by = $cgi->param('group_by') || "number"; |
| 258 | my $monthly = $cgi->param('monthly'); |
| 259 | my $detailed = $cgi->param('detailed'); |
| 260 | my $do_report = $cgi->param('do_report'); |
| 261 | my $inactive = $cgi->param('inactive'); |
| 262 | my $do_depends = $cgi->param('do_depends'); |
| 263 | my $ctype = scalar($cgi->param("ctype")); |
| 264 | |
| 265 | my ($start_date, $end_date); |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 266 | if ($do_report) { |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 267 | my @bugs = @ids; |
| 268 | |
| 269 | # Dependency mode requires a single bug and grabs dependents. |
| 270 | if ($do_depends) { |
| 271 | if (scalar(@bugs) != 1) { |
| 272 | ThrowCodeError("bad_arg", { argument=>"id", |
| 273 | function=>"summarize_time"}); |
| 274 | } |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 275 | @bugs = get_blocker_ids($bugs[0]); |
| 276 | @bugs = grep { $user->can_see_bug($_) } @bugs; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | $start_date = trim $cgi->param('start_date'); |
| 280 | $end_date = trim $cgi->param('end_date'); |
| 281 | |
| 282 | # Swap dates in case the user put an end_date before the start_date |
| 283 | if ($start_date && $end_date && |
| 284 | str2time($start_date) > str2time($end_date)) { |
| 285 | $vars->{'warn_swap_dates'} = 1; |
| 286 | ($start_date, $end_date) = ($end_date, $start_date); |
| 287 | } |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 288 | foreach my $date ($start_date, $end_date) { |
| 289 | next unless $date; |
| 290 | validate_date($date) |
| 291 | || ThrowUserError('illegal_date', {date => $date, format => 'YYYY-MM-DD'}); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 292 | } |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 293 | |
| 294 | # Store dates in a session cookie so re-visiting the page |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 295 | # for other bugs keeps them around. |
| 296 | $cgi->send_cookie(-name => 'time-summary-dates', |
| 297 | -value => join ";", ($start_date, $end_date)); |
| 298 | |
| 299 | my (@parts, $part_data, @part_list); |
| 300 | |
| 301 | # Break dates apart into months if necessary; if not, we use the |
| 302 | # same @parts list to allow us to use a common codepath. |
| 303 | if ($monthly) { |
| 304 | # unfortunately it's not too easy to guess a start date, since |
| 305 | # it depends on what bugs we're looking at. We risk bothering |
| 306 | # the user here. XXX: perhaps run a query to see what the |
| 307 | # earliest activity in longdescs for all bugs and use that as a |
| 308 | # start date. |
| 309 | $start_date || ThrowUserError("illegal_date", {'date' => $start_date}); |
| 310 | # we can, however, provide a default end date. Note that this |
| 311 | # differs in semantics from the open-ended queries we use when |
| 312 | # start/end_date aren't provided -- and clock skews will make |
| 313 | # this evident! |
| 314 | @parts = split_by_month($start_date, |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 315 | $end_date || format_time(scalar localtime(time()), '%Y-%m-%d')); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 316 | } else { |
| 317 | @parts = ([$start_date, $end_date]); |
| 318 | } |
| 319 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 320 | # For each of the separate divisions, grab the relevant data. |
| 321 | my $keyname = ($group_by eq 'owner') ? 'login_name' : 'bug_id'; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 322 | foreach my $part (@parts) { |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 323 | my ($sub_start, $sub_end) = @$part; |
| 324 | $part_data = get_list(\@bugs, $sub_start, $sub_end, $keyname); |
| 325 | push(@part_list, $part_data); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 326 | } |
| 327 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 328 | # Do we want to see inactive bugs? |
| 329 | if ($inactive) { |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 330 | $vars->{'null'} = get_inactive_bugs(\@bugs, $start_date, $end_date); |
| 331 | } else { |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 332 | $vars->{'null'} = {}; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 333 | } |
| 334 | |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 335 | # Convert bug IDs to bug objects. |
| 336 | @bugs = map {new Bugzilla::Bug($_)} @bugs; |
| 337 | |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 338 | $vars->{'part_list'} = \@part_list; |
| 339 | $vars->{'parts'} = \@parts; |
ddkilzer@apple.com | 097da08 | 2009-07-03 02:14:25 +0000 | [diff] [blame] | 340 | # We pass the list of bugs as a hashref. |
| 341 | $vars->{'bugs'} = {map { $_->id => $_ } @bugs}; |
| 342 | } |
| 343 | elsif ($cgi->cookie("time-summary-dates")) { |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 344 | ($start_date, $end_date) = split ";", $cgi->cookie('time-summary-dates'); |
| 345 | } |
| 346 | |
| 347 | $vars->{'ids'} = \@ids; |
| 348 | $vars->{'start_date'} = $start_date; |
| 349 | $vars->{'end_date'} = $end_date; |
| 350 | $vars->{'group_by'} = $group_by; |
| 351 | $vars->{'monthly'} = $monthly; |
| 352 | $vars->{'detailed'} = $detailed; |
| 353 | $vars->{'inactive'} = $inactive; |
| 354 | $vars->{'do_report'} = $do_report; |
| 355 | $vars->{'do_depends'} = $do_depends; |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 356 | |
ddkilzer@apple.com | f3615fc | 2009-07-03 02:13:41 +0000 | [diff] [blame] | 357 | my $format = $template->get_format("bug/summarize-time", undef, $ctype); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 358 | |
| 359 | # Get the proper content-type |
ddkilzer@apple.com | f3615fc | 2009-07-03 02:13:41 +0000 | [diff] [blame] | 360 | print $cgi->header(-type=> $format->{'ctype'}); |
timothy@apple.com | f42518d | 2008-02-06 20:19:16 +0000 | [diff] [blame] | 361 | $template->process("$format->{'template'}", $vars) |
| 362 | || ThrowTemplateError($template->error()); |