reports.cgi: code style

hinted-selects
Vitaliy Filippov 2014-10-08 18:39:09 +04:00
parent 7b1bb3726a
commit f97ee3614b
1 changed files with 85 additions and 70 deletions

View File

@ -1,6 +1,4 @@
#!/usr/bin/perl -wT #!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public # The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file # License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of # except in compliance with the License. You may obtain a copy of
@ -50,55 +48,56 @@ use Digest::MD5 qw(md5_hex);
# If we're using bug groups for products, we should apply those restrictions # If we're using bug groups for products, we should apply those restrictions
# to viewing reports, as well. Time to check the login in that case. # to viewing reports, as well. Time to check the login in that case.
my $user = Bugzilla->login(); my $user = Bugzilla->login;
my $cgi = Bugzilla->cgi; my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template; my $template = Bugzilla->template;
my $vars = {}; my $vars = {};
if (!Bugzilla->feature('old_charts')) { if (!Bugzilla->feature('old_charts'))
{
ThrowCodeError('feature_disabled', { feature => 'old_charts' }); ThrowCodeError('feature_disabled', { feature => 'old_charts' });
} }
my $dir = bz_locations()->{'datadir'} . "/mining"; my $dir = bz_locations()->{datadir} . "/mining";
my $graph_dir = bz_locations()->{'graphsdir'}; my $graph_dir = bz_locations()->{graphsdir};
my $graph_url = basename($graph_dir); my $graph_url = basename($graph_dir);
my $product_name = $cgi->param('product') || ''; my $product_name = $cgi->param('product') || '';
Bugzilla->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
if (!$product_name) { if (!$product_name)
{
# Can we do bug charts? # Can we do bug charts?
(-d $dir && -d $graph_dir) (-d $dir && -d $graph_dir) || ThrowCodeError('chart_dir_nonexistent', { dir => $dir, graph_dir => $graph_dir });
|| ThrowCodeError('chart_dir_nonexistent',
{dir => $dir, graph_dir => $graph_dir});
my %default_sel = map { $_ => 1 } grep { $_->is_open } Bugzilla::Status->get_all; my %default_sel = map { $_ => 1 } grep { $_->is_open } Bugzilla::Status->get_all;
my @datasets; my @datasets;
my @data = get_data($dir); my @data = get_data($dir);
foreach my $dataset (@data)
foreach my $dataset (@data) { {
my $datasets = {}; my $datasets = {};
$datasets->{'value'} = $dataset; $datasets->{value} = $dataset;
$datasets->{'selected'} = $default_sel{$dataset} ? 1 : 0; $datasets->{selected} = $default_sel{$dataset} ? 1 : 0;
push(@datasets, $datasets); push @datasets, $datasets;
} }
# We only want those products that the user has permissions for. # We only want those products that the user has permissions for.
my @myproducts = ('-All-'); my @myproducts = ('-All-');
# Extract product names from objects and add them to the list. # Extract product names from objects and add them to the list.
push( @myproducts, map { $_->name } @{$user->get_selectable_products} ); push @myproducts, map { $_->name } @{$user->get_selectable_products};
$vars->{'datasets'} = \@datasets; $vars->{datasets} = \@datasets;
$vars->{'products'} = \@myproducts; $vars->{products} = \@myproducts;
} }
else { else
{
# For security and correctness, validate the value of the "product" form variable. # For security and correctness, validate the value of the "product" form variable.
# Valid values are those products for which the user has permissions which appear # Valid values are those products for which the user has permissions which appear
# in the "product" drop-down menu on the report generation form. # in the "product" drop-down menu on the report generation form.
my ($product) = grep { $_->name eq $product_name } @{$user->get_selectable_products}; my ($product) = grep { $_->name eq $product_name } @{$user->get_selectable_products};
($product || $product_name eq '-All-') $product || $product_name eq '-All-'
|| ThrowUserError('invalid_product_name', {product => $product_name}); || ThrowUserError('invalid_product_name', { product => $product_name });
# Product names can change over time. Their ID cannot; so use the ID # Product names can change over time. Their ID cannot; so use the ID
# to generate the filename. # to generate the filename.
@ -106,51 +105,56 @@ else {
# Make sure there is something to plot. # Make sure there is something to plot.
my @datasets = $cgi->param('datasets'); my @datasets = $cgi->param('datasets');
scalar(@datasets) || ThrowUserError('missing_datasets'); @datasets || ThrowUserError('missing_datasets');
if (grep { $_ !~ /^[A-Za-z0-9:_-]+$/ } @datasets) { if (grep { $_ !~ /^[A-Za-z0-9:_-]+$/ } @datasets)
ThrowUserError('invalid_datasets', {'datasets' => \@datasets}); {
ThrowUserError('invalid_datasets', { datasets => \@datasets });
} }
# Filenames must not be guessable as they can point to products # Filenames must not be guessable as they can point to products
# you are not allowed to see. Also, different projects can have # you are not allowed to see. Also, different projects can have
# the same product names. # the same product names.
my $key = Bugzilla->localconfig->{'site_wide_secret'}; my $key = Bugzilla->localconfig->{site_wide_secret};
my $project = bz_locations()->{'project'} || ''; my $project = bz_locations()->{project} || '';
my $image_file = join(':', ($key, $project, $prod_id, @datasets)); my $image_file = join(':', $key, $project, $prod_id, @datasets);
# Wide characters cause md5_hex() to die. # Wide characters cause md5_hex() to die.
if (Bugzilla->params->{'utf8'}) { if (Bugzilla->params->{utf8})
{
utf8::encode($image_file) if utf8::is_utf8($image_file); utf8::encode($image_file) if utf8::is_utf8($image_file);
} }
my $type = chart_image_type(); my $type = chart_image_type();
$image_file = md5_hex($image_file) . ".$type"; $image_file = md5_hex($image_file) . ".$type";
trick_taint($image_file); trick_taint($image_file);
if (! -e "$graph_dir/$image_file") { if (!-e "$graph_dir/$image_file")
{
generate_chart($dir, "$graph_dir/$image_file", $type, $product, \@datasets); generate_chart($dir, "$graph_dir/$image_file", $type, $product, \@datasets);
} }
$vars->{'url_image'} = "$graph_url/$image_file"; $vars->{url_image} = "$graph_url/$image_file";
$cgi->send_header(-Content_Disposition=>'inline; filename=bugzilla_report.html'); $cgi->send_header(-Content_Disposition => 'inline; filename=bugzilla_report.html');
} }
$template->process('reports/old-charts.html.tmpl', $vars) $template->process('reports/old-charts.html.tmpl', $vars)
|| ThrowTemplateError($template->error()); || ThrowTemplateError($template->error());
exit;
##################### #####################
# Subroutines # # Subroutines #
##################### #####################
sub get_data { sub get_data
{
my $dir = shift; my $dir = shift;
my @datasets; my @datasets;
open(DATA, '<', "$dir/-All-") open(DATA, '<', "$dir/-All-")
|| ThrowCodeError('chart_file_open_fail', {filename => "$dir/-All-"}); || ThrowCodeError('chart_file_open_fail', { filename => "$dir/-All-" });
while (<DATA>)
while (<DATA>) { {
if (/^# fields?: (.+)\s*$/) { if (/^# fields?: (.+)\s*$/)
{
@datasets = grep ! /date/i, (split /\|/, $1); @datasets = grep ! /date/i, (split /\|/, $1);
last; last;
} }
@ -159,24 +163,27 @@ sub get_data {
return @datasets; return @datasets;
} }
sub chart_image_type { sub chart_image_type
{
# what chart type should we be generating? # what chart type should we be generating?
my $testimg = Chart::Lines->new(2,2); my $testimg = Chart::Lines->new(2,2);
my $type = $testimg->can('gif') ? "gif" : "png"; my $type = $testimg->can('gif') ? "gif" : "png";
undef $testimg; undef $testimg;
return $type; return $type;
} }
sub generate_chart { sub generate_chart
{
my ($dir, $image_file, $type, $product, $datasets) = @_; my ($dir, $image_file, $type, $product, $datasets) = @_;
$product = $product ? $product->name : '-All-'; $product = $product ? $product->name : '-All-';
my $data_file = $product; my $data_file = $product;
$data_file =~ s/\//-/gs; $data_file =~ s/\//-/gs;
$data_file = $dir . '/' . $data_file; $data_file = $dir . '/' . $data_file;
if (! open FILE, $data_file) { if (!open FILE, $data_file)
if ($product eq '-All-') { {
if ($product eq '-All-')
{
$product = ''; $product = '';
} }
ThrowCodeError('chart_data_not_generated', {'product' => $product}); ThrowCodeError('chart_data_not_generated', {'product' => $product});
@ -187,22 +194,26 @@ sub generate_chart {
my %datasets = map { $_ => 1 } @$datasets; my %datasets = map { $_ => 1 } @$datasets;
my %data = (); my %data = ();
while (<FILE>) { while (<FILE>)
{
chomp; chomp;
next unless $_; next unless $_;
if (/^#/) { if (/^#/)
if (/^# fields?: (.*)\s*$/) { {
if (/^# fields?: (.*)\s*$/)
{
@fields = split /\||\r/, $1; @fields = split /\||\r/, $1;
$data{$_} ||= [] foreach @fields; $data{$_} ||= [] foreach @fields;
unless ($fields[0] =~ /date/i) { unless ($fields[0] =~ /date/i)
{
ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file}); ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file});
} }
push @labels, grep($datasets{$_}, @fields); push @labels, grep($datasets{$_}, @fields);
} }
next; next;
} }
unless (@fields)
unless (@fields) { {
ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file}); ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file});
} }
@ -211,15 +222,18 @@ sub generate_chart {
my ($yy, $mm, $dd) = $date =~ /^\d{2}(\d{2})(\d{2})(\d{2})$/; my ($yy, $mm, $dd) = $date =~ /^\d{2}(\d{2})(\d{2})(\d{2})$/;
push @{$data{DATE}}, "$mm/$dd/$yy"; push @{$data{DATE}}, "$mm/$dd/$yy";
for my $i (1 .. $#fields) { for my $i (1 .. $#fields)
{
my $field = $fields[$i]; my $field = $fields[$i];
if (! defined $line[$i] or $line[$i] eq '') { if (!defined $line[$i] || $line[$i] eq '')
{
# no data point given, don't plot (this will probably # no data point given, don't plot (this will probably
# generate loads of Chart::Base warnings, but that's not # generate loads of Chart::Base warnings, but that's not
# our fault.) # our fault.)
push @{$data{$field}}, undef; push @{$data{$field}}, undef;
} }
else { else
{
push @{$data{$field}}, $line[$i]; push @{$data{$field}}, $line[$i];
} }
} }
@ -229,34 +243,35 @@ sub generate_chart {
close FILE; close FILE;
if (! @{$data{DATE}}) { if (!@{$data{DATE}})
{
ThrowUserError('insufficient_data_points'); ThrowUserError('insufficient_data_points');
} }
my $img = Chart::Lines->new (800, 600); my $img = Chart::Lines->new(800, 600);
my $i = 0; my $i = 0;
my $MAXTICKS = 20; # Try not to show any more x ticks than this. my $MAXTICKS = 20; # Try not to show any more x ticks than this.
my $skip = 1; my $skip = 1;
if (@{$data{DATE}} > $MAXTICKS) { if (@{$data{DATE}} > $MAXTICKS)
{
$skip = int((@{$data{DATE}} + $MAXTICKS - 1) / $MAXTICKS); $skip = int((@{$data{DATE}} + $MAXTICKS - 1) / $MAXTICKS);
} }
my %settings = my %settings = (
( "title" => "Status Counts for $product",
"title" => "Status Counts for $product", "x_label" => "Dates",
"x_label" => "Dates", "y_label" => "Bug Counts",
"y_label" => "Bug Counts", "legend_labels" => \@labels,
"legend_labels" => \@labels, "skip_x_ticks" => $skip,
"skip_x_ticks" => $skip, "y_grid_lines" => "true",
"y_grid_lines" => "true", "grey_background" => "false",
"grey_background" => "false", "colors" => {
"colors" => { # default dataset colours are too alike
# default dataset colours are too alike dataset4 => [0, 0, 0], # black
dataset4 => [0, 0, 0], # black },
}, );
);
$img->set (%settings); $img->set(%settings);
$img->$type($image_file, [ @data{('DATE', @labels)} ]); $img->$type($image_file, [ @data{('DATE', @labels)} ]);
} }