report.cgi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. # The Initial Developer of the Original Code is Netscape Communications
  17. # Corporation. Portions created by Netscape are
  18. # Copyright (C) 1998 Netscape Communications Corporation. All
  19. # Rights Reserved.
  20. #
  21. # Contributor(s): Gervase Markham <gerv@gerv.net>
  22. # <rdean@cambianetworks.com>
  23. use strict;
  24. use lib qw(. lib);
  25. use Bugzilla;
  26. use Bugzilla::Constants;
  27. use Bugzilla::Util;
  28. use Bugzilla::Error;
  29. use Bugzilla::Field;
  30. my $cgi = Bugzilla->cgi;
  31. my $template = Bugzilla->template;
  32. my $vars = {};
  33. my $buffer = $cgi->query_string();
  34. # Go straight back to query.cgi if we are adding a boolean chart.
  35. if (grep(/^cmd-/, $cgi->param())) {
  36. my $params = $cgi->canonicalise_query("format", "ctype");
  37. my $location = "query.cgi?format=" . $cgi->param('query_format') .
  38. ($params ? "&$params" : "");
  39. print $cgi->redirect($location);
  40. exit;
  41. }
  42. use Bugzilla::Search;
  43. Bugzilla->login();
  44. my $dbh = Bugzilla->switch_to_shadow_db();
  45. my $action = $cgi->param('action') || 'menu';
  46. if ($action eq "menu") {
  47. # No need to do any searching in this case, so bail out early.
  48. print $cgi->header();
  49. $template->process("reports/menu.html.tmpl", $vars)
  50. || ThrowTemplateError($template->error());
  51. exit;
  52. }
  53. my $col_field = $cgi->param('x_axis_field') || '';
  54. my $row_field = $cgi->param('y_axis_field') || '';
  55. my $tbl_field = $cgi->param('z_axis_field') || '';
  56. if (!($col_field || $row_field || $tbl_field)) {
  57. ThrowUserError("no_axes_defined");
  58. }
  59. my $width = $cgi->param('width');
  60. my $height = $cgi->param('height');
  61. if (defined($width)) {
  62. (detaint_natural($width) && $width > 0)
  63. || ThrowCodeError("invalid_dimensions");
  64. $width <= 2000 || ThrowUserError("chart_too_large");
  65. }
  66. if (defined($height)) {
  67. (detaint_natural($height) && $height > 0)
  68. || ThrowCodeError("invalid_dimensions");
  69. $height <= 2000 || ThrowUserError("chart_too_large");
  70. }
  71. # These shenanigans are necessary to make sure that both vertical and
  72. # horizontal 1D tables convert to the correct dimension when you ask to
  73. # display them as some sort of chart.
  74. if (defined $cgi->param('format') && $cgi->param('format') eq "table") {
  75. if ($col_field && !$row_field) {
  76. # 1D *tables* should be displayed vertically (with a row_field only)
  77. $row_field = $col_field;
  78. $col_field = '';
  79. }
  80. }
  81. else {
  82. if ($row_field && !$col_field) {
  83. # 1D *charts* should be displayed horizontally (with an col_field only)
  84. $col_field = $row_field;
  85. $row_field = '';
  86. }
  87. }
  88. my %columns;
  89. $columns{'bug_severity'} = "bugs.bug_severity";
  90. $columns{'priority'} = "bugs.priority";
  91. $columns{'rep_platform'} = "bugs.rep_platform";
  92. $columns{'assigned_to'} = "map_assigned_to.login_name";
  93. $columns{'reporter'} = "map_reporter.login_name";
  94. $columns{'qa_contact'} = "map_qa_contact.login_name";
  95. $columns{'bug_status'} = "bugs.bug_status";
  96. $columns{'resolution'} = "bugs.resolution";
  97. $columns{'component'} = "map_components.name";
  98. $columns{'product'} = "map_products.name";
  99. $columns{'classification'} = "map_classifications.name";
  100. $columns{'version'} = "bugs.version";
  101. $columns{'op_sys'} = "bugs.op_sys";
  102. $columns{'votes'} = "bugs.votes";
  103. $columns{'keywords'} = "bugs.keywords";
  104. $columns{'target_milestone'} = "bugs.target_milestone";
  105. # One which means "nothing". Any number would do, really. It just gets SELECTed
  106. # so that we always select 3 items in the query.
  107. $columns{''} = "42217354";
  108. # Validate the values in the axis fields or throw an error.
  109. !$row_field
  110. || ($columns{$row_field} && trick_taint($row_field))
  111. || ThrowCodeError("report_axis_invalid", {fld => "x", val => $row_field});
  112. !$col_field
  113. || ($columns{$col_field} && trick_taint($col_field))
  114. || ThrowCodeError("report_axis_invalid", {fld => "y", val => $col_field});
  115. !$tbl_field
  116. || ($columns{$tbl_field} && trick_taint($tbl_field))
  117. || ThrowCodeError("report_axis_invalid", {fld => "z", val => $tbl_field});
  118. my @axis_fields = ($row_field, $col_field, $tbl_field);
  119. my @selectnames = map($columns{$_}, @axis_fields);
  120. # Clone the params, so that Bugzilla::Search can modify them
  121. my $params = new Bugzilla::CGI($cgi);
  122. my $search = new Bugzilla::Search('fields' => \@selectnames,
  123. 'params' => $params);
  124. my $query = $search->getSQL();
  125. $::SIG{TERM} = 'DEFAULT';
  126. $::SIG{PIPE} = 'DEFAULT';
  127. my $results = $dbh->selectall_arrayref($query);
  128. # We have a hash of hashes for the data itself, and a hash to hold the
  129. # row/col/table names.
  130. my %data;
  131. my %names;
  132. # Read the bug data and count the bugs for each possible value of row, column
  133. # and table.
  134. #
  135. # We detect a numerical field, and sort appropriately, if all the values are
  136. # numeric.
  137. my $col_isnumeric = 1;
  138. my $row_isnumeric = 1;
  139. my $tbl_isnumeric = 1;
  140. foreach my $result (@$results) {
  141. my ($row, $col, $tbl) = @$result;
  142. # handle empty dimension member names
  143. $row = ' ' if ($row eq '');
  144. $col = ' ' if ($col eq '');
  145. $tbl = ' ' if ($tbl eq '');
  146. $row = "" if ($row eq $columns{''});
  147. $col = "" if ($col eq $columns{''});
  148. $tbl = "" if ($tbl eq $columns{''});
  149. # account for the fact that names may start with '_' or '.'. Change this
  150. # so the template doesn't hide hash elements with those keys
  151. $row =~ s/^([._])/ $1/;
  152. $col =~ s/^([._])/ $1/;
  153. $tbl =~ s/^([._])/ $1/;
  154. $data{$tbl}{$col}{$row}++;
  155. $names{"col"}{$col}++;
  156. $names{"row"}{$row}++;
  157. $names{"tbl"}{$tbl}++;
  158. $col_isnumeric &&= ($col =~ /^-?\d+(\.\d+)?$/o);
  159. $row_isnumeric &&= ($row =~ /^-?\d+(\.\d+)?$/o);
  160. $tbl_isnumeric &&= ($tbl =~ /^-?\d+(\.\d+)?$/o);
  161. }
  162. my @col_names = @{get_names($names{"col"}, $col_isnumeric, $col_field)};
  163. my @row_names = @{get_names($names{"row"}, $row_isnumeric, $row_field)};
  164. my @tbl_names = @{get_names($names{"tbl"}, $tbl_isnumeric, $tbl_field)};
  165. # The GD::Graph package requires a particular format of data, so once we've
  166. # gathered everything into the hashes and made sure we know the size of the
  167. # data, we reformat it into an array of arrays of arrays of data.
  168. push(@tbl_names, "-total-") if (scalar(@tbl_names) > 1);
  169. my @image_data;
  170. foreach my $tbl (@tbl_names) {
  171. my @tbl_data;
  172. push(@tbl_data, \@col_names);
  173. foreach my $row (@row_names) {
  174. my @col_data;
  175. foreach my $col (@col_names) {
  176. $data{$tbl}{$col}{$row} = $data{$tbl}{$col}{$row} || 0;
  177. push(@col_data, $data{$tbl}{$col}{$row});
  178. if ($tbl ne "-total-") {
  179. # This is a bit sneaky. We spend every loop except the last
  180. # building up the -total- data, and then last time round,
  181. # we process it as another tbl, and push() the total values
  182. # into the image_data array.
  183. $data{"-total-"}{$col}{$row} += $data{$tbl}{$col}{$row};
  184. }
  185. }
  186. push(@tbl_data, \@col_data);
  187. }
  188. unshift(@image_data, \@tbl_data);
  189. }
  190. $vars->{'col_field'} = $col_field;
  191. $vars->{'row_field'} = $row_field;
  192. $vars->{'tbl_field'} = $tbl_field;
  193. $vars->{'time'} = time();
  194. $vars->{'col_names'} = \@col_names;
  195. $vars->{'row_names'} = \@row_names;
  196. $vars->{'tbl_names'} = \@tbl_names;
  197. # Below a certain width, we don't see any bars, so there needs to be a minimum.
  198. if ($width && $cgi->param('format') eq "bar") {
  199. my $min_width = (scalar(@col_names) || 1) * 20;
  200. if (!$cgi->param('cumulate')) {
  201. $min_width *= (scalar(@row_names) || 1);
  202. }
  203. $vars->{'min_width'} = $min_width;
  204. }
  205. $vars->{'width'} = $width if $width;
  206. $vars->{'height'} = $height if $height;
  207. $vars->{'query'} = $query;
  208. if ($cgi->param('debug')
  209. && Bugzilla->params->{debug_group}
  210. && Bugzilla->user->in_group(Bugzilla->params->{debug_group})
  211. ) {
  212. $vars->{'debug'} = 1;
  213. }
  214. my $formatparam = $cgi->param('format');
  215. if ($action eq "wrap") {
  216. # So which template are we using? If action is "wrap", we will be using
  217. # no format (it gets passed through to be the format of the actual data),
  218. # and either report.csv.tmpl (CSV), or report.html.tmpl (everything else).
  219. # report.html.tmpl produces an HTML framework for either tables of HTML
  220. # data, or images generated by calling report.cgi again with action as
  221. # "plot".
  222. $formatparam =~ s/[^a-zA-Z\-]//g;
  223. trick_taint($formatparam);
  224. $vars->{'format'} = $formatparam;
  225. $formatparam = '';
  226. # We need to keep track of the defined restrictions on each of the
  227. # axes, because buglistbase, below, throws them away. Without this, we
  228. # get buglistlinks wrong if there is a restriction on an axis field.
  229. $vars->{'col_vals'} = join("&", $buffer =~ /[&?]($col_field=[^&]+)/g);
  230. $vars->{'row_vals'} = join("&", $buffer =~ /[&?]($row_field=[^&]+)/g);
  231. $vars->{'tbl_vals'} = join("&", $buffer =~ /[&?]($tbl_field=[^&]+)/g);
  232. # We need a number of different variants of the base URL for different
  233. # URLs in the HTML.
  234. $vars->{'buglistbase'} = $cgi->canonicalise_query(
  235. "x_axis_field", "y_axis_field", "z_axis_field",
  236. "ctype", "format", "query_format", @axis_fields);
  237. $vars->{'imagebase'} = $cgi->canonicalise_query(
  238. $tbl_field, "action", "ctype", "format", "width", "height");
  239. $vars->{'switchbase'} = $cgi->canonicalise_query(
  240. "query_format", "action", "ctype", "format", "width", "height");
  241. $vars->{'data'} = \%data;
  242. }
  243. elsif ($action eq "plot") {
  244. # If action is "plot", we will be using a format as normal (pie, bar etc.)
  245. # and a ctype as normal (currently only png.)
  246. $vars->{'cumulate'} = $cgi->param('cumulate') ? 1 : 0;
  247. $vars->{'x_labels_vertical'} = $cgi->param('x_labels_vertical') ? 1 : 0;
  248. $vars->{'data'} = \@image_data;
  249. }
  250. else {
  251. ThrowCodeError("unknown_action", {action => $cgi->param('action')});
  252. }
  253. my $format = $template->get_format("reports/report", $formatparam,
  254. scalar($cgi->param('ctype')));
  255. # If we get a template or CGI error, it comes out as HTML, which isn't valid
  256. # PNG data, and the browser just displays a "corrupt PNG" message. So, you can
  257. # set debug=1 to always get an HTML content-type, and view the error.
  258. $format->{'ctype'} = "text/html" if $cgi->param('debug');
  259. my @time = localtime(time());
  260. my $date = sprintf "%04d-%02d-%02d", 1900+$time[5],$time[4]+1,$time[3];
  261. my $filename = "report-$date.$format->{extension}";
  262. print $cgi->header(-type => $format->{'ctype'},
  263. -content_disposition => "inline; filename=$filename");
  264. # Problems with this CGI are often due to malformed data. Setting debug=1
  265. # prints out both data structures.
  266. if ($cgi->param('debug')) {
  267. require Data::Dumper;
  268. print "<pre>data hash:\n";
  269. print Data::Dumper::Dumper(%data) . "\n\n";
  270. print "data array:\n";
  271. print Data::Dumper::Dumper(@image_data) . "\n\n</pre>";
  272. }
  273. # All formats point to the same section of the documentation.
  274. $vars->{'doc_section'} = 'reporting.html#reports';
  275. disable_utf8() if ($format->{'ctype'} =~ /^image\//);
  276. $template->process("$format->{'template'}", $vars)
  277. || ThrowTemplateError($template->error());
  278. exit;
  279. sub get_names {
  280. my ($names, $isnumeric, $field) = @_;
  281. # These are all the fields we want to preserve the order of in reports.
  282. my %fields = ('priority' => get_legal_field_values('priority'),
  283. 'bug_severity' => get_legal_field_values('bug_severity'),
  284. 'rep_platform' => get_legal_field_values('rep_platform'),
  285. 'op_sys' => get_legal_field_values('op_sys'),
  286. 'bug_status' => get_legal_field_values('bug_status'),
  287. 'resolution' => [' ', @{get_legal_field_values('resolution')}]);
  288. my $field_list = $fields{$field};
  289. my @sorted;
  290. if ($field_list) {
  291. my @unsorted = keys %{$names};
  292. # Extract the used fields from the field_list, in the order they
  293. # appear in the field_list. This lets us keep e.g. severities in
  294. # the normal order.
  295. #
  296. # This is O(n^2) but it shouldn't matter for short lists.
  297. @sorted = map {lsearch(\@unsorted, $_) == -1 ? () : $_} @{$field_list};
  298. }
  299. elsif ($isnumeric) {
  300. # It's not a field we are preserving the order of, so sort it
  301. # numerically...
  302. sub numerically { $a <=> $b }
  303. @sorted = sort numerically keys(%{$names});
  304. } else {
  305. # ...or alphabetically, as appropriate.
  306. @sorted = sort(keys(%{$names}));
  307. }
  308. return \@sorted;
  309. }