source: trunk/npemap.org.uk/cgi/get-distant-postcodes.fcgi @ 479

Last change on this file since 479 was 479, checked in by Dominic Hargreaves, 13 years ago

Tweak distances for time being

  • Property svn:executable set to *
File size: 3.4 KB
Line 
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# TODO: Make PostGreSQL temp tables + FastCGI play nicely together
9#       For now, we have a race condition related to the "temp" table
10
11use strict;
12use warnings;
13
14use CGI::Fast qw/:standard -debug/;
15
16# Find our private perl libraries
17use FindBin;
18use lib "$FindBin::Bin/../perllib";
19use NPEMap;
20
21sub print_err;
22
23# Set up database handler to try and make sure it's ready for the first
24# request
25# No point in handling errors here since they'll get handled by the request
26# handler
27my $dbh = setup_dbh();
28
29
30my $cgi;
31# Process incoming requests
32REQUEST: while ($cgi = new CGI::Fast) {
33
34    # In case the database went away, make sure we have a connection
35    unless ($dbh = setup_dbh()) {
36        print_internal_err('Error setting up database connection');
37        next REQUEST;
38    }
39
40    # Input validation
41        my $doing_outer1 = 0;
42    if (defined $cgi->param("outer1") || defined $cgi->param("outerone")) {
43        $doing_outer1 = 1;
44    }
45
46        # How big a distance before we flag it?
47        my $flag_distance = 100 * 1000;
48        if($doing_outer1) {
49                $flag_distance = 25 * 1000;
50        }
51
52        my $pcpart = "outward";
53        my $pcgroup = "outward";
54        if($doing_outer1) {
55                $pcpart = "outward || ' ' || substr(inward,1,1)";
56                $pcgroup = "outward, substr(inward,1,1)";
57        }
58
59        # Tidy up from another run, if required
60        my $checksql = "SELECT tablename FROM pg_tables WHERE tablename = 'averages'";
61        my @has_table = $dbh->selectrow_array($checksql);
62        if(@has_table) {
63                $dbh->do("DROP TABLE averages;");
64        }
65
66        # Get our sql
67        # First calculate the average
68        my $prepsql .= "SELECT $pcpart AS pcpart, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing INTO TEMP TABLE averages FROM postcodes WHERE NOT deleted GROUP BY $pcgroup ";
69
70        # Then join to find problem ones
71        my $sql .= "SELECT postcodes.id, outward || ' ' || inward AS postcode, $pcpart AS pcpart, easting, northing, avg_easting, avg_northing, source, sources.name AS source_name ";
72        $sql .= "FROM postcodes ";
73        $sql .= "INNER JOIN averages ON ($pcpart = pcpart) ";
74        $sql .= "INNER JOIN sources ON (postcodes.source = sources.id) ";
75        $sql .= "WHERE NOT deleted AND (";
76        $sql .= "       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, source id, source\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, $row[7], '$row[8]');\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
112sub print_err {
113    my $err = shift;
114    print header("text/plain", "400 $err");
115    print "Error: $err\n";
116}
117
118sub print_internal_err {
119    my $err = shift;
120    print header("text/plain", "500 $err");
121    print "Error: $err\n";
122}
Note: See TracBrowser for help on using the repository browser.