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

Last change on this file since 222 was 222, checked in by Dominic Hargreaves, 15 years ago

Make everything use the modules

  • Property svn:executable set to *
File size: 3.5 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 DBI;
15use CGI::Fast qw/:standard -debug/;
16
17use vars qw($dbname $dbhost $dbuser $dbpass);
18
19# Find our private perl libraries
20use FindBin;
21use lib "$FindBin::Bin/../perllib";
22use NPEMap;
23
24sub print_err;
25
26# Set up database handler to try and make sure it's ready for the first
27# request
28# No point in handling errors here since they'll get handled by the request
29# handler
30my $dbh = setup_dbh();
31
32
33my $cgi;
34# Process incoming requests
35REQUEST: while ($cgi = new CGI::Fast) {
36
37    # In case the database went away, make sure we have a connection
38    unless ($dbh = setup_dbh()) {
39        print_internal_err('Error setting up database connection');
40        next REQUEST;
41    }
42
43    # Input validation
44        my $doing_outer1 = 0;
45    if (defined $cgi->param("outer1") || defined $cgi->param("outerone")) {
46        $doing_outer1 = 1;
47    }
48
49        # How big a distance before we flag it?
50        # 10km / 10km
51        my $flag_distance = 100 * 1000;
52        if($doing_outer1) {
53                $flag_distance = 10 * 1000;
54        }
55
56        my $pcpart = "outward";
57        my $pcgroup = "outward";
58        if($doing_outer1) {
59                $pcpart = "outward || ' ' || substr(inward,1,1)";
60                $pcgroup = "outward, substr(inward,1,1)";
61        }
62
63        # Tidy up from another run, if required
64        my $checksql = "SELECT tablename FROM pg_tables WHERE tablename = 'averages'";
65        my @has_table = $dbh->selectrow_array($checksql);
66        if(@has_table) {
67                $dbh->do("DROP TABLE averages;");
68        }
69
70        # Get our sql
71        # First calculate the average
72        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 ";
73
74        # Then join to find problem ones
75        my $sql .= "SELECT postcodes.id, outward || ' ' || inward AS postcode, $pcpart AS pcpart, easting, northing, avg_easting, avg_northing, source, sources.name AS source_name ";
76        $sql .= "FROM postcodes ";
77        $sql .= "INNER JOIN averages ON ($pcpart = pcpart) ";
78        $sql .= "INNER JOIN sources ON (postcodes.source = sources.id) ";
79        $sql .= "WHERE NOT deleted AND (";
80        $sql .= "       abs(avg_easting-easting) > $flag_distance ";
81        $sql .= "       OR abs(avg_northing-northing) > $flag_distance ";
82        $sql .= ") ORDER BY outward, inward ";
83
84        # Build the temp table
85    $dbh->do($prepsql);
86
87        # Do the query
88    my $sth = $dbh->prepare($sql);
89    if ($sth->execute()) {
90        print "Content-type: text/javascript\n\n";
91
92                print "// id, postcode, match part, easting, northing, avg easting, avg northing, source id, source\n";
93        while(my @row = $sth->fetchrow_array) {
94                        # Tidy up avg
95                        my $avg_e = int($row[5]);
96                        my $avg_n = int($row[6]);
97
98                        # Print
99                        print "addProblem($row[0],'$row[1]','$row[2]', $row[3], $row[4], $avg_e, $avg_n, $row[7], '$row[8]');\n";
100        }
101    } else {
102        print_internal_err("Database error retrieving data");
103    }
104
105        # Tidy up
106        @has_table = $dbh->selectrow_array($checksql);
107        if(@has_table) {
108                $dbh->do("DROP TABLE averages");
109        }
110}
111
112# No more requests to serve, so tidy up
113$dbh->disconnect;
114
115# Helper routines
116sub print_err {
117    my $err = shift;
118    print header("text/plain", "400 $err");
119    print "Error: $err\n";
120}
121
122sub print_internal_err {
123    my $err = shift;
124    print header("text/plain", "500 $err");
125    print "Error: $err\n";
126}
Note: See TracBrowser for help on using the repository browser.