[219] | 1 | package NPEMap::Postcodes; |
---|
| 2 | |
---|
| 3 | use vars qw(@ISA @EXPORT); |
---|
| 4 | use warnings; |
---|
| 5 | use strict; |
---|
| 6 | |
---|
| 7 | use vars qw($tt_config); |
---|
| 8 | |
---|
| 9 | use Template; |
---|
| 10 | use NPEMap::Config; |
---|
[333] | 11 | use Date::Format; |
---|
[219] | 12 | |
---|
| 13 | require Exporter; |
---|
| 14 | @ISA = qw(Exporter); |
---|
[333] | 15 | @EXPORT = qw(build_home_stats missing_district_stats); |
---|
[219] | 16 | |
---|
| 17 | sub _getAllStats { |
---|
| 18 | my $dbh = shift; |
---|
| 19 | my $sql = <<"HERE"; |
---|
| 20 | SELECT COUNT(*) AS all, |
---|
| 21 | SUM(CASE WHEN source = 0 THEN 1 ELSE 0 END) AS us, |
---|
| 22 | COUNT(DISTINCT outward) AS all_out, |
---|
| 23 | COUNT(DISTINCT CASE WHEN source = 0 THEN outward ELSE '' END) - 1 AS us_out |
---|
| 24 | FROM postcodes WHERE deleted = 'f'; |
---|
| 25 | HERE |
---|
| 26 | |
---|
| 27 | my $sth = $dbh->prepare($sql); |
---|
| 28 | |
---|
| 29 | $sth->execute(); |
---|
| 30 | |
---|
| 31 | return $sth->fetchrow_hashref; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | sub _getDateStats { |
---|
| 35 | my $dbh = shift; |
---|
| 36 | my $sql = <<"HERE"; |
---|
| 37 | SELECT TO_CHAR(DATE_TRUNC('day', created_at), 'YYYY-MM-DD') AS day, |
---|
| 38 | COUNT(*) AS all, |
---|
| 39 | SUM(CASE WHEN source=0 THEN 1 ELSE 0 END) AS us |
---|
| 40 | FROM postcodes WHERE deleted ='f' |
---|
| 41 | GROUP BY date_trunc('day', created_at) |
---|
| 42 | ORDER BY date_trunc('day', created_at); |
---|
| 43 | |
---|
| 44 | HERE |
---|
| 45 | |
---|
| 46 | my $sth = $dbh->prepare($sql); |
---|
| 47 | $sth->execute(); |
---|
[235] | 48 | return $sth->fetchall_arrayref({}); |
---|
[219] | 49 | } |
---|
| 50 | |
---|
[235] | 51 | sub _getLastPostcodes { |
---|
| 52 | my $dbh = shift; |
---|
| 53 | my $num = shift; |
---|
| 54 | my $sql = <<"HERE"; |
---|
| 55 | SELECT easting, northing, outward, inward, TO_CHAR(created_at, 'DDth Mon HH24:MI')AS date |
---|
| 56 | FROM postcodes |
---|
| 57 | WHERE NOT deleted |
---|
| 58 | ORDER BY created_at DESC LIMIT ? |
---|
| 59 | HERE |
---|
| 60 | |
---|
| 61 | my $sth = $dbh->prepare($sql); |
---|
| 62 | $sth->execute($num); |
---|
| 63 | return $sth->fetchall_arrayref({}); |
---|
| 64 | } |
---|
| 65 | |
---|
[219] | 66 | =pod |
---|
| 67 | |
---|
| 68 | =head build_home_stats |
---|
| 69 | |
---|
| 70 | build_home_stats takes a database handle as a parameter and builds the |
---|
| 71 | HTML stats for inclusion into the home page |
---|
| 72 | |
---|
| 73 | =cut |
---|
| 74 | |
---|
| 75 | sub build_home_stats { |
---|
| 76 | my $dbh = shift; |
---|
| 77 | my $template = Template->new($tt_config); |
---|
| 78 | |
---|
| 79 | my $allStats = _getAllStats($dbh); |
---|
[247] | 80 | my $lastPostcodes = _getLastPostcodes($dbh, 10); |
---|
[219] | 81 | |
---|
| 82 | my $vars = { |
---|
| 83 | allstats => $allStats, |
---|
[235] | 84 | lastpostcodes => $lastPostcodes |
---|
[219] | 85 | }; |
---|
[230] | 86 | |
---|
| 87 | $template->process('home_stats.tt', $vars, $staticdir . '/stats.html') |
---|
| 88 | or warn $template->error(); |
---|
[219] | 89 | } |
---|
| 90 | |
---|
[333] | 91 | sub missing_district_stats { |
---|
| 92 | my $dbh = shift; |
---|
| 93 | my $template = Template->new($tt_config); |
---|
| 94 | |
---|
| 95 | my $sql = <<EOF; |
---|
| 96 | SELECT d.outward, d.district, d.county FROM districts AS d LEFT OUTER JOIN postcodes AS p ON (d.outward = p.outward) |
---|
| 97 | WHERE p.id IS NULL |
---|
| 98 | ORDER BY d.outward |
---|
| 99 | EOF |
---|
| 100 | |
---|
| 101 | my $sth = $dbh->prepare($sql); |
---|
| 102 | $sth->execute or die $dbh->errstr; |
---|
| 103 | my $districts = $sth->fetchall_arrayref({}); |
---|
| 104 | |
---|
| 105 | my $vars = { |
---|
| 106 | districts => $districts, |
---|
| 107 | updated => ctime(time) |
---|
| 108 | }; |
---|
| 109 | |
---|
| 110 | $template->process('missing_district_stats.tt', $vars, $staticdir . '/stats/missing_district_stats.html') |
---|
| 111 | or warn $template->error(); |
---|
| 112 | } |
---|
| 113 | |
---|
[219] | 114 | 1; |
---|