blob: 5ebb7e1a70d96f80fe7b61658510ca25b3690b80 [file] [log] [blame]
ddkilzer@apple.comf573e632009-07-03 02:14:39 +00001#!/usr/bin/env perl -wT
timothy@apple.comf42518d2008-02-06 20:19:16 +00002# -*- 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.com097da082009-07-03 02:14:25 +000018# Frédéric Buclin <LpSolit@gmail.com>
19
timothy@apple.comf42518d2008-02-06 20:19:16 +000020use strict;
21
ddkilzer@apple.com097da082009-07-03 02:14:25 +000022use lib qw(. lib);
timothy@apple.comf42518d2008-02-06 20:19:16 +000023
24use Date::Parse; # strptime
timothy@apple.comf42518d2008-02-06 20:19:16 +000025
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000026use Bugzilla;
27use Bugzilla::Constants; # LOGIN_*
timothy@apple.comf42518d2008-02-06 20:19:16 +000028use Bugzilla::Bug; # EmitDependList
29use Bugzilla::Util; # trim
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000030use Bugzilla::Error;
timothy@apple.comf42518d2008-02-06 20:19:16 +000031
32#
33# Date handling
34#
35
36sub 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
67sub 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.comf42518d2008-02-06 20:19:16 +000097sub 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.comf42518d2008-02-06 20:19:16 +0000150sub sqlize_dates {
151 my ($start_date, $end_date) = @_;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000152 my $date_bits = "";
timothy@apple.comf42518d2008-02-06 20:19:16 +0000153 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.com097da082009-07-03 02:14:25 +0000173# Return all blockers of the current bug, recursively.
174sub get_blocker_ids {
175 my ($bug_id, $unique) = @_;
176 $unique ||= {$bug_id => 1};
timothy@apple.comf42518d2008-02-06 20:19:16 +0000177 my $deps = Bugzilla::Bug::EmitDependList("blocked", "dependson", $bug_id);
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000178 my @unseen = grep { !$unique->{$_}++ } @$deps;
179 foreach $bug_id (@unseen) {
180 get_blocker_ids($bug_id, $unique);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000181 }
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000182 return keys %$unique;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000183}
184
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000185# 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.
189sub get_list {
190 my ($bugids, $start_date, $end_date, $keyname) = @_;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000191 my $dbh = Bugzilla->dbh;
192
193 my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000194 my $buglist = join(", ", @$bugids);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000195
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000196 # 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.comf42518d2008-02-06 20:19:16 +0000201 ON longdescs.who = profiles.userid
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000202 INNER JOIN bugs
timothy@apple.comf42518d2008-02-06 20:19:16 +0000203 ON bugs.bug_id = longdescs.bug_id
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000204 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.comf42518d2008-02-06 20:19:16 +0000212}
213
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000214# Return bugs which had no activity (a.k.a work_time = 0) during the given time range.
timothy@apple.comf42518d2008-02-06 20:19:16 +0000215sub 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.com097da082009-07-03 02:14:25 +0000219 my $buglist = join(", ", @$bugids);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000220
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000221 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.comf42518d2008-02-06 20:19:16 +0000231
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000232 return $bugs;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000233}
234
235#
236# Template code starts here
237#
238
239Bugzilla->login(LOGIN_REQUIRED);
240
241my $cgi = Bugzilla->cgi;
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000242my $user = Bugzilla->user;
243my $template = Bugzilla->template;
244my $vars = {};
timothy@apple.comf42518d2008-02-06 20:19:16 +0000245
246Bugzilla->switch_to_shadow_db();
247
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000248$user->in_group(Bugzilla->params->{"timetrackinggroup"})
timothy@apple.comf42518d2008-02-06 20:19:16 +0000249 || ThrowUserError("auth_failure", {group => "time-tracking",
250 action => "access",
251 object => "timetracking_summaries"});
252
253my @ids = split(",", $cgi->param('id'));
254map { ValidateBugID($_) } @ids;
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000255scalar(@ids) || ThrowUserError('no_bugs_chosen', {action => 'view'});
timothy@apple.comf42518d2008-02-06 20:19:16 +0000256
257my $group_by = $cgi->param('group_by') || "number";
258my $monthly = $cgi->param('monthly');
259my $detailed = $cgi->param('detailed');
260my $do_report = $cgi->param('do_report');
261my $inactive = $cgi->param('inactive');
262my $do_depends = $cgi->param('do_depends');
263my $ctype = scalar($cgi->param("ctype"));
264
265my ($start_date, $end_date);
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000266if ($do_report) {
timothy@apple.comf42518d2008-02-06 20:19:16 +0000267 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.com097da082009-07-03 02:14:25 +0000275 @bugs = get_blocker_ids($bugs[0]);
276 @bugs = grep { $user->can_see_bug($_) } @bugs;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000277 }
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.com097da082009-07-03 02:14:25 +0000288 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.comf42518d2008-02-06 20:19:16 +0000292 }
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000293
294 # Store dates in a session cookie so re-visiting the page
timothy@apple.comf42518d2008-02-06 20:19:16 +0000295 # 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.com097da082009-07-03 02:14:25 +0000315 $end_date || format_time(scalar localtime(time()), '%Y-%m-%d'));
timothy@apple.comf42518d2008-02-06 20:19:16 +0000316 } else {
317 @parts = ([$start_date, $end_date]);
318 }
319
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000320 # For each of the separate divisions, grab the relevant data.
321 my $keyname = ($group_by eq 'owner') ? 'login_name' : 'bug_id';
timothy@apple.comf42518d2008-02-06 20:19:16 +0000322 foreach my $part (@parts) {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000323 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.comf42518d2008-02-06 20:19:16 +0000326 }
327
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000328 # Do we want to see inactive bugs?
329 if ($inactive) {
timothy@apple.comf42518d2008-02-06 20:19:16 +0000330 $vars->{'null'} = get_inactive_bugs(\@bugs, $start_date, $end_date);
331 } else {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000332 $vars->{'null'} = {};
timothy@apple.comf42518d2008-02-06 20:19:16 +0000333 }
334
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000335 # Convert bug IDs to bug objects.
336 @bugs = map {new Bugzilla::Bug($_)} @bugs;
337
timothy@apple.comf42518d2008-02-06 20:19:16 +0000338 $vars->{'part_list'} = \@part_list;
339 $vars->{'parts'} = \@parts;
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000340 # We pass the list of bugs as a hashref.
341 $vars->{'bugs'} = {map { $_->id => $_ } @bugs};
342}
343elsif ($cgi->cookie("time-summary-dates")) {
timothy@apple.comf42518d2008-02-06 20:19:16 +0000344 ($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.comf42518d2008-02-06 20:19:16 +0000356
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000357my $format = $template->get_format("bug/summarize-time", undef, $ctype);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000358
359# Get the proper content-type
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000360print $cgi->header(-type=> $format->{'ctype'});
timothy@apple.comf42518d2008-02-06 20:19:16 +0000361$template->process("$format->{'template'}", $vars)
362 || ThrowTemplateError($template->error());