1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # Copyright (c) 2006 Dominic Hargreaves |
---|
4 | # See accompanying file "LICENCE" for licence details |
---|
5 | # |
---|
6 | # Find postcodes that seem to be a long way from their friends |
---|
7 | |
---|
8 | use strict; |
---|
9 | use warnings; |
---|
10 | |
---|
11 | use DBI; |
---|
12 | use CGI::Fast qw/:standard -debug/; |
---|
13 | |
---|
14 | use vars qw($dbname $dbhost $dbuser $dbpass); |
---|
15 | |
---|
16 | sub print_err; |
---|
17 | sub setup_dbh; |
---|
18 | |
---|
19 | # Read in database config |
---|
20 | my $config = 'npemap.conf'; |
---|
21 | do $config or die "Can't read $config!\n"; |
---|
22 | |
---|
23 | |
---|
24 | # Set up database handler to try and make sure it's ready for the first |
---|
25 | # request |
---|
26 | # No point in handling errors here since they'll get handled by the request |
---|
27 | # handler |
---|
28 | my $dbh; |
---|
29 | setup_dbh(); |
---|
30 | |
---|
31 | |
---|
32 | my $cgi; |
---|
33 | # Process incoming requests |
---|
34 | REQUEST: while ($cgi = new CGI::Fast) { |
---|
35 | |
---|
36 | # In case the database went away, make sure we have a connection |
---|
37 | unless (setup_dbh()) { |
---|
38 | print_internal_err('Error setting up database connection'); |
---|
39 | next REQUEST; |
---|
40 | } |
---|
41 | |
---|
42 | # Input validation |
---|
43 | my $doing_outer1 = 0; |
---|
44 | if (defined $cgi->param("outer1") || defined $cgi->param("outerone")) { |
---|
45 | $doing_outer1 = 1; |
---|
46 | } |
---|
47 | |
---|
48 | # How big a distance before we flag it? |
---|
49 | # 10km / 10km |
---|
50 | my $flag_distance = 100 * 1000; |
---|
51 | if($doing_outer1) { |
---|
52 | $flag_distance = 10 * 1000; |
---|
53 | } |
---|
54 | |
---|
55 | my $pcpart = "outward"; |
---|
56 | my $pcgroup = "outward"; |
---|
57 | if($doing_outer1) { |
---|
58 | $pcpart = "outward || substr(inward,1,1)"; |
---|
59 | $pcgroup = "outward, substr(inward,1,1)"; |
---|
60 | } |
---|
61 | |
---|
62 | # Tidy up from another run, if required |
---|
63 | my $checksql = "SELECT tablename FROM pg_tables WHERE tablename = 'averages'"; |
---|
64 | my @has_table = $dbh->selectrow_array($checksql); |
---|
65 | if(@has_table) { |
---|
66 | $dbh->do("DROP TABLE averages;"); |
---|
67 | } |
---|
68 | |
---|
69 | # Get our sql |
---|
70 | # First calculate the average |
---|
71 | my $prepsql .= "SELECT $pcpart AS pcpart, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing INTO TEMP TABLE averages FROM postcodes GROUP BY $pcgroup "; |
---|
72 | |
---|
73 | # Then join to find problem ones |
---|
74 | my $sql .= "SELECT id, outward || ' ' || inward AS postcode, $pcpart AS pcpart, easting, northing, avg_easting, avg_northing FROM postcodes "; |
---|
75 | $sql .= "INNER JOIN averages ON ($pcpart = pcpart) "; |
---|
76 | $sql .= "WHERE abs(avg_easting-easting) > $flag_distance "; |
---|
77 | $sql .= "OR abs(avg_northing-northing) > $flag_distance "; |
---|
78 | $sql .= "ORDER BY outward, inward "; |
---|
79 | |
---|
80 | # Build the temp table |
---|
81 | $dbh->do($prepsql); |
---|
82 | |
---|
83 | # Do the query |
---|
84 | my $sth = $dbh->prepare($sql); |
---|
85 | if ($sth->execute()) { |
---|
86 | print "Content-type: text/javascript\n\n"; |
---|
87 | |
---|
88 | print "// id, postcode, match part, easting, northing, avg easting, avg northing\n"; |
---|
89 | while(my @row = $sth->fetchrow_array) { |
---|
90 | # Tidy up avg |
---|
91 | my $avg_e = int($row[5]); |
---|
92 | my $avg_n = int($row[6]); |
---|
93 | |
---|
94 | # Print |
---|
95 | print "addProblem($row[0],'$row[1]','$row[2]', $row[3], $row[4], $avg_e, $avg_n);\n"; |
---|
96 | } |
---|
97 | } else { |
---|
98 | print_internal_err("Database error retrieving data"); |
---|
99 | } |
---|
100 | |
---|
101 | # Tidy up |
---|
102 | @has_table = $dbh->selectrow_array($checksql); |
---|
103 | if(@has_table) { |
---|
104 | $dbh->do("DROP TABLE averages"); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | # No more requests to serve, so tidy up |
---|
109 | $dbh->disconnect; |
---|
110 | |
---|
111 | # Helper routines |
---|
112 | sub print_err { |
---|
113 | my $err = shift; |
---|
114 | print header("text/plain", "400 $err"); |
---|
115 | print "Error: $err\n"; |
---|
116 | } |
---|
117 | |
---|
118 | sub print_internal_err { |
---|
119 | my $err = shift; |
---|
120 | print header("text/plain", "500 $err"); |
---|
121 | print "Error: $err\n"; |
---|
122 | } |
---|
123 | |
---|
124 | sub setup_dbh { |
---|
125 | # $dbh is global |
---|
126 | my $data_source = "dbi:Pg:dbname=$dbname"; |
---|
127 | $data_source .= ";host=$dbhost" if $dbhost; |
---|
128 | return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass); |
---|
129 | } |
---|