From f1f2319b679fa01ec9060691491e5ed9039d2924 Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Fri, 4 Aug 2006 00:54:04 +0000 Subject: Bug 304889: Templatize reports.cgi - Patch by Frédéric Buclin r=mkanat a=myk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reports.cgi | 214 +++++++++++++++++++++--------------------------------------- 1 file changed, 74 insertions(+), 140 deletions(-) (limited to 'reports.cgi') diff --git a/reports.cgi b/reports.cgi index 0f5842d0b..2c9f8facc 100755 --- a/reports.cgi +++ b/reports.cgi @@ -30,8 +30,10 @@ # daily stats file, so now works independently of collectstats.pl # version # Added image caching by date and datasets -# Myk Melez : # Implemented form field validation and reorganized code. +# Frédéric Buclin : +# Templatization. use strict; @@ -47,9 +49,9 @@ $@ && ThrowCodeError("gd_not_installed"); eval "use Chart::Lines"; $@ && ThrowCodeError("chart_lines_not_installed"); -local our $dir = bz_locations()->{'datadir'} . "/mining"; -local our $graph_url = 'graphs'; -local our $graph_dir = bz_locations()->{'libpath'} . '/' .$graph_url; +my $dir = bz_locations()->{'datadir'} . "/mining"; +my $graph_url = 'graphs'; +my $graph_dir = bz_locations()->{'libpath'} . '/' .$graph_url; # 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. @@ -59,7 +61,7 @@ Bugzilla->switch_to_shadow_db(); my $cgi = Bugzilla->cgi; my $template = Bugzilla->template; -local our $vars = {}; +my $vars = {}; # We only want those products that the user has permissions for. my @myproducts; @@ -68,11 +70,33 @@ push( @myproducts, "-All-"); push( @myproducts, map { $_->name } @{$user->get_selectable_products} ); if (! defined $cgi->param('product')) { + # Can we do bug charts? + (-d $dir && -d $graph_dir) + || ThrowCodeError('chart_dir_nonexistent', + {dir => $dir, graph_dir => $graph_dir}); + + my %default_sel = map { $_ => 1 } qw/UNCONFIRMED NEW ASSIGNED REOPENED/; + + my @datasets; + my @data = get_data($dir); + + foreach my $dataset (@data) { + my $datasets = {}; + $datasets->{'value'} = $dataset; + $datasets->{'selected'} = $default_sel{$dataset} ? 1 : 0; + push(@datasets, $datasets); + } + + $vars->{'datasets'} = \@datasets; + $vars->{'products'} = \@myproducts; - choose_product(@myproducts); - $template->put_footer(); + print $cgi->header(); -} else { + $template->process('reports/old-charts.html.tmpl', $vars) + || ThrowTemplateError($template->error()); + exit; +} +else { my $product = $cgi->param('product'); # For security and correctness, validate the value of the "product" form variable. @@ -85,96 +109,49 @@ if (! defined $cgi->param('product')) { # This means that is OK to detaint trick_taint($product); - print $cgi->header(-Content_Disposition=>'inline; filename=bugzilla_report.html'); + defined($cgi->param('datasets')) || ThrowUserError('missing_datasets'); - $template->put_header("Bug Charts"); - $vars->{'header_done'} = 1; + my $datasets = join('', $cgi->param('datasets')); - show_chart($product); + my $type = chart_image_type(); + my $data_file = daily_stats_filename($product); + my $image_file = chart_image_name($data_file, $type, $datasets); + my $url_image = correct_urlbase() . "$graph_url/$image_file"; - $template->put_footer(); -} + if (! -e "$graph_dir/$image_file") { + generate_chart("$dir/$data_file", "$graph_dir/$image_file", $type, + $product, $datasets); + } + $vars->{'url_image'} = $url_image; -################################## -# user came in with no form data # -################################## + print $cgi->header(-Content_Disposition=>'inline; filename=bugzilla_report.html'); -sub choose_product { - my @myproducts = (@_); - my $cgi = Bugzilla->cgi; - my $template = Bugzilla->template; + $template->process('reports/old-charts.html.tmpl', $vars) + || ThrowTemplateError($template->error()); + exit; +} +##################### +# Subroutines # +##################### + +sub get_data { + my $dir = shift; + + my @datasets; my $datafile = daily_stats_filename('-All-'); + open(DATA, '<', "$dir/$datafile") + || ThrowCodeError('chart_file_open_fail', {filename => "$dir/$datafile"}); - # Can we do bug charts? - (-d $dir && -d $graph_dir) - || ThrowCodeError("chart_dir_nonexistent", - {dir => $dir, graph_dir => $graph_dir}); - - open(DATA, "$dir/$datafile") - || ThrowCodeError("chart_file_open_fail", {filename => "$dir/$datafile"}); - - print $cgi->header(); - $template->put_header("Bug Charts"); - $vars->{'header_done'} = 1; - - print < -

