diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/Memcached.pm | 31 | ||||
-rw-r--r-- | perllib/Page.pm | 73 | ||||
-rw-r--r-- | perllib/Problems.pm | 117 |
3 files changed, 179 insertions, 42 deletions
diff --git a/perllib/Memcached.pm b/perllib/Memcached.pm new file mode 100644 index 000000000..99ee60be1 --- /dev/null +++ b/perllib/Memcached.pm @@ -0,0 +1,31 @@ +# +# Memcached.pm: +# Trying out memcached on FixMyStreet +# +# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ +# +# $Id: Memcached.pm,v 1.1 2008-09-16 15:45:09 matthew Exp $ +# + +package Memcached; + +use strict; +use Cache::Memcached; + +my $memcache = new Cache::Memcached { + 'servers' => [ '127.0.0.1:11211' ], + 'namespace' => 'fms', + 'debug' => 0, + 'compress_threshold' => 10_000, +}; + +sub get { + $memcache->get(@_); +} + +sub set { + $memcache->set(@_); +} + +1; diff --git a/perllib/Page.pm b/perllib/Page.pm index 6cacf18be..164ccca76 100644 --- a/perllib/Page.pm +++ b/perllib/Page.pm @@ -6,7 +6,7 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: Page.pm,v 1.107 2008-09-10 11:37:36 matthew Exp $ +# $Id: Page.pm,v 1.108 2008-09-16 15:45:09 matthew Exp $ # package Page; @@ -25,6 +25,8 @@ use Problems; use mySociety::Config; use mySociety::DBHandle qw/dbh select_all/; use mySociety::EvEl; +use mySociety::Gaze; +use mySociety::GeoUtil; use mySociety::Locale; use mySociety::MaPit; use mySociety::PostcodeUtil; @@ -221,7 +223,7 @@ EOF <a href="http://www.mysociety.org/"><img id="logo" src="/i/mysociety-dark.png" alt="View mySociety.org"><span id="logoie"></span></a> <p id="footer">Built by <a href="http://www.mysociety.org/">mySociety</a>, -using some <a href="https://secure.mysociety.org/cvstrac/dir?d=mysociety/bci">clever</a> <a +using some <a href="https://secure.mysociety.org/cvstrac/dir?d=mysociety/bci">clever</a> <a href="https://secure.mysociety.org/cvstrac/dir?d=mysociety/services/TilMa">code</a>. Formerly <a href="/faq#nfi">Neighbourhood Fix-It</a>.</p> $track @@ -292,8 +294,8 @@ sub display_map { $out .= <<EOF; <form action="./" method="post" id="mapForm"$encoding> <input type="hidden" name="submit_map" value="1"> -<input type="hidden" name="x" value="$x"> -<input type="hidden" name="y" value="$y"> +<input type="hidden" name="x" id="formX" value="$x"> +<input type="hidden" name="y" id="formY" value="$y"> <input type="hidden" name="pc" value="$pc_enc"> EOF $img_type = '<input type="image"'; @@ -311,7 +313,7 @@ var start_x = $px; var start_y = $py; $params{pre} <div id="map"><div id="drag"> $img_type alt="NW map tile" id="t2.2" name="tile_$tl" src="$tl_src" style="top:0px; left:0;">$img_type alt="NE map tile" id="t2.3" name="tile_$tr" src="$tr_src" style="top:0px; left:$imgw;"><br>$img_type alt="SW map tile" id="t3.2" name="tile_$bl" src="$bl_src" style="top:$imgh; left:0;">$img_type alt="SE map tile" id="t3.3" name="tile_$br" src="$br_src" style="top:$imgh; left:$imgw;"> - $params{pins} + <div id="pins">$params{pins}</div> </div> EOF $out .= '<div id="watermark"></div>'; @@ -347,6 +349,66 @@ sub display_pin { return $out; } +sub map_pins { + my ($q, $x, $y, $sx, $sy) = @_; + + my $pins = ''; + my $min_e = Page::tile_to_os($x-2); # Extra space to left/below due to rounding, I think + my $min_n = Page::tile_to_os($y-2); + #my $map_le = Page::tile_to_os($x); + #my $map_ln = Page::tile_to_os($y); + my $mid_e = Page::tile_to_os($x+1); + my $mid_n = Page::tile_to_os($y+1); + #my $map_re = Page::tile_to_os($x+2); + #my $map_rn = Page::tile_to_os($y+2); + my $max_e = Page::tile_to_os($x+3); + my $max_n = Page::tile_to_os($y+3); + + my $around_map = Problems::around_map($min_e, $max_e, $min_n, $max_n); + my @ids = (); + #my $count_prob = 1; + #my $count_fixed = 1; + foreach (@$around_map) { + push(@ids, $_->{id}); + my $px = Page::os_to_px($_->{easting}, $sx); + my $py = Page::os_to_px($_->{northing}, $sy, 1); + my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; + $pins .= Page::display_pin($q, $px, $py, $col); # , $count_prob++); + } + + my ($lat, $lon) = mySociety::GeoUtil::national_grid_to_wgs84($mid_e, $mid_n, 'G'); + my $dist = mySociety::Gaze::get_radius_containing_population($lat, $lon, 200000); + $dist = int($dist*10+0.5)/10; + + # XXX: Change to only show problems with extract(epoch from ms_current_timestamp()-lastupdate) < 8 weeks + # And somehow display/link to old problems somewhere else... + my $nearby = []; + #if (@$current_map < 9) { + my $limit = 9; # - @$current_map; + $nearby = Problems::nearby($dist, join(',', @ids), $limit, $mid_e, $mid_n); + foreach (@$nearby) { + my $px = Page::os_to_px($_->{easting}, $sx); + my $py = Page::os_to_px($_->{northing}, $sy, 1); + my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; + $pins .= Page::display_pin($q, $px, $py, $col); #, $count_prob++); + } + #} else { + # @$current_map = @$current_map[0..8]; + #} + + #my $fixed = Problems::fixed_nearby($dist, $mid_e, $mid_n); + #foreach (@$fixed) { + # my $px = Page::os_to_px($_->{easting}, $sx); + # my $py = Page::os_to_px($_->{northing}, $sy, 1); + # $pins .= Page::display_pin($q, $px, $py, 'green'); # , $count_fixed++); + #} + #if (@$fixed > 9) { + # @$fixed = @$fixed[0..8]; + #} + + return ($pins, $around_map, $nearby, $dist); # , $current_map, $current, '', $dist); # $fixed, $dist); +} + sub compass ($$$) { my ($q, $x, $y) = @_; my @compass; @@ -451,6 +513,7 @@ sub send_email { my $action = ($thing eq 'alert') ? 'confirmed' : 'posted'; $thing .= ' report' if $thing eq _('problem'); + $thing = 'expression of interest' if $thing eq 'tms'; my $out = <<EOF; <h1>Nearly Done! Now check your email...</h1> <p>The confirmation email <strong>may</strong> take a few minutes to arrive — <em>please</em> be patient.</p> diff --git a/perllib/Problems.pm b/perllib/Problems.pm index 38dbdef07..72b3a7ebc 100644 --- a/perllib/Problems.pm +++ b/perllib/Problems.pm @@ -6,49 +6,74 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: Problems.pm,v 1.4 2008-09-10 11:37:36 matthew Exp $ +# $Id: Problems.pm,v 1.5 2008-09-16 15:45:09 matthew Exp $ # package Problems; use strict; +# use Memcached; use mySociety::DBHandle qw/dbh select_all/; use mySociety::Web qw/ent/; my $site_restriction = ''; +my $site_key = 0; sub set_site_restriction { my $site = shift; my @cats = Page::scambs_categories(); my $cats = join("','", @cats); $site_restriction = " and council=2260 and category in - ('$cats') " + ('$cats') " if $site eq 'scambs'; + $site_key = 1; } # Front page statistics sub recent_fixed { - dbh()->selectrow_array("select count(*) from problem - where state='fixed' and lastupdate>ms_current_timestamp()-'1 month'::interval - $site_restriction"); + my $result; + #my $key = "recent_fixed:$site_key"; + #$result = Memcached::get($key); + #unless ($result) { + $result = dbh()->selectrow_array("select count(*) from problem + where state='fixed' and lastupdate>ms_current_timestamp()-'1 month'::interval + $site_restriction"); + # Memcached::set($key, $result, 3600); + #} + return $result; } sub number_comments { - if ($site_restriction) { - dbh()->selectrow_array("select count(*) from comment, problem - where comment.problem_id=problem.id and comment.state='confirmed' - $site_restriction"); - } else { - dbh()->selectrow_array("select count(*) from comment - where state='confirmed'"); - } + my $result; + #my $key = "number_comments:$site_key"; + #$result = Memcached::get($key); + #unless ($result) { + if ($site_restriction) { + $result = dbh()->selectrow_array("select count(*) from comment, problem + where comment.problem_id=problem.id and comment.state='confirmed' + $site_restriction"); + } else { + $result = dbh()->selectrow_array("select count(*) from comment + where state='confirmed'"); + } + # Memcached::set($key, $result, 3600); + #} + return $result; } sub recent_new { my $interval = shift; - dbh()->selectrow_array("select count(*) from problem - where state in ('confirmed','fixed') and confirmed>ms_current_timestamp()-'$interval'::interval - $site_restriction"); + my $result; + #(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>ms_current_timestamp()-'$interval'::interval + $site_restriction"); + # Memcached::set($key, $result, 3600); + #} + return $result; } # Front page recent lists @@ -57,17 +82,27 @@ sub recent_photos { my ($num, $e, $n, $dist) = @_; my $probs; if ($e) { - $probs = select_all("select id, title - from problem_find_nearby(?, ?, ?) as nearby, problem - where nearby.problem_id = problem.id - and state in ('confirmed', 'fixed') and photo is not null - $site_restriction - order by confirmed desc limit $num", $e, $n, $dist); + #my $key = "recent_photos:$site_key:$num:$e:$n:$dist"; + #$probs = Memcached::get($key); + #unless ($probs) { + $probs = select_all("select id, title + from problem_find_nearby(?, ?, ?) as nearby, problem + where nearby.problem_id = problem.id + and state in ('confirmed', 'fixed') and photo is not null + $site_restriction + order by confirmed desc limit $num", $e, $n, $dist); + # Memcached::set($key, $probs, 3600); + #} } else { - $probs = select_all("select id, title from problem - where state in ('confirmed', 'fixed') and photo is not null - $site_restriction - order by confirmed desc limit $num"); + #my $key = "recent_photos:$site_key:$num"; + #$probs = Memcached::get($key); + #unless ($probs) { + $probs = select_all("select id, title from problem + where state in ('confirmed', 'fixed') and photo is not null + $site_restriction + order by confirmed desc limit $num"); + # Memcached::set($key, $probs, 3600); + #} } my $out = ''; foreach (@$probs) { @@ -80,30 +115,38 @@ sub recent_photos { } sub recent { - select_all("select id,title from problem - where state in ('confirmed', 'fixed') - $site_restriction - order by confirmed desc limit 5"); + my $result; + #my $key = "recent:$site_key"; + #$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 current_on_map { +sub around_map { my ($min_e, $max_e, $min_n, $max_n) = @_; select_all( - "select id,title,easting,northing from problem where state='confirmed' - and easting>=? and easting<? and northing>=? and northing<? + "select id,title,easting,northing,state from problem + where state in ('confirmed', 'fixed') + and easting>=? and easting<? and northing>=? and northing<? $site_restriction order by created desc", $min_e, $max_e, $min_n, $max_n); } -sub current_nearby { +sub nearby { my ($dist, $ids, $limit, $mid_e, $mid_n) = @_; select_all( - "select id, title, easting, northing, distance + "select id, title, easting, northing, distance, state from problem_find_nearby(?, ?, $dist) as nearby, problem where nearby.problem_id = problem.id - and state = 'confirmed'" . ($ids ? ' and id not in (' . $ids . ')' : '') . " + and state in ('confirmed', 'fixed')" . ($ids ? ' and id not in (' . $ids . ')' : '') . " $site_restriction order by distance, created desc limit $limit", $mid_e, $mid_n); } @@ -115,7 +158,7 @@ sub fixed_nearby { from problem_find_nearby(?, ?, $dist) as nearby, problem where nearby.problem_id = problem.id and state='fixed' $site_restriction - order by created desc limit 9", $mid_e, $mid_n); + order by lastupdate desc", $mid_e, $mid_n); } # Fetch an individual problem |