Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / summarize_time.cgi
blob5ebb7e1a70d96f80fe7b61658510ca25b3690b80
1 #!/usr/bin/env perl -wT
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>
18 # Frédéric Buclin <LpSolit@gmail.com>
20 use strict;
22 use lib qw(. lib);
24 use Date::Parse; # strptime
26 use Bugzilla;
27 use Bugzilla::Constants; # LOGIN_*
28 use Bugzilla::Bug; # EmitDependList
29 use Bugzilla::Util; # trim
30 use Bugzilla::Error;
33 # Date handling
36 sub date_adjust_down {
38 my ($year, $month, $day) = @_;
40 if ($day == 0) {
41 $month -= 1;
42 $day = 31;
43 # Proper day adjustment is done later.
45 if ($month == 0) {
46 $year -= 1;
47 $month = 12;
51 if (($month == 2) && ($day > 28)) {
52 if ($year % 4 == 0 && $year % 100 != 0) {
53 $day = 29;
54 } else {
55 $day = 28;
59 if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
60 ($day == 31) )
62 $day = 30;
64 return ($year, $month, $day);
67 sub date_adjust_up {
68 my ($year, $month, $day) = @_;
70 if ($day > 31) {
71 $month += 1;
72 $day = 1;
74 if ($month == 13) {
75 $month = 1;
76 $year += 1;
80 if ($month == 2 && $day > 28) {
81 if ($year % 4 != 0 || $year % 100 == 0 || $day > 29) {
82 $month = 3;
83 $day = 1;
87 if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
88 ($day == 31) )
90 $month += 1;
91 $day = 1;
94 return ($year, $month, $day);
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) = @_;
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);
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;
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;
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;
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;
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];
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];
147 return @months;
150 sub sqlize_dates {
151 my ($start_date, $end_date) = @_;
152 my $date_bits = "";
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;
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);
167 $date_bits .= " AND longdescs.bug_when < ?";
168 push @date_values, $end_date;
170 return ($date_bits, \@date_values);
173 # Return all blockers of the current bug, recursively.
174 sub get_blocker_ids {
175 my ($bug_id, $unique) = @_;
176 $unique ||= {$bug_id => 1};
177 my $deps = Bugzilla::Bug::EmitDependList("blocked", "dependson", $bug_id);
178 my @unseen = grep { !$unique->{$_}++ } @$deps;
179 foreach $bug_id (@unseen) {
180 get_blocker_ids($bug_id, $unique);
182 return keys %$unique;
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) = @_;
191 my $dbh = Bugzilla->dbh;
193 my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
194 my $buglist = join(", ", @$bugids);
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
201 ON longdescs.who = profiles.userid
202 INNER JOIN bugs
203 ON bugs.bug_id = longdescs.bug_id
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);
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;
214 # Return bugs which had no activity (a.k.a work_time = 0) during the given time range.
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);
219 my $buglist = join(", ", @$bugids);
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);
232 return $bugs;
236 # Template code starts here
239 Bugzilla->login(LOGIN_REQUIRED);
241 my $cgi = Bugzilla->cgi;
242 my $user = Bugzilla->user;
243 my $template = Bugzilla->template;
244 my $vars = {};
246 Bugzilla->switch_to_shadow_db();
248 $user->in_group(Bugzilla->params->{"timetrackinggroup"})
249 || ThrowUserError("auth_failure", {group => "time-tracking",
250 action => "access",
251 object => "timetracking_summaries"});
253 my @ids = split(",", $cgi->param('id'));
254 map { ValidateBugID($_) } @ids;
255 scalar(@ids) || ThrowUserError('no_bugs_chosen', {action => 'view'});
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"));
265 my ($start_date, $end_date);
266 if ($do_report) {
267 my @bugs = @ids;
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"});
275 @bugs = get_blocker_ids($bugs[0]);
276 @bugs = grep { $user->can_see_bug($_) } @bugs;
279 $start_date = trim $cgi->param('start_date');
280 $end_date = trim $cgi->param('end_date');
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);
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'});
294 # Store dates in a session cookie so re-visiting the page
295 # for other bugs keeps them around.
296 $cgi->send_cookie(-name => 'time-summary-dates',
297 -value => join ";", ($start_date, $end_date));
299 my (@parts, $part_data, @part_list);
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,
315 $end_date || format_time(scalar localtime(time()), '%Y-%m-%d'));
316 } else {
317 @parts = ([$start_date, $end_date]);
320 # For each of the separate divisions, grab the relevant data.
321 my $keyname = ($group_by eq 'owner') ? 'login_name' : 'bug_id';
322 foreach my $part (@parts) {
323 my ($sub_start, $sub_end) = @$part;
324 $part_data = get_list(\@bugs, $sub_start, $sub_end, $keyname);
325 push(@part_list, $part_data);
328 # Do we want to see inactive bugs?
329 if ($inactive) {
330 $vars->{'null'} = get_inactive_bugs(\@bugs, $start_date, $end_date);
331 } else {
332 $vars->{'null'} = {};
335 # Convert bug IDs to bug objects.
336 @bugs = map {new Bugzilla::Bug($_)} @bugs;
338 $vars->{'part_list'} = \@part_list;
339 $vars->{'parts'} = \@parts;
340 # We pass the list of bugs as a hashref.
341 $vars->{'bugs'} = {map { $_->id => $_ } @bugs};
343 elsif ($cgi->cookie("time-summary-dates")) {
344 ($start_date, $end_date) = split ";", $cgi->cookie('time-summary-dates');
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;
357 my $format = $template->get_format("bug/summarize-time", undef, $ctype);
359 # Get the proper content-type
360 print $cgi->header(-type=> $format->{'ctype'});
361 $template->process("$format->{'template'}", $vars)
362 || ThrowTemplateError($template->error());