diff options
Diffstat (limited to 'perllib/FixMyStreet/Script/Reports.pm')
-rw-r--r-- | perllib/FixMyStreet/Script/Reports.pm | 82 |
1 files changed, 49 insertions, 33 deletions
diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm index 2b558fdc0..3e9b2d693 100644 --- a/perllib/FixMyStreet/Script/Reports.pm +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -7,69 +7,89 @@ use FixMyStreet::DB; use FixMyStreet::Queue::Item::Report; has verbose => ( is => 'ro' ); -has debug_mode => ( is => 'ro' ); -has debug_unsent_count => ( is => 'rw', default => 0 ); has unconfirmed_data => ( is => 'ro', default => sub { {} } ); has test_data => ( is => 'ro', default => sub { {} } ); # Static method, used by send-reports cron script and tests. # Creates a manager object from provided data and processes it. -sub send(;$) { - my ($site_override) = @_; - my $rs = FixMyStreet::DB->resultset('Problem'); - - # Set up site, language etc. - my ($verbose, $nomail, $debug_mode) = CronFns::options(); +sub send { + my ($verbose, $nomail, $debug) = @_; my $manager = __PACKAGE__->new( verbose => $verbose, - debug_mode => $debug_mode, ); - my $base_url = FixMyStreet->config('BASE_URL'); - my $site = $site_override || CronFns::site($base_url); - + my $site = CronFns::site(FixMyStreet->config('BASE_URL')); my $states = [ FixMyStreet::DB::Result::Problem::open_states() ]; $states = [ 'submitted', 'confirmed', 'in progress', 'feedback pending', 'external', 'wish' ] if $site eq 'zurich'; - my $unsent = $rs->search( { + + # Devolved Noop categories (unlikely to be any, but still) + my @noop_params; + my $noop_cats = FixMyStreet::DB->resultset('Contact')->search({ + 'body.can_be_devolved' => 1, + 'me.send_method' => 'Noop' + }, { join => 'body' }); + while (my $cat = $noop_cats->next) { + push @noop_params, [ + \[ "NOT regexp_split_to_array(bodies_str, ',') && ?", [ {} => [ $cat->body_id ] ] ], + category => { '!=' => $cat->category } ]; + } + + # Noop bodies + my @noop_bodies = FixMyStreet::DB->resultset('Body')->search({ send_method => 'Noop' })->all; + @noop_bodies = map { $_->id } @noop_bodies; + push @noop_params, \[ "NOT regexp_split_to_array(bodies_str, ',') && ?", [ {} => \@noop_bodies ] ]; + + my $params = { state => $states, whensent => undef, bodies_str => { '!=', undef }, - } ); + -and => \@noop_params, + }; + if (!$debug) { + $params->{'-or'} = [ + send_fail_count => 0, + { send_fail_count => 1, send_fail_timestamp => { '<', \"current_timestamp - '5 minutes'::interval" } }, + { send_fail_timestamp => { '<', \"current_timestamp - '30 minutes'::interval" } }, + ]; + } + + my $unsent = FixMyStreet::DB->resultset('Problem')->search($params); - $manager->debug_print("starting to loop through unsent problem reports..."); + $manager->log("starting to loop through unsent problem reports..."); + my $unsent_count = 0; while (my $row = $unsent->next) { + $unsent_count++; my $item = FixMyStreet::Queue::Item::Report->new( report => $row, manager => $manager, + verbose => $verbose, nomail => $nomail, - debug_mode => $debug_mode, ); $item->process; } - $manager->end_debug_line; + $manager->end_line($unsent_count); $manager->end_summary_unconfirmed; return $manager->test_data; } -sub end_debug_line { - my $self = shift; - return unless $self->debug_mode; +sub end_line { + my ($self, $unsent_count) = @_; + return unless $self->verbose; - print "\n"; - if ($self->debug_unsent_count) { - $self->debug_print("processed all unsent reports (total: " . $self->debug_unsent_count . ")"); + if ($unsent_count) { + $self->log("processed all unsent reports (total: $unsent_count)"); } else { - $self->debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); + $self->log("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); } } sub end_summary_unconfirmed { my $self = shift; - return unless $self->verbose || $self->debug_mode; + return unless $self->verbose; my %unconfirmed_data = %{$self->unconfirmed_data}; print "Council email addresses that need checking:\n" if keys %unconfirmed_data; @@ -102,14 +122,10 @@ sub end_summary_failures { } } -sub debug_print { - my $self = shift; - return unless $self->debug_mode; - - my $msg = shift; - my $id = shift || ''; - $id = "report $id: " if $id; - print "[] $id$msg\n"; +sub log { + my ($self, $msg) = @_; + return unless $self->verbose; + STDERR->print("[fmsd] $msg\n"); } 1; |