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 # 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
21 # Contributor(s): Harrison Page <harrison@netscape.com>,
22 # Terry Weissman <terry@mozilla.org>,
23 # Dawn Endico <endico@mozilla.org>
24 # Bryce Nesbitt <bryce@nextbus.COM>,
25 # Joe Robins <jmrobins@tgix.com>,
26 # Gervase Markham <gerv@gerv.net> and Adam Spiers <adam@spiers.net>
27 # Added ability to chart any combination of resolutions/statuses.
28 # Derive the choice of resolutions/statuses from the -All- data file
29 # Removed hardcoded order of resolutions/statuses when reading from
30 # daily stats file, so now works independently of collectstats.pl
32 # Added image caching by date and datasets
33 # Myk Melez <myk@mozilla.org>:
34 # Implemented form field validation and reorganized code.
35 # Frédéric Buclin <LpSolit@gmail.com>:
43 use Bugzilla
::Constants
;
49 $@
&& ThrowCodeError
("gd_not_installed");
50 eval "use Chart::Lines";
51 $@
&& ThrowCodeError
("chart_lines_not_installed");
53 my $dir = bz_locations
()->{'datadir'} . "/mining";
54 my $graph_url = 'graphs';
55 my $graph_dir = bz_locations
()->{'libpath'} . '/' .$graph_url;
57 # If we're using bug groups for products, we should apply those restrictions
58 # to viewing reports, as well. Time to check the login in that case.
59 my $user = Bugzilla
->login();
61 Bugzilla
->switch_to_shadow_db();
63 my $cgi = Bugzilla
->cgi;
64 my $template = Bugzilla
->template;
67 # We only want those products that the user has permissions for.
69 push( @myproducts, "-All-");
70 # Extract product names from objects and add them to the list.
71 push( @myproducts, map { $_->name } @
{$user->get_selectable_products} );
73 if (! defined $cgi->param('product')) {
74 # Can we do bug charts?
75 (-d
$dir && -d
$graph_dir)
76 || ThrowCodeError
('chart_dir_nonexistent',
77 {dir
=> $dir, graph_dir
=> $graph_dir});
79 my %default_sel = map { $_ => 1 } BUG_STATE_OPEN
;
82 my @data = get_data
($dir);
84 foreach my $dataset (@data) {
86 $datasets->{'value'} = $dataset;
87 $datasets->{'selected'} = $default_sel{$dataset} ?
1 : 0;
88 push(@datasets, $datasets);
91 $vars->{'datasets'} = \
@datasets;
92 $vars->{'products'} = \
@myproducts;
96 $template->process('reports/old-charts.html.tmpl', $vars)
97 || ThrowTemplateError
($template->error());
101 my $product = $cgi->param('product');
103 # For security and correctness, validate the value of the "product" form variable.
104 # Valid values are those products for which the user has permissions which appear
105 # in the "product" drop-down menu on the report generation form.
106 grep($_ eq $product, @myproducts)
107 || ThrowUserError
("invalid_product_name", {product
=> $product});
109 # We've checked that the product exists, and that the user can see it
110 # This means that is OK to detaint
111 trick_taint
($product);
113 defined($cgi->param('datasets')) || ThrowUserError
('missing_datasets');
115 my $datasets = join('', $cgi->param('datasets'));
117 my $type = chart_image_type
();
118 my $data_file = daily_stats_filename
($product);
119 my $image_file = chart_image_name
($data_file, $type, $datasets);
120 my $url_image = correct_urlbase
() . "$graph_url/$image_file";
122 if (! -e
"$graph_dir/$image_file") {
123 generate_chart
("$dir/$data_file", "$graph_dir/$image_file", $type,
124 $product, $datasets);
127 $vars->{'url_image'} = $url_image;
129 print $cgi->header(-Content_Disposition
=>'inline; filename=bugzilla_report.html');
131 $template->process('reports/old-charts.html.tmpl', $vars)
132 || ThrowTemplateError
($template->error());
136 #####################
138 #####################
144 my $datafile = daily_stats_filename
('-All-');
145 open(DATA
, '<', "$dir/$datafile")
146 || ThrowCodeError
('chart_file_open_fail', {filename
=> "$dir/$datafile"});
149 if (/^# fields?: (.+)\s*$/) {
150 @datasets = grep ! /date/i, (split /\|/, $1);
158 sub daily_stats_filename
{
160 $prodname =~ s/\//-/gs
;
164 sub chart_image_type
{
165 # what chart type should we be generating?
166 my $testimg = Chart
::Lines
->new(2,2);
167 my $type = $testimg->can('gif') ?
"gif" : "png";
173 sub chart_image_name
{
174 my ($data_file, $type, $datasets) = @_;
176 # This routine generates a filename from the requested fields. The problem
177 # is that we have to check the safety of doing this. We can't just require
178 # that the fields exist, because what stats were collected could change
179 # over time (eg by changing the resolutions available)
180 # Instead, just require that each field name consists only of letters,
181 # numbers, underscores and hyphens.
183 if ($datasets !~ m/^[A-Za-z0-9:_-]+$/) {
184 ThrowUserError
('invalid_datasets', {'datasets' => $datasets});
187 # Since we pass the tests, consider it OK
188 trick_taint
($datasets);
190 # Cache charts by generating a unique filename based on what they
191 # show. Charts should be deleted by collectstats.pl nightly.
192 my $id = join ("_", split (":", $datasets));
194 return "${data_file}_${id}.$type";
198 my ($data_file, $image_file, $type, $product, $datasets) = @_;
200 if (! open FILE
, $data_file) {
201 if ($product eq '-All-') {
204 ThrowCodeError
('chart_data_not_generated', {'product' => $product});
208 my @labels = qw(DATE);
209 my %datasets = map { $_ => 1 } split /:/, $datasets;
216 if (/^# fields?: (.*)\s*$/) {
217 @fields = split /\||\r/, $1;
218 $data{$_} ||= [] foreach @fields;
219 unless ($fields[0] =~ /date/i) {
220 ThrowCodeError
('chart_datafile_corrupt', {'file' => $data_file});
222 push @labels, grep($datasets{$_}, @fields);
228 ThrowCodeError
('chart_datafile_corrupt', {'file' => $data_file});
231 my @line = split /\|/;
233 my ($yy, $mm, $dd) = $date =~ /^\d{2}(\d{2})(\d{2})(\d{2})$/;
234 push @
{$data{DATE
}}, "$mm/$dd/$yy";
236 for my $i (1 .. $#fields) {
237 my $field = $fields[$i];
238 if (! defined $line[$i] or $line[$i] eq '') {
239 # no data point given, don't plot (this will probably
240 # generate loads of Chart::Base warnings, but that's not
242 push @
{$data{$field}}, undef;
245 push @
{$data{$field}}, $line[$i];
254 if (! @
{$data{DATE
}}) {
255 ThrowUserError
('insufficient_data_points');
258 my $img = Chart
::Lines
->new (800, 600);
261 my $MAXTICKS = 20; # Try not to show any more x ticks than this.
263 if (@
{$data{DATE
}} > $MAXTICKS) {
264 $skip = int((@
{$data{DATE
}} + $MAXTICKS - 1) / $MAXTICKS);
269 "title" => "Status Counts for $product",
270 "x_label" => "Dates",
271 "y_label" => "Bug Counts",
272 "legend_labels" => \
@labels,
273 "skip_x_ticks" => $skip,
274 "y_grid_lines" => "true",
275 "grey_background" => "false",
277 # default dataset colours are too alike
278 dataset4
=> [0, 0, 0], # black
282 $img->set (%settings);
283 $img->$type($image_file, [ @data{('DATE', @labels)} ]);