summarize_time.cgi 12 KB

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