1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#!/usr/bin/perl
#
# Problems.pm:
# Various problem report database fetching related functions for FixMyStreet.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: Problems.pm,v 1.18 2009-07-01 14:55:35 louise Exp $
#
package Problems;
use strict;
use Memcached;
use mySociety::DBHandle qw/dbh select_all/;
use mySociety::Locale;
use mySociety::Web qw/ent/;
use mySociety::MaPit;
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') "
if $site eq 'scambs';
$site_key = 1;
}
# 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>ms_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);
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;
(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
sub recent_photos {
my ($num, $e, $n, $dist) = @_;
my $probs;
if ($e) {
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 {
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) {
my $title = ent($_->{title});
$out .= '<a href="/report/' . $_->{id} .
'"><img border="0" height="100" src="/photo?tn=1;id=' . $_->{id} .
'" alt="' . $title . '" title="' . $title . '"></a>';
}
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 {
my ($min_e, $max_e, $min_n, $max_n, $interval) = @_;
mySociety::Locale::in_gb_locale { select_all(
"select id,title,easting,northing,state from problem
where state in ('confirmed', 'fixed')
and easting>=? and easting<? and northing>=? and northing<? " .
($interval ? " and ms_current_timestamp()-lastupdate < '$interval'::interval" : '') .
" $site_restriction
order by created desc", $min_e, $max_e, $min_n, $max_n);
};
}
sub nearby {
my ($dist, $ids, $limit, $mid_e, $mid_n, $interval) = @_;
mySociety::Locale::in_gb_locale { select_all(
"select id, title, easting, northing, distance, state
from problem_find_nearby(?, ?, $dist) as nearby, problem
where nearby.problem_id = problem.id " .
($interval ? " and ms_current_timestamp()-lastupdate < '$interval'::interval" : '') .
" and state in ('confirmed', 'fixed')" . ($ids ? ' and id not in (' . $ids . ')' : '') . "
$site_restriction
order by distance, created desc limit $limit", $mid_e, $mid_n);
}
}
sub fixed_nearby {
my ($dist, $mid_e, $mid_n) = @_;
mySociety::Locale::in_gb_locale { select_all(
"select id, title, easting, northing, distance
from problem_find_nearby(?, ?, $dist) as nearby, problem
where nearby.problem_id = problem.id and state='fixed'
$site_restriction
order by lastupdate desc", $mid_e, $mid_n);
}
}
# Fetch an individual problem
sub fetch_problem {
my $id = shift;
dbh()->selectrow_hashref(
"select id, easting, northing, council, category, title, detail, photo,
used_map, name, anonymous, extract(epoch from confirmed) as time,
state, extract(epoch from whensent-confirmed) as whensent,
extract(epoch from ms_current_timestamp()-lastupdate) as duration, service
from problem where id=? and state in ('confirmed','fixed', 'hidden')
$site_restriction", {}, $id
);
}
# API functions
sub problems_matching_criteria{
my ($criteria) = @_;
my $problems = select_all(
"select id, title, council, category, detail, name, anonymous,
confirmed, whensent, service
from problem
$criteria
$site_restriction");
my @councils;
foreach my $problem (@$problems){
if ($problem->{anonymous} == 1){
$problem->{name} = '';
}
if ($problem->{service} eq ''){
$problem->{service} = 'Web interface';
}
if ($problem->{council}) {
$problem->{council} =~ s/\|.*//g;
my @council_ids = split /,/, $problem->{council};
push(@councils, @council_ids);
$problem->{council} = \@council_ids;
}
}
my $areas_info = mySociety::MaPit::get_voting_areas_info(\@councils);
foreach my $problem (@$problems){
if ($problem->{council}) {
my @council_names = map { $areas_info->{$_}->{name}} @{$problem->{council}} ;
$problem->{council} = join(' and ', @council_names);
}
}
return $problems;
}
sub fixed_in_interval {
my ($start_date, $end_date) = @_;
my $criteria = "where state='fixed' and date_trunc('day',lastupdate)>='$start_date' and
date_trunc('day',lastupdate)<='$end_date'";
return problems_matching_criteria($criteria);
}
sub created_in_interval {
my ($start_date, $end_date) = @_;
my $criteria = "where state='confirmed' and date_trunc('day',created)>='$start_date' and
date_trunc('day',created)<='$end_date'";
return problems_matching_criteria($criteria);
}
1;
|