#!/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); }