diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-08-18 20:21:33 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-08-18 20:21:33 +0100 |
commit | bbe8fb87daab16ca285521658bec1dcb2fbdf4d1 (patch) | |
tree | 3ca6de0720457ed9bd54d5665c9abac19d64df1e | |
parent | 9d8ad13ed231671f2ece5d81c5ce928807d0ba5f (diff) | |
parent | d1c90d15c2f9fd7ecb63a34c835d2c2007f290fe (diff) |
Merge branch 'speed-up-reports'
-rw-r--r-- | CHANGELOG.md | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 21 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm | 12 | ||||
-rw-r--r-- | t/app/model/user_planned_report.t | 5 |
7 files changed, 51 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 50d2ca9e0..4cc06e07a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,18 +7,21 @@ - Extra fields can be added to report form site-wide. #1743 - Body users can filter reports by all states. #1790 - Front end improvements: - - Always show pagination figures even if only one page. + - Always show pagination figures even if only one page. #1787 - Report pages list every update to a report. #1806 - Admin improvements: - - Highlight current shortlisted user in list tooltip. + - Highlight current shortlisted user in list tooltip. #1788 - Extra fields on contacts can be edited. #1743 + - Clearer highlight for selected duplicate on inspect form. #1798 + - Include MapIt API key on admin config page. #1778 - Bugfixes: - Set up action scheduled field when report loaded. #1789 - Fix sidebar hover behaviour being lost. #1808 - Stop errors from JS validator due to form in form. - Stop update form toggle causing report submission. - Update map size if an extra column has appeared. - - Improve performance of duplicate display. + - Improve performance of various pages. #1799 + - Duplicate list not loading when phone number present. #1803 - Development improvements: - `switch-site` script to automate switching config.yml files. #1741 - `make_css --watch` can run custom script after each compilation. diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index a410c2d91..c617f5733 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -441,6 +441,7 @@ sub inspect : Private { # This problem might no longer be visible on the current cobrand, # if its body has changed (e.g. by virtue of the category changing) # so redirect to a cobrand where it can be seen if necessary + $problem->discard_changes; my $redirect_uri; if ( $c->cobrand->is_council && !$c->cobrand->owns_problem($problem) ) { $redirect_uri = $c->cobrand->base_url_for_report( $problem ) . $problem->url; diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index fbb0e94a1..20ec8f0a2 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -414,6 +414,9 @@ sub load_and_group_problems : Private { order_by => $c->stash->{sort_order}, rows => $c->cobrand->reports_per_page, }; + if ($c->user_exists && $c->stash->{body} && $c->user->has_permission_to('planned_reports', $c->stash->{body}->id)) { + $filter->{prefetch} = 'user_planned_reports'; + } if (defined $c->stash->{filter_status}{shortlisted}) { $where->{'me.id'} = { '=', \"user_planned_reports.report_id"}; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 720e8cc93..19e76f6ed 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -502,13 +502,20 @@ Returns a hashref of bodies to which a report was sent. =cut -sub bodies($) { - my $self = shift; - return {} unless $self->bodies_str; - my $bodies = $self->bodies_str_ids; - my @bodies = $self->result_source->schema->resultset('Body')->search({ id => $bodies })->all; - return { map { $_->id => $_ } @bodies }; -} +has bodies => ( + is => 'ro', + lazy => 1, + default => sub { + my $self = shift; + return {} unless $self->bodies_str; + my $bodies = $self->bodies_str_ids; + my @bodies = $self->result_source->schema->resultset('Body')->search( + { id => $bodies }, + { prefetch => 'body_areas' }, + )->all; + return { map { $_->id => $_ } @bodies }; + }, +); sub body_names($) { my $self = shift; diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 8385d2eea..e44b2530f 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -412,9 +412,19 @@ sub active_planned_reports { $self->planned_reports->search({ removed => undef }); } +has active_user_planned_reports => ( + is => 'ro', + lazy => 1, + default => sub { + my $self = shift; + [ $self->user_planned_reports->search({ removed => undef })->all ]; + }, +); + sub is_planned_report { my ($self, $problem) = @_; - return $self->active_planned_reports->find({ id => $problem->id }); + my $id = $problem->id; + return scalar grep { $_->report_id == $id } @{$self->active_user_planned_reports}; } sub update_reputation { diff --git a/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm b/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm index 7e16e2dd3..460a4912e 100644 --- a/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm +++ b/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm @@ -6,7 +6,17 @@ use warnings; sub active { my $rs = shift; - $rs->search({ removed => undef }); + + # If we have been prefetched we can't use `active` as that'll blow away the + # cache and query the DB due to the `removed IS NULL` clause. So let's do + # the filtering here instead, if the query has been prefetched. + if ( $rs->get_cache ) { + my @users = grep { !defined($_->removed) } $rs->all; + $rs->set_cache(\@users); + $rs; + } else { + $rs->search({ removed => undef }); + } } sub for_report { diff --git a/t/app/model/user_planned_report.t b/t/app/model/user_planned_report.t index f2fe79cc2..e51552e5c 100644 --- a/t/app/model/user_planned_report.t +++ b/t/app/model/user_planned_report.t @@ -14,6 +14,7 @@ is $user->planned_reports, 0; $user->add_to_planned_reports($problem); is $user->active_planned_reports, 1; is $user->planned_reports, 1; +is $user->is_planned_report($problem), 1; $user->add_to_planned_reports($problem); is $user->active_planned_reports, 1; @@ -22,10 +23,14 @@ is $user->planned_reports, 1; $user->remove_from_planned_reports($problem); is $user->active_planned_reports, 0; is $user->planned_reports, 1; +$user->discard_changes; +is $user->is_planned_report($problem), 0; $user->add_to_planned_reports($problem); is $user->active_planned_reports, 1; is $user->planned_reports, 2; +$user->discard_changes; +is $user->is_planned_report($problem), 1; $user2->add_to_planned_reports($problem); is $user->active_planned_reports, 0; |