Skip to content

Commit

Permalink
Include same-day data in pre-generated CSV exports
Browse files Browse the repository at this point in the history
If a request for a CSV export includes the current day, stop the
pre-generated export at midnight and switch to a live export for
the current day.
  • Loading branch information
dracos committed Aug 30, 2024
1 parent e86888f commit 6ded89d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion perllib/FixMyStreet/App/Controller/Dashboard.pm
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ sub index : Path : Args(0) {
my $reporting = $c->forward('construct_rs_filter', [ $c->get_param('updates') ]);

if ( my $export = $c->get_param('export') ) {
$reporting->csv_parameters;
if ($export == 1) {
# Existing method, generate and serve
if ($reporting->premade_csv_exists) {
$reporting->filter_premade_csv_http($c);
} else {
$reporting->csv_parameters;
$reporting->generate_csv_http($c);
}
} elsif ($export == 2) {
Expand Down
4 changes: 0 additions & 4 deletions perllib/FixMyStreet/Cobrand/TfL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,6 @@ sub dashboard_export_problems_add_columns {

my @contacts = $csv->body->contacts->order_by('category')->all;
my %extra_columns;
if (@{$csv->category}) {
my %picked_cats = map { $_ => 1} @{$csv->category};
@contacts = grep { $picked_cats{$_->category} } @contacts;
}
foreach my $contact (@contacts) {
foreach (@{$contact->get_metadata_for_storage}) {
next if $_->{code} eq 'safety_critical';
Expand Down
24 changes: 20 additions & 4 deletions perllib/FixMyStreet/Reporting.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ has body => ( is => 'ro', isa => Maybe[InstanceOf['FixMyStreet::DB::Result::Body
has wards => ( is => 'ro', isa => ArrayRef[Int], default => sub { [] } );
has category => ( is => 'ro', isa => ArrayRef[Str], default => sub { [] } );
has state => ( is => 'ro', isa => Maybe[Str] );
has start_date => ( is => 'ro',
has start_date => ( is => 'rwp',
isa => Str,
default => sub {
my $days30 = DateTime->now(time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone)->subtract(days => 30);
Expand Down Expand Up @@ -268,10 +268,10 @@ Generates a CSV output to a file handler provided
=cut

sub generate_csv {
my ($self, $handle) = @_;
my ($self, $handle, $exclude_header) = @_;

my $csv = Text::CSV->new({ binary => 1, eol => "\n" });
$csv->print($handle, $self->csv_headers);
$csv->print($handle, $self->csv_headers) unless $exclude_header;

my $fixed_states = FixMyStreet::DB::Result::Problem->fixed_states;
my $closed_states = FixMyStreet::DB::Result::Problem->closed_states;
Expand Down Expand Up @@ -460,9 +460,17 @@ sub filter_premade_csv {
$state_column = 'DBState';
}

my $add_on_today = 0;
my $today = DateTime->today(time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone);
my $end_date = $self->end_date;
if (!$end_date || $end_date ge $today->strftime('%Y-%m-%d')) {
$add_on_today = 1;
$end_date = $today->subtract(days => 1)->strftime('%Y-%m-%d');
}

my $range = FixMyStreet::DateRange->new(
start_date => $self->start_date,
end_date => $self->end_date,
end_date => $end_date,
formatter => FixMyStreet::DB->schema->storage->datetime_parser,
);

Expand Down Expand Up @@ -511,6 +519,14 @@ sub filter_premade_csv {
# Areas and Roles, that were only included for the filtering above
$csv->print($handle, [ (@{$row}{@$arr})[$first_column..@$arr-$last_column-1] ]);
}

if ($add_on_today) {
# Add in any information from today the 'live' way
$self->_set_start_date($today->strftime('%Y-%m-%d'));
$self->construct_rs_filter;
$self->csv_parameters;
$self->generate_csv($handle, 1);
}
}

# Outputs relevant CSV HTTP headers, and then streams the CSV
Expand Down
2 changes: 1 addition & 1 deletion t/cobrand/tfl.t
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ subtest 'Dashboard CSV extra columns' => sub {
$mech->content_contains(',12345,,no,[email protected],,', "Bike number added to csv");
$mech->content_contains('"Council User",,,98756', "Stop code added to csv for all categories report");
$mech->get_ok('/dashboard?export=1&category=Bus+stops');
$mech->content_contains('"Council User",,98756', "Stop code added to csv for bus stop category report");
$mech->content_contains('"Council User",,,98756', "Stop code added to csv for bus stop category report");

$report->set_extra_fields({ name => 'leaning', value => 'Yes' }, { name => 'safety_critical', value => 'yes' },
{ name => 'stop_code', value => '98756' }, { name => 'Question', value => '12345' });
Expand Down

0 comments on commit 6ded89d

Please sign in to comment.