<commit_before>package FixMyStreet::App::Controller::Reports;
use Moose;
use namespace::autoclean;

use File::Slurp;
use List::MoreUtils qw(zip);
use POSIX qw(strcoll);
use mySociety::MaPit;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Reports - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index

Show the summary page of all reports.

=cut

sub index : Path : Args(0) {
    my ( $self, $c ) = @_;

    # Fetch all areas of the types we're interested in
    my $areas_info;
    eval {
        my $area_types = $c->cobrand->area_types;
        $areas_info = mySociety::MaPit::call('areas', $area_types,
            min_generation => $c->cobrand->area_min_generation
        );
    };
    if ($@) {
        $c->stash->{message} = _("Unable to look up areas in MaPit. Please try again later.") . ' ' .
            sprintf(_('The error was: %s'), $@);
        $c->stash->{template} = 'errors/generic.html';
        return;
    }

    # For each area, add its link and perhaps alter its name if we need to for
    # places with the same name.
    foreach (values %$areas_info) {
        $_->{url} = $c->uri_for( '/reports/' . $c->cobrand->short_name( $_, $areas_info ) );
        if ($_->{parent_area} && $_->{url} =~ /,|%2C/) {
            $_->{name} .= ', ' . $areas_info->{$_->{parent_area}}{name};
        }
    }

    $c->stash->{areas_info} = $areas_info;
    my @keys = sort { strcoll($areas_info->{$a}{name}, $areas_info->{$b}{name}) } keys %$areas_info;
    @keys = $c->cobrand->filter_all_council_ids_list( @keys );
    $c->stash->{areas_info_sorted} = [ map { $areas_info->{$_} } @keys ];

    eval {
        my $data = File::Slurp::read_file(
            FixMyStreet->path_to( '../data/all-reports.json' )->stringify
        );
        my $j = JSON->new->utf8->decode($data);
        $c->stash->{fixed} = $j->{fixed};
        $c->stash->{open} = $j->{open};
    };
    if ($@) {
        $c->stash->{message} = _("There was a problem showing the All Reports page. Please try again later.");
        if ($c->config->{STAGING_SITE}) {
            $c->stash->{message} .= '</p><p>Perhaps the bin/update-all-reports script needs running.</p><p>'
                . sprintf(_('The error was: %s'), $@);
        }
        $c->stash->{template} = 'errors/generic.html';
        return;
    }

    # Down here so that error pages aren't cached.
    $c->response->header('Cache-Control' => 'max-age=3600');
}

=head2 index
<commit_msg>Expand no reports message to make it more helpful<commit_after>package FixMyStreet::App::Controller::Reports;
use Moose;
use namespace::autoclean;

use File::Slurp;
use List::MoreUtils qw(zip);
use POSIX qw(strcoll);
use mySociety::MaPit;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Reports - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index

Show the summary page of all reports.

=cut

sub index : Path : Args(0) {
    my ( $self, $c ) = @_;

    # Fetch all areas of the types we're interested in
    my $areas_info;
    eval {
        my $area_types = $c->cobrand->area_types;
        $areas_info = mySociety::MaPit::call('areas', $area_types,
            min_generation => $c->cobrand->area_min_generation
        );
    };
    if ($@) {
        $c->stash->{message} = _("Unable to look up areas in MaPit. Please try again later.") . ' ' .
            sprintf(_('The error was: %s'), $@);
        $c->stash->{template} = 'errors/generic.html';
        return;
    }

    # For each area, add its link and perhaps alter its name if we need to for
    # places with the same name.
    foreach (values %$areas_info) {
        $_->{url} = $c->uri_for( '/reports/' . $c->cobrand->short_name( $_, $areas_info ) );
        if ($_->{parent_area} && $_->{url} =~ /,|%2C/) {
            $_->{name} .= ', ' . $areas_info->{$_->{parent_area}}{name};
        }
    }

    $c->stash->{areas_info} = $areas_info;
    my @keys = sort { strcoll($areas_info->{$a}{name}, $areas_info->{$b}{name}) } keys %$areas_info;
    @keys = $c->cobrand->filter_all_council_ids_list( @keys );
    $c->stash->{areas_info_sorted} = [ map { $areas_info->{$_} } @keys ];

    eval {
        my $data = File::Slurp::read_file(
            FixMyStreet->path_to( '../data/all-reports.json' )->stringify
        );
        my $j = JSON->new->utf8->decode($data);
        $c->stash->{fixed} = $j->{fixed};
        $c->stash->{open} = $j->{open};
    };
    if ($@) {
        $c->stash->{message} = _("There was a problem showing the All Reports page. Please try again later.");
        if ($c->config->{STAGING_SITE}) {
            $c->stash->{message} .= '</p><p>Perhaps the bin/update-all-reports script needs running. Use: bin/cron-wrapper bin/update-all-reports</p><p>'
                . sprintf(_('The error was: %s'), $@);
        }
        $c->stash->{template} = 'errors/generic.html';
        return;
    }

    # Down here so that error pages aren't cached.
    $c->response->header('Cache-Control' => 'max-age=3600');
}

=head2 index

{'fraction_non_alphanumeric': 0.18543821979309, 'fraction_numerical': 0.004684755026351747, 'mean_word_length': 2.688984881209503, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3216740', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1810, 'n_words': 403}