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
|
package FixMyStreet::DB::ResultSet::Problem;
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;
}
# Front page statistics
sub recent_fixed {
my $rs = shift;
my $key = "recent_fixed:$site_key";
my $result = Memcached::get($key);
unless ($result) {
$result = $rs->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->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->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 ) = @_;
my $prefetch =
FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ?
[ qw/user/ ] :
[];
return $rs->search(
{
-or => {
created => { '>=', \"ms_current_timestamp()-'7 days'::interval" },
confirmed => { '>=', \"ms_current_timestamp()-'7 days'::interval" },
whensent => { '>=', \"ms_current_timestamp()-'7 days'::interval" },
%{ $restriction },
}
},
{
prefetch => $prefetch,
}
);
}
sub summary_count {
my ( $rs, $restriction ) = @_;
return $rs->search(
$restriction,
{
group_by => ['state'],
select => [ 'state', { count => 'id' } ],
as => [qw/state state_count/]
}
);
}
1;
|