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

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

Make CGIs use shared error functions.

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