]> git.ipfire.org Git - thirdparty/bugzilla.git/blob - summarize_time.cgi
add a new hook: template_after_create (#60)
[thirdparty/bugzilla.git] / summarize_time.cgi
1 #!/usr/bin/perl -T
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/.
5 #
6 # This Source Code Form is "Incompatible With Secondary Licenses", as
7 # defined by the Mozilla Public License, v. 2.0.
8
9 use 5.10.1;
10 use strict;
11 use warnings;
12
13 use lib qw(. lib);
14
15 use Date::Parse; # strptime
16
17 use Bugzilla;
18 use Bugzilla::Constants; # LOGIN_*
19 use Bugzilla::Bug; # EmitDependList
20 use Bugzilla::Util; # trim
21 use Bugzilla::Error;
22
23 #
24 # Date handling
25 #
26
27 sub date_adjust_down {
28
29 my ($year, $month, $day) = @_;
30
31 if ($day == 0) {
32 $month -= 1;
33 $day = 31;
34 # Proper day adjustment is done later.
35
36 if ($month == 0) {
37 $year -= 1;
38 $month = 12;
39 }
40 }
41
42 if (($month == 2) && ($day > 28)) {
43 if ($year % 4 == 0 && $year % 100 != 0) {
44 $day = 29;
45 } else {
46 $day = 28;
47 }
48 }
49
50 if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
51 ($day == 31) )
52 {
53 $day = 30;
54 }
55 return ($year, $month, $day);
56 }
57
58 sub date_adjust_up {
59 my ($year, $month, $day) = @_;
60
61 if ($day > 31) {
62 $month += 1;
63 $day = 1;
64
65 if ($month == 13) {
66 $month = 1;
67 $year += 1;
68 }
69 }
70
71 if ($month == 2 && $day > 28) {
72 if ($year % 4 != 0 || $year % 100 == 0 || $day > 29) {
73 $month = 3;
74 $day = 1;
75 }
76 }
77
78 if (($month == 4 || $month == 6 || $month == 9 || $month == 11) &&
79 ($day == 31) )
80 {
81 $month += 1;
82 $day = 1;
83 }
84
85 return ($year, $month, $day);
86 }
87
88 sub split_by_month {
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) = @_;
92
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);
96
97 # Find out how many months fit between the two dates so we know
98 # how many times we loop.
99 my $yd = $ey - $sy;
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.
102 if ($sd > $ed) {
103 $md -= 1;
104 }
105
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;
109 my $month = $sm + 1;
110
111 # Keep the original date, when the date will be changed in the adjust_date.
112 my $sd_tmp = $sd;
113 my $month_tmp = $month;
114 my $year_tmp = $year;
115
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);
121 $month += 1;
122 if ($month == 13) {
123 $month = 1;
124 $year += 1;
125 }
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];
130 }
131
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];
137
138 return @months;
139 }
140
141 sub sqlize_dates {
142 my ($start_date, $end_date) = @_;
143 my $date_bits = "";
144 my @date_values;
145 if ($start_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;
150 }
151 if ($end_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);
157
158 $date_bits .= " AND longdescs.bug_when < ?";
159 push @date_values, $end_date;
160 }
161 return ($date_bits, \@date_values);
162 }
163
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);
172 }
173 return keys %$unique;
174 }
175
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.
180 sub get_list {
181 my ($bugids, $start_date, $end_date, $keyname) = @_;
182 my $dbh = Bugzilla->dbh;
183
184 my ($date_bits, $date_values) = sqlize_dates($start_date, $end_date);
185 my $buglist = join(", ", @$bugids);
186
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
190 FROM longdescs
191 INNER JOIN profiles
192 ON longdescs.who = profiles.userid
193 INNER JOIN bugs
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);
198
199 my %list;
200 # What this loop does is to push data having the same key in an array.
201 push(@{$list{ $_->{$keyname} }}, $_) foreach @$data;
202 return \%list;
203 }
204
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);
211
212 my $bugs = $dbh->selectcol_arrayref(
213 "SELECT bug_id
214 FROM bugs
215 WHERE bugs.bug_id IN ($buglist)
216 AND NOT EXISTS (
217 SELECT 1
218 FROM longdescs
219 WHERE bugs.bug_id = longdescs.bug_id
220 AND work_time > 0 $date_bits)",
221 undef, @$date_values);
222
223 return $bugs;
224 }
225
226 # Return 1st day of the month of the earliest activity date for a given list of bugs.
227 sub get_earliest_activity_date {
228 my ($bugids) = @_;
229 my $dbh = Bugzilla->dbh;
230
231 my ($date) = $dbh->selectrow_array(
232 'SELECT ' . $dbh->sql_date_format('MIN(bug_when)', '%Y-%m-01')
233 . ' FROM longdescs
234 WHERE ' . $dbh->sql_in('bug_id', $bugids)
235 . ' AND work_time > 0');
236
237 return $date;
238 }
239
240 #
241 # Template code starts here
242 #
243
244 my $user = Bugzilla->login(LOGIN_REQUIRED);
245
246 my $cgi = Bugzilla->cgi;
247 my $template = Bugzilla->template;
248 my $vars = {};
249
250 Bugzilla->switch_to_shadow_db();
251
252 $user->is_timetracker
253 || ThrowUserError("auth_failure", {group => "time-tracking",
254 action => "access",
255 object => "timetracking_summaries"});
256
257 my @ids = split(",", $cgi->param('id') || '');
258 @ids = map { Bugzilla::Bug->check($_)->id } @ids;
259 scalar(@ids) || ThrowUserError('no_bugs_chosen', {action => 'summarize'});
260
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"));
268
269 my ($start_date, $end_date);
270 if ($do_report) {
271 my @bugs = @ids;
272
273 # Dependency mode requires a single bug and grabs dependents.
274 if ($do_depends) {
275 if (scalar(@bugs) != 1) {
276 ThrowCodeError("bad_arg", { argument=>"id",
277 function=>"summarize_time"});
278 }
279 @bugs = get_blocker_ids($bugs[0]);
280 @bugs = @{ $user->visible_bugs(\@bugs) };
281 }
282
283 $start_date = trim $cgi->param('start_date');
284 $end_date = trim $cgi->param('end_date');
285
286 foreach my $date ($start_date, $end_date) {
287 next unless $date;
288 validate_date($date)
289 || ThrowUserError('illegal_date', {date => $date, format => 'YYYY-MM-DD'});
290 }
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);
296 }
297
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));
302
303 my (@parts, $part_data, @part_list);
304
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.
307 if ($monthly) {
308 # Calculate the earliest activity date if the user doesn't
309 # specify a start date.
310 if (!$start_date) {
311 $start_date = get_earliest_activity_date(\@bugs);
312 }
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'));
318 } else {
319 @parts = ([$start_date, $end_date]);
320 }
321
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);
328 }
329
330 # Do we want to see inactive bugs?
331 if ($inactive) {
332 $vars->{'null'} = get_inactive_bugs(\@bugs, $start_date, $end_date);
333 } else {
334 $vars->{'null'} = {};
335 }
336
337 # Convert bug IDs to bug objects.
338 @bugs = map {new Bugzilla::Bug($_)} @bugs;
339
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};
344 }
345 elsif ($cgi->cookie("time-summary-dates")) {
346 ($start_date, $end_date) = split ";", $cgi->cookie('time-summary-dates');
347 }
348
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;
358
359 my $format = $template->get_format("bug/summarize-time", undef, $ctype);
360
361 # Get the proper content-type
362 print $cgi->header(-type=> $format->{'ctype'});
363 $template->process("$format->{'template'}", $vars)
364 || ThrowTemplateError($template->error());