diff options
-rw-r--r-- | perllib/FixMyStreet/App.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Barnet.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 17 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Southampton.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 80 | ||||
-rw-r--r-- | perllib/Problems.pm | 41 |
6 files changed, 80 insertions, 68 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 4aa9cc75b..811a40eb0 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -130,7 +130,7 @@ sub _get_cobrand { ? FixMyStreet::Cobrand->get_class_for_moniker($override_moniker) : FixMyStreet::Cobrand->get_class_for_host($host); - my $cobrand = $cobrand_class->new( { request => $c->req } ); + my $cobrand = $cobrand_class->new( { c => $c } ); return $cobrand; } diff --git a/perllib/FixMyStreet/Cobrand/Barnet.pm b/perllib/FixMyStreet/Cobrand/Barnet.pm index f68d61256..5373d8c1d 100644 --- a/perllib/FixMyStreet/Cobrand/Barnet.pm +++ b/perllib/FixMyStreet/Cobrand/Barnet.pm @@ -52,8 +52,8 @@ sub council_check { } my $url = 'http://www.fixmystreet.com/'; $url .= 'alert' if $context eq 'alert'; - $url .= '?pc=' . URI::Escape::uri_escape( $self->{request}->param('pc') ) - if $self->{request}->param('pc'); + $url .= '?pc=' . URI::Escape::uri_escape( $self->{c}->req->param('pc') ) + if $self->{c}->req->param('pc'); my $error_msg = "That location is not covered by Barnet. Please visit <a href=\"$url\">the main FixMyStreet site</a>."; return ( 0, $error_msg ); diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index b002dd7f2..2c64449c3 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -11,9 +11,9 @@ use mySociety::MaPit; =head2 new my $cobrand = $class->new; - my $cobrand = $class->new( { request => $c->req } ); + my $cobrand = $class->new( { c => $c } ); -Create a new cobrand object, optionally setting the web request. +Create a new cobrand object, optionally setting the context. You probably shouldn't need to do this and should get the cobrand object via a method in L<FixMyStreet::Cobrand> instead. @@ -208,8 +208,8 @@ Return recent problems on the site. =cut sub recent { - my $self = shift; - return Problems::recent(@_); + my ( $self ) = @_; + return $self->{c}->model('DB::Problem')->recent(); } =item shorten_recency_if_new_greater_than_fixed @@ -231,18 +231,19 @@ can then format. =cut sub front_stats_data { - my $self = shift; + my ( $self ) = @_; + my $c = $self->{c}; my $recency = '1 week'; my $shorter_recency = '3 days'; - my $fixed = Problems::recent_fixed(); + my $fixed = $c->model('DB::Problem')->recent_fixed(); my $updates = Problems::number_comments(); - my $new = Problems::recent_new($recency); + my $new = $c->model('DB::Problem')->recent_new( $recency ); if ( $new > $fixed && $self->shorten_recency_if_new_greater_than_fixed ) { $recency = $shorter_recency; - $new = Problems::recent_new($recency); + $new = $c->model('DB::Problem')->recent_new( $recency ); } my $stats = { diff --git a/perllib/FixMyStreet/Cobrand/Southampton.pm b/perllib/FixMyStreet/Cobrand/Southampton.pm index aa9945c00..24449cf6e 100644 --- a/perllib/FixMyStreet/Cobrand/Southampton.pm +++ b/perllib/FixMyStreet/Cobrand/Southampton.pm @@ -51,8 +51,8 @@ sub council_check { } my $url = 'http://www.fixmystreet.com/'; $url .= 'alert' if $context eq 'alert'; - $url .= '?pc=' . URI::Escape::uri_escape_utf8($self->{request}->param('pc')) - if $self->{request}->param('pc'); + $url .= '?pc=' . URI::Escape::uri_escape_utf8($self->{c}->req->param('pc')) + if $self->{c}->req->param('pc'); my $error_msg = "That location is not covered by Southampton. Please visit <a href=\"$url\">the main FixMyStreet site</a>."; return ( 0, $error_msg ); diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index b029caf3b..d73d58b82 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -4,6 +4,72 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +my $site_restriction; +my $site_key; + +sub set_restriction { + my ( $rs, $sql, $key, $restriction ) = @_; + $site_key = $key; + $site_restriction = $restriction; +} + +sub site_restricted { + my ( $rs ) = @_; + return $rs->search( $site_restriction ); +} + +# Front page statistics + +sub recent_fixed { + my $rs = shift; + my $key = "recent_fixed:$site_key"; + my $result = Memcached::get($key); + unless ($result) { + $result = $rs->site_restricted->search( { + state => 'fixed', + lastupdate => { '>', \"current_timestamp-'1 month'::interval" }, + } )->count; + Memcached::set($key, $result, 3600); + } + return $result; +} + +sub recent_new { + my ( $rs, $interval ) = @_; + (my $key = $interval) =~ s/\s+//g; + $key = "recent_new:$site_key:$key"; + my $result = Memcached::get($key); + unless ($result) { + $result = $rs->site_restricted->search( { + state => [ 'confirmed', 'fixed' ], + confirmed => { '>', \"current_timestamp-'$interval'::interval" }, + } )->count; + Memcached::set($key, $result, 3600); + } + return $result; +} + +# Front page recent lists + +sub recent { + my ( $rs ) = @_; + my $key = "recent:$site_key"; + my $result = Memcached::get($key); + unless ($result) { + $result = $rs->site_restricted->search( { + state => [ 'confirmed', 'fixed' ] + }, { + columns => [ 'id', 'title' ], + order_by => { -desc => 'confirmed' }, + rows => 5, + } )->count; + Memcached::set($key, $result, 3600); + } + return $result; +} + +# Admin functions + sub timeline { my ( $rs, $restriction ) = @_; @@ -40,18 +106,4 @@ sub summary_count { ); } -my $site_restriction; -my $site_key; - -sub set_restriction { - my ( $rs, $sql, $key, $restriction ) = @_; - $site_key = $key; - $site_restriction = $restriction; -} - -sub site_restricted { - my ( $rs ) = @_; - return $rs->search( $site_restriction ); -} - 1; diff --git a/perllib/Problems.pm b/perllib/Problems.pm index 143b5bb9b..a6eb0c519 100644 --- a/perllib/Problems.pm +++ b/perllib/Problems.pm @@ -27,20 +27,6 @@ sub current_timestamp { return "'$current_timestamp'::timestamp"; } -# Front page statistics - -sub recent_fixed { - my $key = "recent_fixed:$site_key"; - my $result = Memcached::get($key); - unless ($result) { - $result = dbh()->selectrow_array("select count(*) from problem - where state='fixed' and lastupdate>" . current_timestamp() . "-'1 month'::interval - $site_restriction"); - Memcached::set($key, $result, 3600); - } - return $result; -} - sub number_comments { my $key = "number_comments:$site_key"; my $result = Memcached::get($key); @@ -58,20 +44,6 @@ sub number_comments { return $result; } -sub recent_new { - my $interval = shift; - (my $key = $interval) =~ s/\s+//g; - $key = "recent_new:$site_key:$key"; - my $result = Memcached::get($key); - unless ($result) { - $result = dbh()->selectrow_array("select count(*) from problem - where state in ('confirmed','fixed') and confirmed>" . current_timestamp() . "-'$interval'::interval - $site_restriction"); - Memcached::set($key, $result, 3600); - } - return $result; -} - # Front page recent lists sub recent_photos { @@ -113,19 +85,6 @@ sub recent_photos { return $out; } -sub recent { - my $key = "recent:$site_key"; - my $result = Memcached::get($key); - unless ($result) { - $result = select_all("select id,title from problem - where state in ('confirmed', 'fixed') - $site_restriction - order by confirmed desc limit 5"); - Memcached::set($key, $result, 3600); - } - return $result; -} - # Problems around a location sub around_map { |