diff options
author | Matthew Somerville <matthew@mysociety.org> | 2011-06-16 12:15:03 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2011-06-16 12:15:03 +0100 |
commit | 4a7912b97c75d8934bbab33532f0b6ac1e86e47f (patch) | |
tree | 3645cd9bd55c77b71adbe7a8fc74bcc58202ba01 /bin | |
parent | 4cbadaa9cd86ff3c0a82ccb696d115c2496b287a (diff) |
Generate /reports data on cron.
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/update-all-reports | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/bin/update-all-reports b/bin/update-all-reports new file mode 100755 index 000000000..2263a3d9d --- /dev/null +++ b/bin/update-all-reports @@ -0,0 +1,69 @@ +#!/usr/bin/perl + +# update-all-reports: +# Generate the data for the /reports page +# +# Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org. WWW: http://www.mysociety.org + +use strict; +use warnings; +require 5.8.0; + +use FixMyStreet::App; +use File::Path (); +use File::Slurp; +use JSON; +use List::MoreUtils qw(zip); + +my $fourweeks = 4*7*24*60*60; + +my $problems = FixMyStreet::App->model("DB::Problem")->search( + { + state => [ 'confirmed', 'fixed' ] + }, + { + columns => [ + 'id', 'council', 'state', 'areas', + { duration => { extract => "epoch from current_timestamp-lastupdate" } }, + { age => { extract => "epoch from current_timestamp-confirmed" } }, + ] + } +); +$problems = $problems->cursor; # Raw DB cursor for speed + +my ( %fixed, %open ); +my @cols = ( 'id', 'council', 'state', 'areas', 'duration', 'age' ); +while ( my @problem = $problems->next ) { + my %problem = zip @cols, @problem; + my @areas; + if ( !$problem{council} ) { + # Problem was not sent to any council, add to all areas + @areas = grep { $_ } split( /,/, $problem{areas} ); + $problem{councils} = 0; + } else { + # Add to councils it was sent to + (my $council = $problem{council}) =~ s/\|.*$//; + @areas = split( /,/, $council ); + $problem{councils} = scalar @areas; + } + foreach my $council ( @areas ) { + my $duration_str = ( $problem{duration} > 2 * $fourweeks ) ? 'old' : 'new'; + my $type = ( $problem{duration} > 2 * $fourweeks ) + ? 'unknown' + : ($problem{age} > $fourweeks ? 'older' : 'new'); + # Fixed problems are either old or new + $fixed{$council}{$duration_str}++ if $problem{state} eq 'fixed'; + # Open problems are either unknown, older, or new + $open{$council}{$type}++ if $problem{state} eq 'confirmed'; + } +} + +my $body = JSON->new->utf8(1)->encode( { + fixed => \%fixed, + open => \%open, +} ); + +File::Path::mkpath( FixMyStreet->path_to( '../data/' )->stringify ); +File::Slurp::write_file( FixMyStreet->path_to( '../data/all-reports.json' )->stringify, \$body ); + |