aboutsummaryrefslogtreecommitdiffstats
path: root/bin/update-all-reports
blob: f6cd34210c90c8802e2ecb127ae0421b4a0a87d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env 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;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../setenv.pl";
}

use FixMyStreet::App;
use File::Path ();
use File::Slurp;
use JSON;
use List::MoreUtils qw(zip);

my $fourweeks = 4*7*24*60*60;

# Age problems from when they're confirmed, except on Zurich
# where they appear as soon as they're created.
my $age_column = 'confirmed';
if ( FixMyStreet->config('BASE_URL') =~ /zurich|zueri/ ) {
    $age_column = 'created';
}

my $problems = FixMyStreet::App->model("DB::Problem")->search(
    {
        state => [ FixMyStreet::DB::Result::Problem->visible_states() ],
    },
    {
        columns => [
            'id', 'bodies_str', 'state', 'areas',
            { duration => { extract => "epoch from current_timestamp-lastupdate" } },
            { age      => { extract => "epoch from current_timestamp-$age_column"  } },
        ]
    }
);
$problems = $problems->cursor; # Raw DB cursor for speed

my ( %fixed, %open );
my @cols = ( 'id', 'bodies_str', 'state', 'areas', 'duration', 'age' );
while ( my @problem = $problems->next ) {
    my %problem = zip @cols, @problem;
    my @bodies;
    if ( !$problem{bodies_str} ) {
        # Problem was not sent to any bodies, add to all areas
        @bodies = grep { $_ } split( /,/, $problem{areas} );
        $problem{bodies} = 0;
    } else {
        # Add to bodies it was sent to
        (my $bodies = $problem{bodies_str}) =~ s/\|.*$//;
        @bodies = split( /,/, $bodies );
        $problem{bodies} = scalar @bodies;
    }
    foreach my $body ( @bodies ) {
        my $duration_str = ( $problem{duration} > 2 * $fourweeks ) ? 'old' : 'new';
        my $type = ( $problem{duration} > 2 * $fourweeks )
            ? 'unknown'
            : ($problem{age} > $fourweeks ? 'older' : 'new');
        if (FixMyStreet::DB::Result::Problem->fixed_states()->{$problem{state}} || FixMyStreet::DB::Result::Problem->closed_states()->{$problem{state}}) {
            # Fixed problems are either old or new
            $fixed{$body}{$duration_str}++;
        } else {
            # Open problems are either unknown, older, or new
            $open{$body}{$type}++;
        }
    }
}

if ( FixMyStreet->config('BASE_URL') =~ /emptyhomes/ ) {
    my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('emptyhomes')->new();
    my $stats = $cobrand->old_site_stats;
    foreach (keys %$stats) {
        $open{$_}{unknown} += $stats->{$_};
    }
}

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 );