aboutsummaryrefslogtreecommitdiffstats
path: root/bin/update-all-reports
blob: 087c59d06e2657ea3b31cb9cb7d91e88ac22b467 (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
#!/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;

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

use FixMyStreet::DB;
use FixMyStreet::Script::UpdateAllReports;
use Path::Tiny;
use Getopt::Long::Descriptive;
use JSON::MaybeXS;

my ($opt, $usage) = describe_options(
    '%c %o',
    [ 'table', "Output JSON for old table-style page." ],
    [ 'body=i', "Restrict results to a particular body (dashboard-style)." ],
    [ 'all-bodies', "Generate set of results for all bodies." ],
    [ 'areas', "Include area IDs in output JSON (table-style)." ],
    [ 'help', "print usage message and exit", { shortcircuit => 1 } ],
);
print($usage->text), exit if $opt->help;

my ($data, $filename);
if ($opt->table) {
    $data = FixMyStreet::Script::UpdateAllReports::generate($opt->areas);
    output('all-reports', $data);
} elsif ($opt->all_bodies) {
    my $bodies = FixMyStreet::DB->resultset("Body")->active;
    while (my $body = $bodies->next) {
        next unless $body->body_areas->first;
        my $data = FixMyStreet::Script::UpdateAllReports::generate_dashboard($body);
        output("all-reports-dashboard-" . $body->id, $data);
    }
} elsif (my $body_id = $opt->body) {
    my $body = FixMyStreet::DB->resultset("Body")->find({ id => $body_id });
    die "Could not find body $body_id" unless $body;
    $data = FixMyStreet::Script::UpdateAllReports::generate_dashboard($body);
    output("all-reports-dashboard-$body_id", $data);
} else {
    $data = FixMyStreet::Script::UpdateAllReports::generate_dashboard();
    output("all-reports-dashboard", $data);
}

sub output {
    my ($filename, $data) = @_;
    my $json = encode_json($data);
    path(FixMyStreet->path_to('../data/'))->mkpath;
    path(FixMyStreet->path_to("../data/$filename.json"))->spew_utf8($json);
}