Welcome to the Bugzilla Charting Kitchen

-
- - - - - - - - - - - - -
Product: -
Chart datasets: -
- -
- -
-

-FIN + while () { + if (/^# fields?: (.+)\s*$/) { + @datasets = grep ! /date/i, (split /\|/, $1); + last; + } + } + close(DATA); + return @datasets; } sub daily_stats_filename { @@ -183,37 +160,6 @@ sub daily_stats_filename { return $prodname; } -sub show_chart { - my ($product) = @_; - my $cgi = Bugzilla->cgi; - - if (! defined $cgi->param('datasets')) { - ThrowUserError("missing_datasets", $vars); - } - my $datasets = join('', $cgi->param('datasets')); - - print < -FIN - - my $type = chart_image_type(); - my $data_file = daily_stats_filename($product); - my $image_file = chart_image_name($data_file, $type, $datasets); - my $url_image = correct_urlbase() . "$graph_url/" - . url_quote($image_file); - - if (! -e "$graph_dir/$image_file") { - generate_chart("$dir/$data_file", "$graph_dir/$image_file", $type, - $product, $datasets); - } - - print < -
-
-FIN -} - sub chart_image_type { # what chart type should we be generating? my $testimg = Chart::Lines->new(2,2); @@ -234,8 +180,7 @@ sub chart_image_name { # numbers, underscores and hyphens. if ($datasets !~ m/^[A-Za-z0-9:_-]+$/) { - $vars->{'datasets'} = $datasets; - ThrowUserError('invalid_datasets', $vars); + ThrowUserError('invalid_datasets', {'datasets' => $datasets}); } # Since we pass the tests, consider it OK @@ -248,23 +193,14 @@ sub chart_image_name { return "${data_file}_${id}.$type"; } -sub day_of_year { - my ($mday, $month, $year) = (localtime())[3 .. 5]; - $month += 1; - $year += 1900; - my $date = sprintf "%02d%02d%04d", $mday, $month, $year; -} - sub generate_chart { my ($data_file, $image_file, $type, $product, $datasets) = @_; - + if (! open FILE, $data_file) { if ($product eq '-All-') { $product = ''; } - - $vars->{'product'} = $product; - ThrowCodeError("chart_data_not_generated", $vars); + ThrowCodeError('chart_data_not_generated', {'product' => $product}); } my @fields; @@ -279,8 +215,7 @@ sub generate_chart { if (/^# fields?: (.*)\s*$/) { @fields = split /\||\r/, $1; unless ($fields[0] =~ /date/i) { - $vars->{'file'} = $data_file; - ThrowCodeError("chart_datafile_corrupt", $vars); + ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file}); } push @labels, grep($datasets{$_}, @fields); } @@ -288,10 +223,9 @@ sub generate_chart { } unless (@fields) { - $vars->{'file'} = $data_file; - ThrowCodeError("chart_datafile_corrupt", $vars); + ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file}); } - + my @line = split /\|/; my $date = $line[0]; my ($yy, $mm, $dd) = $date =~ /^\d{2}(\d{2})(\d{2})(\d{2})$/; @@ -316,9 +250,9 @@ sub generate_chart { close FILE; if (! @{$data{DATE}}) { - ThrowUserError("insufficient_data_points", $vars); + ThrowUserError('insufficient_data_points'); } - + my $img = Chart::Lines->new (800, 600); my $i = 0; -- cgit v1.2.3-24-g4f1b