diff options
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 151 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/Stats.pm | 156 | ||||
-rw-r--r-- | t/app/controller/admin.t | 6 | ||||
-rw-r--r-- | templates/web/base/admin/stats/fix_rate.html (renamed from templates/web/base/admin/stats_fix_rate.html) | 0 | ||||
-rw-r--r-- | templates/web/base/admin/stats/index.html (renamed from templates/web/base/admin/stats.html) | 8 | ||||
-rw-r--r-- | templates/web/base/admin/stats/questionnaire.html (renamed from templates/web/base/admin/questionnaire.html) | 0 | ||||
-rw-r--r-- | templates/web/base/admin/stats/state.html (renamed from templates/web/base/admin/stats_by_state.html) | 0 | ||||
-rw-r--r-- | templates/web/zurich/admin/stats/index.html (renamed from templates/web/zurich/admin/stats.html) | 0 |
8 files changed, 167 insertions, 154 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 27aeb9e5b..0551a4d73 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -73,7 +73,7 @@ sub index : Path : Args(0) { return $c->cobrand->admin(); } - $c->forward('stats_by_state'); + $c->forward('/admin/stats/state'); my @unsent = $c->cobrand->problems->search( { state => [ FixMyStreet::DB::Result::Problem::open_states() ], @@ -183,39 +183,6 @@ sub timeline : Path( 'timeline' ) : Args(0) { return 1; } -sub questionnaire : Path('stats/questionnaire') : Args(0) { - my ( $self, $c ) = @_; - - my $questionnaires = $c->model('DB::Questionnaire')->search( - { whenanswered => { '!=', undef } }, - { group_by => [ 'ever_reported' ], - select => [ 'ever_reported', { count => 'me.id' } ], - as => [ qw/reported questionnaire_count/ ] } - ); - - my %questionnaire_counts = map { - ( defined $_->get_column( 'reported' ) ? $_->get_column( 'reported' ) : -1 ) - => $_->get_column( 'questionnaire_count' ) - } $questionnaires->all; - $questionnaire_counts{1} ||= 0; - $questionnaire_counts{0} ||= 0; - $questionnaire_counts{total} = $questionnaire_counts{0} + $questionnaire_counts{1}; - $c->stash->{questionnaires} = \%questionnaire_counts; - - $c->stash->{state_changes_count} = $c->model('DB::Questionnaire')->search( - { whenanswered => \'is not null' } - )->count; - $c->stash->{state_changes} = $c->model('DB::Questionnaire')->search( - { whenanswered => \'is not null' }, - { - group_by => [ 'old_state', 'new_state' ], - columns => [ 'old_state', 'new_state', { c => { count => 'id' } } ], - }, - ); - - return 1; -} - sub bodies : Path('bodies') : Args(0) { my ( $self, $c ) = @_; @@ -1660,122 +1627,6 @@ sub flagged : Path('flagged') : Args(0) { return 1; } -sub stats_by_state : Path('stats/state') : Args(0) { - my ( $self, $c ) = @_; - - my $problems = $c->cobrand->problems->summary_count; - - my %prob_counts = - map { $_->state => $_->get_column('state_count') } $problems->all; - - %prob_counts = - map { $_ => $prob_counts{$_} || 0 } - ( FixMyStreet::DB::Result::Problem->all_states() ); - $c->stash->{problems} = \%prob_counts; - $c->stash->{total_problems_live} += $prob_counts{$_} ? $prob_counts{$_} : 0 - for ( FixMyStreet::DB::Result::Problem->visible_states() ); - $c->stash->{total_problems_users} = $c->cobrand->problems->unique_users; - - my $comments = $c->cobrand->updates->summary_count; - - my %comment_counts = - map { $_->state => $_->get_column('state_count') } $comments->all; - - $c->stash->{comments} = \%comment_counts; -} - -sub stats_fix_rate : Path('stats/fix-rate') : Args(0) { - my ( $self, $c ) = @_; - - $c->stash->{categories} = $c->cobrand->problems->categories_summary(); -} - -sub stats : Path('stats') : Args(0) { - my ( $self, $c ) = @_; - - my $selected_body; - if ( $c->user->is_superuser ) { - $c->forward('fetch_all_bodies'); - $selected_body = $c->get_param('body'); - } else { - $selected_body = $c->user->from_body->id; - } - - if ( $c->cobrand->moniker eq 'zurich' ) { - return $c->cobrand->admin_stats(); - } - - if ( $c->get_param('getcounts') ) { - - my ( $start_date, $end_date, @errors ); - my $parser = DateTime::Format::Strptime->new( pattern => '%d/%m/%Y' ); - - $start_date = $parser-> parse_datetime ( $c->get_param('start_date') ); - - push @errors, _('Invalid start date') unless defined $start_date; - - $end_date = $parser-> parse_datetime ( $c->get_param('end_date') ) ; - - push @errors, _('Invalid end date') unless defined $end_date; - - $c->stash->{errors} = \@errors; - $c->stash->{start_date} = $start_date; - $c->stash->{end_date} = $end_date; - - $c->stash->{unconfirmed} = $c->get_param('unconfirmed') eq 'on' ? 1 : 0; - - return 1 if @errors; - - my $bymonth = $c->get_param('bymonth'); - $c->stash->{bymonth} = $bymonth; - - $c->stash->{selected_body} = $selected_body; - - my $field = 'confirmed'; - - $field = 'created' if $c->get_param('unconfirmed'); - - my $one_day = DateTime::Duration->new( days => 1 ); - - - my %select = ( - select => [ 'state', { 'count' => 'me.id' } ], - as => [qw/state count/], - group_by => [ 'state' ], - order_by => [ 'state' ], - ); - - if ( $c->get_param('bymonth') ) { - %select = ( - select => [ - { extract => \"year from $field", -as => 'c_year' }, - { extract => \"month from $field", -as => 'c_month' }, - { 'count' => 'me.id' } - ], - as => [qw/c_year c_month count/], - group_by => [qw/c_year c_month/], - order_by => [qw/c_year c_month/], - ); - } - - my $p = $c->cobrand->problems->to_body($selected_body)->search( - { - -AND => [ - $field => { '>=', $start_date}, - $field => { '<=', $end_date + $one_day }, - ], - }, - \%select, - ); - - # in case the total_report count is 0 - $c->stash->{show_count} = 1; - $c->stash->{states} = $p; - } - - return 1; -} - =head2 set_allowed_pages Sets up the allowed_pages stash entry for checking if the current page is diff --git a/perllib/FixMyStreet/App/Controller/Admin/Stats.pm b/perllib/FixMyStreet/App/Controller/Admin/Stats.pm new file mode 100644 index 000000000..35e150890 --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Admin/Stats.pm @@ -0,0 +1,156 @@ +package FixMyStreet::App::Controller::Admin::Stats; +use Moose; +use namespace::autoclean; + +BEGIN { extends 'Catalyst::Controller'; } + +sub index : Path : Args(0) { + my ( $self, $c ) = @_; + + my $selected_body; + if ( $c->user->is_superuser ) { + $c->forward('/admin/fetch_all_bodies'); + $selected_body = $c->get_param('body'); + } else { + $selected_body = $c->user->from_body->id; + } + + if ( $c->cobrand->moniker eq 'zurich' ) { + return $c->cobrand->admin_stats(); + } + + if ( $c->get_param('getcounts') ) { + + my ( $start_date, $end_date, @errors ); + my $parser = DateTime::Format::Strptime->new( pattern => '%d/%m/%Y' ); + + $start_date = $parser-> parse_datetime ( $c->get_param('start_date') ); + + push @errors, _('Invalid start date') unless defined $start_date; + + $end_date = $parser-> parse_datetime ( $c->get_param('end_date') ) ; + + push @errors, _('Invalid end date') unless defined $end_date; + + $c->stash->{errors} = \@errors; + $c->stash->{start_date} = $start_date; + $c->stash->{end_date} = $end_date; + + $c->stash->{unconfirmed} = $c->get_param('unconfirmed') eq 'on' ? 1 : 0; + + return 1 if @errors; + + my $bymonth = $c->get_param('bymonth'); + $c->stash->{bymonth} = $bymonth; + + $c->stash->{selected_body} = $selected_body; + + my $field = 'confirmed'; + + $field = 'created' if $c->get_param('unconfirmed'); + + my $one_day = DateTime::Duration->new( days => 1 ); + + + my %select = ( + select => [ 'state', { 'count' => 'me.id' } ], + as => [qw/state count/], + group_by => [ 'state' ], + order_by => [ 'state' ], + ); + + if ( $c->get_param('bymonth') ) { + %select = ( + select => [ + { extract => \"year from $field", -as => 'c_year' }, + { extract => \"month from $field", -as => 'c_month' }, + { 'count' => 'me.id' } + ], + as => [qw/c_year c_month count/], + group_by => [qw/c_year c_month/], + order_by => [qw/c_year c_month/], + ); + } + + my $p = $c->cobrand->problems->to_body($selected_body)->search( + { + -AND => [ + $field => { '>=', $start_date}, + $field => { '<=', $end_date + $one_day }, + ], + }, + \%select, + ); + + # in case the total_report count is 0 + $c->stash->{show_count} = 1; + $c->stash->{states} = $p; + } + + return 1; +} + +sub state : Local : Args(0) { + my ( $self, $c ) = @_; + + my $problems = $c->cobrand->problems->summary_count; + + my %prob_counts = + map { $_->state => $_->get_column('state_count') } $problems->all; + + %prob_counts = + map { $_ => $prob_counts{$_} || 0 } + ( FixMyStreet::DB::Result::Problem->all_states() ); + $c->stash->{problems} = \%prob_counts; + $c->stash->{total_problems_live} += $prob_counts{$_} ? $prob_counts{$_} : 0 + for ( FixMyStreet::DB::Result::Problem->visible_states() ); + $c->stash->{total_problems_users} = $c->cobrand->problems->unique_users; + + my $comments = $c->cobrand->updates->summary_count; + + my %comment_counts = + map { $_->state => $_->get_column('state_count') } $comments->all; + + $c->stash->{comments} = \%comment_counts; +} + +sub fix_rate : Path('fix-rate') : Args(0) { + my ( $self, $c ) = @_; + + $c->stash->{categories} = $c->cobrand->problems->categories_summary(); +} + +sub questionnaire : Local : Args(0) { + my ( $self, $c ) = @_; + + my $questionnaires = $c->model('DB::Questionnaire')->search( + { whenanswered => { '!=', undef } }, + { group_by => [ 'ever_reported' ], + select => [ 'ever_reported', { count => 'me.id' } ], + as => [ qw/reported questionnaire_count/ ] } + ); + + my %questionnaire_counts = map { + ( defined $_->get_column( 'reported' ) ? $_->get_column( 'reported' ) : -1 ) + => $_->get_column( 'questionnaire_count' ) + } $questionnaires->all; + $questionnaire_counts{1} ||= 0; + $questionnaire_counts{0} ||= 0; + $questionnaire_counts{total} = $questionnaire_counts{0} + $questionnaire_counts{1}; + $c->stash->{questionnaires} = \%questionnaire_counts; + + $c->stash->{state_changes_count} = $c->model('DB::Questionnaire')->search( + { whenanswered => \'is not null' } + )->count; + $c->stash->{state_changes} = $c->model('DB::Questionnaire')->search( + { whenanswered => \'is not null' }, + { + group_by => [ 'old_state', 'new_state' ], + columns => [ 'old_state', 'new_state', { c => { count => 'id' } } ], + }, + ); + + return 1; +} + +1; diff --git a/t/app/controller/admin.t b/t/app/controller/admin.t index 3f69829f7..0be54dbc5 100644 --- a/t/app/controller/admin.t +++ b/t/app/controller/admin.t @@ -1746,4 +1746,10 @@ subtest "response priorities can't be viewed across councils" => sub { }; }; +subtest "smoke view some stats pages" => sub { + $mech->log_in_ok( $superuser->email ); + $mech->get_ok('/admin/stats/fix-rate'); + $mech->get_ok('/admin/stats/questionnaire'); +}; + done_testing(); diff --git a/templates/web/base/admin/stats_fix_rate.html b/templates/web/base/admin/stats/fix_rate.html index fb88a1c06..fb88a1c06 100644 --- a/templates/web/base/admin/stats_fix_rate.html +++ b/templates/web/base/admin/stats/fix_rate.html diff --git a/templates/web/base/admin/stats.html b/templates/web/base/admin/stats/index.html index 150afd619..248bea077 100644 --- a/templates/web/base/admin/stats.html +++ b/templates/web/base/admin/stats/index.html @@ -53,9 +53,9 @@ [% END %] [% ELSE %] <ul> -<li><a href="stats/questionnaire">[% loc('Survey Results') %]</a></li> -<li><a href="stats/state">[% loc('Problem breakdown by state') %]</a></li> -<li><a href="stats/fix-rate">[% loc('Category fix rate for problems > 4 weeks old') %]</a></li> +<li><a href="[% c.uri_for_action('admin/stats/questionnaire') %]">[% loc('Survey Results') %]</a></li> +<li><a href="[% c.uri_for_action('admin/stats/state') %]">[% loc('Problem breakdown by state') %]</a></li> +<li><a href="[% c.uri_for_action('admin/stats/fix_rate') %]">[% loc('Category fix rate for problems > 4 weeks old') %]</a></li> </ul> [% END %] @@ -66,7 +66,7 @@ [% END %] [% END %] -<form method="post" action="[% c.uri_for('stats') %]" enctype="application/x-www-form-urlencoded" accept-charset="utf-8"> +<form method="post" action="[% c.uri_for_action('admin/stats/index') %]" enctype="application/x-www-form-urlencoded" accept-charset="utf-8"> <p> <label for="start_date">[% loc('Start Date:') %]</label><input type="text" class="form-control" placeholder="[% loc('Click here or enter as dd/mm/yyyy') %]" name="start_date" id="start_date" diff --git a/templates/web/base/admin/questionnaire.html b/templates/web/base/admin/stats/questionnaire.html index 680e0d214..680e0d214 100644 --- a/templates/web/base/admin/questionnaire.html +++ b/templates/web/base/admin/stats/questionnaire.html diff --git a/templates/web/base/admin/stats_by_state.html b/templates/web/base/admin/stats/state.html index 6bcd35f88..6bcd35f88 100644 --- a/templates/web/base/admin/stats_by_state.html +++ b/templates/web/base/admin/stats/state.html diff --git a/templates/web/zurich/admin/stats.html b/templates/web/zurich/admin/stats/index.html index ce8e238f7..ce8e238f7 100644 --- a/templates/web/zurich/admin/stats.html +++ b/templates/web/zurich/admin/stats/index.html |