Skip to content

Commit

Permalink
Improve speed of front page ‘recent problems’ query
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Somerville <matthew@mysociety.org>
  • Loading branch information
davea and dracos committed May 9, 2023
1 parent 051c9dc commit 85ab0a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@
- Add label to questionnaire textarea. #3944
- Offline report drafting. #4290
- Set width=device-width in viewport meta tag. #4384
- Improve performance of front page ‘recent problems’ query. #4424
- Bugfixes:
- Add ID attributes to change password form inputs.
- Fix link deactivation for privacy policy link on privacy policy page. #3704
Expand Down
11 changes: 7 additions & 4 deletions perllib/FixMyStreet/DB/ResultSet/Problem.pm
Expand Up @@ -162,7 +162,10 @@ sub _recent {
$query->{photo} = { '!=', undef } if $photos;

my $attrs = {
order_by => { -desc => \'coalesce(confirmed, created)' },
# NB order by most recently _created_, not confirmed, as the latter
# is too slow on installations with millions of reports.
# The correct ordering is applied by the `sort` lines below.
order_by => { -desc => 'id' },
rows => $num,
};

Expand All @@ -171,18 +174,18 @@ sub _recent {
$attrs->{bind} = [ $lat, $lon, $dist ];
$attrs->{join} = 'nearby';
$probs = [ mySociety::Locale::in_gb_locale {
$rs->search( $query, $attrs )->all;
sort { $b->confirmed <=> $a->confirmed } $rs->search( $query, $attrs )->all;
} ];
} else {
$probs = Memcached::get($key);
if ($probs) {
# Need to refetch to check if hidden since cached
$probs = [ $rs->search({
$probs = [ sort { $b->confirmed <=> $a->confirmed } $rs->search({

Check warning on line 183 in perllib/FixMyStreet/DB/ResultSet/Problem.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/DB/ResultSet/Problem.pm#L183

Added line #L183 was not covered by tests
id => [ map { $_->id } @$probs ],
%$query,
}, $attrs)->all ];
} else {
$probs = [ $rs->search( $query, $attrs )->all ];
$probs = [ sort { $b->confirmed <=> $a->confirmed } $rs->search( $query, $attrs )->all ];
Memcached::set($key, $probs, _cache_timeout());
}
}
Expand Down
24 changes: 24 additions & 0 deletions t/app/controller/index.t
@@ -1,4 +1,7 @@
use FixMyStreet::TestMech;
use DateTime;
use Web::Scraper;

my $mech = FixMyStreet::TestMech->new;

use t::Mock::Nominatim;
Expand Down Expand Up @@ -100,6 +103,27 @@ subtest "prefilters /around if filter_category given in URL" => sub {
$mech->content_contains("MyUniqueTestGroup");
};

subtest "recent reports are correctly shown on front page" => sub {
FixMyStreet::DB->resultset('Problem')->delete_all;

my ($r1, $r2, $r3)= $mech->create_problems_for_body(3, 2651, 'Recent problem');

# Make the oldest problem have the newest confirmed timestamp so it appears top of recent list
$r1->update( { confirmed => $r1->confirmed + DateTime::Duration->new( minutes => 5 ) } );

$mech->get_ok('/');

my $result = scraper {
process '#front-recently ul li h3', 'meta[]', 'TEXT';
}->scrape( $mech->response );

is_deeply $result->{meta}, [
'Recent problem Test 3 for 2651',
'Recent problem Test 1 for 2651',
'Recent problem Test 2 for 2651'
], "correct ordering of recent problems";
};

END {
done_testing();
}

0 comments on commit 85ab0a4

Please sign in to comment.