reports.cgi 6.75 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
8 9

use strict;
10

11
use lib qw(. lib);
12

13 14 15 16
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Util;
use Bugzilla::Error;
17
use Bugzilla::Status;
18

19
use File::Basename;
20
use Digest::SHA qw(hmac_sha256_base64);
21

22 23 24
# 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.
my $user = Bugzilla->login();
25 26 27
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
my $vars = {};
28 29

if (!Bugzilla->feature('old_charts')) {
30
    ThrowUserError('feature_disabled', { feature => 'old_charts' });
31
}
32

33
my $dir       = bz_locations()->{'datadir'} . "/mining";
34 35 36
my $graph_dir = bz_locations()->{'graphsdir'};
my $graph_url = basename($graph_dir);
my $product_name = $cgi->param('product') || '';
37

38
Bugzilla->switch_to_shadow_db();
39

40
if (!$product_name) {
41 42 43 44 45
    # Can we do bug charts?
    (-d $dir && -d $graph_dir) 
      || ThrowCodeError('chart_dir_nonexistent',
                        {dir => $dir, graph_dir => $graph_dir});

46
    my %default_sel = map { $_ => 1 } BUG_STATE_OPEN;
47 48 49 50 51 52 53 54 55 56 57

    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);
    }

58 59 60 61 62
    # We only want those products that the user has permissions for.
    my @myproducts = ('-All-');
    # Extract product names from objects and add them to the list.
    push( @myproducts, map { $_->name } @{$user->get_selectable_products} );

63 64
    $vars->{'datasets'} = \@datasets;
    $vars->{'products'} = \@myproducts;
65

66 67 68
    print $cgi->header();
}
else {
69 70 71
    # 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
    # in the "product" drop-down menu on the report generation form.
72 73 74
    my ($product) = grep { $_->name eq $product_name } @{$user->get_selectable_products};
    ($product || $product_name eq '-All-')
      || ThrowUserError('invalid_product_name', {product => $product_name});
75

76 77 78
    # Product names can change over time. Their ID cannot; so use the ID
    # to generate the filename.
    my $prod_id = $product ? $product->id : 0;
79

80 81 82
    # Make sure there is something to plot.
    my @datasets = $cgi->param('datasets');
    scalar(@datasets) || ThrowUserError('missing_datasets');
83

84 85 86
    if (grep { $_ !~ /^[A-Za-z0-9:_-]+$/ } @datasets) {
        ThrowUserError('invalid_datasets', {'datasets' => \@datasets});
    }
87

88 89 90 91
    # Filenames must not be guessable as they can point to products
    # you are not allowed to see. Also, different projects can have
    # the same product names.
    my $project = bz_locations()->{'project'} || '';
92 93 94 95 96
    my $image_file =  join(':', ($project, $prod_id, @datasets));
    my $key = Bugzilla->localconfig->{'site_wide_secret'};
    $image_file = hmac_sha256_base64($image_file, $key) . '.png';
    $image_file =~ s/\+/-/g;
    $image_file =~ s/\//_/g;
97
    trick_taint($image_file);
98

99
    if (! -e "$graph_dir/$image_file") {
100
        generate_chart($dir, "$graph_dir/$image_file", $product, \@datasets);
101
    }
102

103
    $vars->{'url_image'} = "$graph_url/$image_file";
104

105 106
    print $cgi->header(-Content_Disposition=>'inline; filename=bugzilla_report.html');
}
107

108 109 110
$template->process('reports/old-charts.html.tmpl', $vars)
  || ThrowTemplateError($template->error());

111 112 113 114 115 116 117 118
#####################
#    Subroutines    #
#####################

sub get_data {
    my $dir = shift;

    my @datasets;
119 120
    open(DATA, '<', "$dir/-All-")
      || ThrowCodeError('chart_file_open_fail', {filename => "$dir/-All-"});
121

122 123 124 125 126 127 128 129
    while (<DATA>) {
        if (/^# fields?: (.+)\s*$/) {
            @datasets = grep ! /date/i, (split /\|/, $1);
            last;
        }
    }
    close(DATA);
    return @datasets;
130
}
131

132
sub generate_chart {
133 134 135 136 137
    my ($dir, $image_file, $product, $datasets) = @_;
    $product = $product ? $product->name : '-All-';
    my $data_file = $product;
    $data_file =~ s/\//-/gs;
    $data_file = $dir . '/' . $data_file;
138

139
    if (! open FILE, $data_file) {
140 141 142
        if ($product eq '-All-') {
            $product = '';
        }
143
        ThrowCodeError('chart_data_not_generated', {'product' => $product});
144 145 146 147
    }

    my @fields;
    my @labels = qw(DATE);
148
    my %datasets = map { $_ => 1 } @$datasets;
149 150 151 152 153 154 155

    my %data = ();
    while (<FILE>) {
        chomp;
        next unless $_;
        if (/^#/) {
            if (/^# fields?: (.*)\s*$/) {
156
                @fields = split /\||\r/, $1;
157
                $data{$_} ||= [] foreach @fields;
158
                unless ($fields[0] =~ /date/i) {
159
                    ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file});
160
                }
161 162 163 164 165
                push @labels, grep($datasets{$_}, @fields);
            }
            next;
        }

166
        unless (@fields) {
167
            ThrowCodeError('chart_datafile_corrupt', {'file' => $data_file});
168
        }
169

170 171 172 173 174 175 176 177 178 179
        my @line = split /\|/;
        my $date = $line[0];
        my ($yy, $mm, $dd) = $date =~ /^\d{2}(\d{2})(\d{2})(\d{2})$/;
        push @{$data{DATE}}, "$mm/$dd/$yy";
        
        for my $i (1 .. $#fields) {
            my $field = $fields[$i];
            if (! defined $line[$i] or $line[$i] eq '') {
                # no data point given, don't plot (this will probably
                # generate loads of Chart::Base warnings, but that's not
180
                # our fault.)
181 182 183 184 185 186 187 188 189 190 191 192 193
                push @{$data{$field}}, undef;
            }
            else {
                push @{$data{$field}}, $line[$i];
            }
        }
    }
    
    shift @labels;

    close FILE;

    if (! @{$data{DATE}}) {
194
        ThrowUserError('insufficient_data_points');
195
    }
196

197 198 199 200 201 202 203 204 205 206 207
    my $img = Chart::Lines->new (800, 600);
    my $i = 0;

    my $MAXTICKS = 20;      # Try not to show any more x ticks than this.
    my $skip = 1;
    if (@{$data{DATE}} > $MAXTICKS) {
        $skip = int((@{$data{DATE}} + $MAXTICKS - 1) / $MAXTICKS);
    }

    my %settings =
        (
208
         "title" => "Status Counts for $product",
209 210 211 212 213 214 215 216 217 218 219 220 221
         "x_label" => "Dates",
         "y_label" => "Bug Counts",
         "legend_labels" => \@labels,
         "skip_x_ticks" => $skip,
         "y_grid_lines" => "true",
         "grey_background" => "false",
         "colors" => {
                      # default dataset colours are too alike
                      dataset4 => [0, 0, 0], # black
                     },
        );
    
    $img->set (%settings);
222
    $img->png($image_file, [ @data{('DATE', @labels)} ]);
223
}