2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 # This Source Code Form is "Incompatible With Secondary Licenses", as
7 # defined by the Mozilla Public License, v. 2.0.
15 use Date
::Parse
; # strptime
18 use Bugzilla
::Constants
; # LOGIN_*
19 use Bugzilla
::Bug
; # EmitDependList
20 use Bugzilla
::Util
; # trim
27 sub date_adjust_down
{
29 my ($year, $month, $day) = @_;
34 # Proper day adjustment is done later.
42 if (($month == 2) && ($day > 28)) {
43 if ($year % 4 == 0 && $year % 100 != 0) {
50 if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
55 return ($year, $month, $day);
59 my ($year, $month, $day) = @_;
71 if ($month == 2 && $day > 28) {
72 if ($year % 4 != 0 || $year % 100 == 0 || $day > 29) {
78 if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
85 return ($year, $month, $day);
89 # Takes start and end dates and splits them into a list of
90 # monthly-spaced 2-lists of dates.
91 my ($start_date, $end_date) = @_;
93 # We assume at this point that the dates are provided and sane
94 my (undef, undef, undef, $sd, $sm, $sy, undef) = strptime
($start_date);
95 my (undef, undef, undef, $ed, $em, $ey, undef) = strptime
($end_date);
97 # Find out how many months fit between the two dates so we know
98 # how many times we loop.
100 my $md = 12 * $yd + $em - $sm;
101 # If the end day is smaller than the start day, last interval is not a whole month.
106 my (@months, $sub_start, $sub_end);
107 # This +1 and +1900 are a result of strptime's bizarre semantics
108 my $year = $sy + 1900;
111 # Keep the original date, when the date will be changed in the adjust_date.
113 my $month_tmp = $month;
114 my $year_tmp = $year;
116 # This section handles only the whole months.
117 for (my $i=0; $i < $md; $i++) {
118 # Start of interval is adjusted up: 31.2. -> 1.3.
119 ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_up
($year, $month, $sd);
120 $sub_start = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp);
126 # End of interval is adjusted down: 31.2 -> 28.2.
127 ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_down
($year, $month, $sd - 1);
128 $sub_end = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp);
129 push @months, [$sub_start, $sub_end];
132 # This section handles the last (unfinished) month.
133 $sub_end = sprintf("%04d-%02d-%02d", $ey + 1900, $em + 1, $ed);
134 ($year_tmp, $month_tmp, $sd_tmp) = date_adjust_up
($year, $month, $sd);
135 $sub_start = sprintf("%04d-%02d-%02d", $year_tmp, $month_tmp, $sd_tmp);
136 push @months, [$sub_start, $sub_end];
142 my ($start_date, $end_date) = @_;
146 # we've checked, trick_taint is fine
147 trick_taint
($start_date);
148 $date_bits = " AND longdescs.bug_when > ?";
149 push @date_values, $start_date;
152 # we need to add one day to end_date to catch stuff done today
153 # do not forget to adjust date if it was the last day of month
154 my (undef, undef, undef, $ed, $em, $ey, undef) = strptime
($end_date);
155 ($ey, $em, $ed) = date_adjust_up
($ey+1900, $em+1, $ed+1);
156 $end_date = sprintf("%04d-%02d-%02d", $ey, $em, $ed);
158 $date_bits .= " AND longdescs.bug_when < ?";
159 push @date_values, $end_date;
161 return ($date_bits, \
@date_values);
164 # Return all blockers of the current bug, recursively.
165 sub get_blocker_ids
{
166 my ($bug_id, $unique) = @_;
167 $unique ||= {$bug_id => 1};
168 my $deps = Bugzilla
::Bug
::EmitDependList
("blocked", "dependson", $bug_id);
169 my @unseen = grep { !$unique->{$_}++ } @
$deps;
170 foreach $bug_id (@unseen) {
171 get_blocker_ids
($bug_id, $unique);
173 return keys %$unique;
176 # Return a hashref whose key is chosen by the user (bug ID or commenter)
177 # and value is a hash of the form {bug ID, commenter, time spent}.
178 # So you can either view it as the time spent by commenters on each bug
179 # or the time spent in bugs by each commenter.
181 my ($bugids, $start_date, $end_date, $keyname) = @_;
182 my $dbh = Bugzilla
->dbh;
184 my ($date_bits, $date_values) = sqlize_dates
($start_date, $end_date);
185 my $buglist = join(", ", @
$bugids);
187 # Returns the total time worked on each bug *per developer*.
188 my $data = $dbh->selectall_arrayref(
189 qq{SELECT SUM
(work_time
) AS total_time
, login_name
, longdescs
.bug_id
192 ON longdescs
.who
= profiles
.userid
194 ON bugs
.bug_id
= longdescs
.bug_id
195 WHERE longdescs
.bug_id IN
($buglist) $date_bits } .
196 $dbh->sql_group_by('longdescs.bug_id, login_name', 'longdescs.bug_when') .
197 qq{ HAVING SUM
(work_time
) > 0}, {Slice
=> {}}, @
$date_values);
200 # What this loop does is to push data having the same key in an array.
201 push(@
{$list{ $_->{$keyname} }}, $_) foreach @
$data;
205 # Return bugs which had no activity (a.k.a work_time = 0) during the given time range.
206 sub get_inactive_bugs
{
207 my ($bugids, $start_date, $end_date) = @_;
208 my $dbh = Bugzilla
->dbh;
209 my ($date_bits, $date_values) = sqlize_dates
($start_date, $end_date);
210 my $buglist = join(", ", @
$bugids);
212 my $bugs = $dbh->selectcol_arrayref(
215 WHERE bugs.bug_id IN ($buglist)
219 WHERE bugs.bug_id = longdescs.bug_id
220 AND work_time > 0 $date_bits)",
221 undef, @
$date_values);
226 # Return 1st day of the month of the earliest activity date for a given list of bugs.
227 sub get_earliest_activity_date
{
229 my $dbh = Bugzilla
->dbh;
231 my ($date) = $dbh->selectrow_array(
232 'SELECT ' . $dbh->sql_date_format('MIN(bug_when)', '%Y-%m-01')
234 WHERE ' . $dbh->sql_in('bug_id', $bugids)
235 . ' AND work_time > 0');
241 # Template code starts here
244 my $user = Bugzilla
->login(LOGIN_REQUIRED
);
246 my $cgi = Bugzilla
->cgi;
247 my $template = Bugzilla
->template;
250 Bugzilla
->switch_to_shadow_db();
252 $user->is_timetracker
253 || ThrowUserError
("auth_failure", {group
=> "time-tracking",
255 object
=> "timetracking_summaries"});
257 my @ids = split(",", $cgi->param('id') || '');
258 @ids = map { Bugzilla
::Bug
->check($_)->id } @ids;
259 scalar(@ids) || ThrowUserError
('no_bugs_chosen', {action
=> 'summarize'});
261 my $group_by = $cgi->param('group_by') || "number";
262 my $monthly = $cgi->param('monthly');
263 my $detailed = $cgi->param('detailed');
264 my $do_report = $cgi->param('do_report');
265 my $inactive = $cgi->param('inactive');
266 my $do_depends = $cgi->param('do_depends');
267 my $ctype = scalar($cgi->param("ctype"));
269 my ($start_date, $end_date);
273 # Dependency mode requires a single bug and grabs dependents.
275 if (scalar(@bugs) != 1) {
276 ThrowCodeError
("bad_arg", { argument
=>"id",
277 function
=>"summarize_time"});
279 @bugs = get_blocker_ids
($bugs[0]);
280 @bugs = @
{ $user->visible_bugs(\
@bugs) };
283 $start_date = trim
$cgi->param('start_date');
284 $end_date = trim
$cgi->param('end_date');
286 foreach my $date ($start_date, $end_date) {
289 || ThrowUserError
('illegal_date', {date
=> $date, format
=> 'YYYY-MM-DD'});
291 # Swap dates in case the user put an end_date before the start_date
292 if ($start_date && $end_date &&
293 str2time
($start_date) > str2time
($end_date)) {
294 $vars->{'warn_swap_dates'} = 1;
295 ($start_date, $end_date) = ($end_date, $start_date);
298 # Store dates in a session cookie so re-visiting the page
299 # for other bugs keeps them around.
300 $cgi->send_cookie(-name
=> 'time-summary-dates',
301 -value
=> join ";", ($start_date, $end_date));
303 my (@parts, $part_data, @part_list);
305 # Break dates apart into months if necessary; if not, we use the
306 # same @parts list to allow us to use a common codepath.
308 # Calculate the earliest activity date if the user doesn't
309 # specify a start date.
311 $start_date = get_earliest_activity_date
(\
@bugs);
313 # Provide a default end date. Note that this differs in semantics
314 # from the open-ended queries we use when start/end_date aren't
315 # provided -- and clock skews will make this evident!
316 @parts = split_by_month
($start_date,
317 $end_date || format_time
(scalar localtime(time()), '%Y-%m-%d'));
319 @parts = ([$start_date, $end_date]);
322 # For each of the separate divisions, grab the relevant data.
323 my $keyname = ($group_by eq 'owner') ?
'login_name' : 'bug_id';
324 foreach my $part (@parts) {
325 my ($sub_start, $sub_end) = @
$part;
326 $part_data = get_list
(\
@bugs, $sub_start, $sub_end, $keyname);
327 push(@part_list, $part_data);
330 # Do we want to see inactive bugs?
332 $vars->{'null'} = get_inactive_bugs
(\
@bugs, $start_date, $end_date);
334 $vars->{'null'} = {};
337 # Convert bug IDs to bug objects.
338 @bugs = map {new Bugzilla
::Bug
($_)} @bugs;
340 $vars->{'part_list'} = \
@part_list;
341 $vars->{'parts'} = \
@parts;
342 # We pass the list of bugs as a hashref.
343 $vars->{'bugs'} = {map { $_->id => $_ } @bugs};
345 elsif ($cgi->cookie("time-summary-dates")) {
346 ($start_date, $end_date) = split ";", $cgi->cookie('time-summary-dates');
349 $vars->{'ids'} = \
@ids;
350 $vars->{'start_date'} = $start_date;
351 $vars->{'end_date'} = $end_date;
352 $vars->{'group_by'} = $group_by;
353 $vars->{'monthly'} = $monthly;
354 $vars->{'detailed'} = $detailed;
355 $vars->{'inactive'} = $inactive;
356 $vars->{'do_report'} = $do_report;
357 $vars->{'do_depends'} = $do_depends;
359 my $format = $template->get_format("bug/summarize-time", undef, $ctype);
361 # Get the proper content-type
362 print $cgi->header(-type
=> $format->{'ctype'});
363 $template->process("$format->{'template'}", $vars)
364 || ThrowTemplateError
($template->error());