diff options
Diffstat (limited to 'perllib/FixMyStreet')
| -rw-r--r-- | perllib/FixMyStreet/Cobrand/Bexley.pm | 9 | ||||
| -rw-r--r-- | perllib/FixMyStreet/Queue/Item/Report.pm | 43 | ||||
| -rw-r--r-- | perllib/FixMyStreet/Script/Reports.pm | 82 | ||||
| -rw-r--r-- | perllib/FixMyStreet/SendReport.pm | 15 | ||||
| -rw-r--r-- | perllib/FixMyStreet/SendReport/Noop.pm | 5 | 
5 files changed, 74 insertions, 80 deletions
| diff --git a/perllib/FixMyStreet/Cobrand/Bexley.pm b/perllib/FixMyStreet/Cobrand/Bexley.pm index 74a7b5032..481926e72 100644 --- a/perllib/FixMyStreet/Cobrand/Bexley.pm +++ b/perllib/FixMyStreet/Cobrand/Bexley.pm @@ -172,6 +172,12 @@ sub open311_post_send {              $p1_email = 1;              $outofhours_email = 1;          } +    } elsif ($row->category eq 'Street cleaning and litter') { +        my $reportType = $row->get_extra_field_value('reportType') || ''; +        if ($reportType eq 'Oil spillage' || $dangerous eq 'Yes') { +            $p1_email = 1; +            $outofhours_email = 1; +        }      } elsif ($row->category eq 'Damage to kerb' || $row->category eq 'Damaged road' || $row->category eq 'Damaged pavement') {          $p1_email = 1;          $outofhours_email = 1; @@ -181,7 +187,8 @@ sub open311_post_send {      }      my @to; -    push @to, email_list($emails->{p1}, 'Bexley P1 email') if $p1_email; +    my $p1_email_to_use = ($contact->email =~ /^Confirm/) ? $emails->{p1confirm} : $emails->{p1}; +    push @to, email_list($p1_email_to_use, 'Bexley P1 email') if $p1_email;      push @to, email_list($emails->{lighting}, 'FixMyStreet Bexley Street Lighting') if $lighting{$row->category};      push @to, email_list($emails->{flooding}, 'FixMyStreet Bexley Flooding') if $flooding{$row->category};      push @to, email_list($emails->{outofhours}, 'Bexley out of hours') if $outofhours_email && _is_out_of_hours(); diff --git a/perllib/FixMyStreet/Queue/Item/Report.pm b/perllib/FixMyStreet/Queue/Item/Report.pm index 1ef640fc4..4d0d62752 100644 --- a/perllib/FixMyStreet/Queue/Item/Report.pm +++ b/perllib/FixMyStreet/Queue/Item/Report.pm @@ -41,19 +41,17 @@ has h => ( is => 'rwp' );  has reporters => ( is => 'rwp' );  # Run parameters +has verbose => ( is => 'ro');  has nomail => ( is => 'ro' ); -has debug_mode => ( is => 'ro' );  sub process {      my $self = shift;      FixMyStreet::DB->schema->cobrand($self->cobrand); -    if ($self->debug_mode) { -        $self->manager->debug_unsent_count++; -        print "\n"; +    if ($self->verbose) {          my $row = $self->report; -        $self->debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id); +        $self->log("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""));      }      # Cobranded and non-cobranded messages can share a database. In this case, the conf file @@ -61,7 +59,7 @@ sub process {      # more than once if there are multiple vhosts running off the same database. The email_host      # call checks if this is the host that sends mail for this cobrand.      if (! $self->cobrand->email_host()) { -        $self->debug_print("skipping because this host does not send reports for cobrand " . $self->cobrand->moniker, $self->report->id); +        $self->log("skipping because this host does not send reports for cobrand " . $self->cobrand->moniker);          return;      } @@ -79,11 +77,11 @@ sub _check_abuse {      my $self = shift;      if ( $self->report->is_from_abuser) {          $self->report->update( { state => 'hidden' } ); -        $self->debug_print("hiding because its sender is flagged as an abuser", $self->report->id); +        $self->log("hiding because its sender is flagged as an abuser");          return;      } elsif ( $self->report->title =~ /app store test/i ) {          $self->report->update( { state => 'hidden' } ); -        $self->debug_print("hiding because it is an app store test message", $self->report->id); +        $self->log("hiding because it is an app store test message");          return;      }      return 1; @@ -173,33 +171,25 @@ sub _create_reporters {      my @dear;      my %reporters = (); -    my $skip = 0;      while (my $body = $bodies->next) {          my $sender_info = $self->cobrand->get_body_sender( $body, $row->category );          my $sender = "FixMyStreet::SendReport::" . $sender_info->{method};          if ( ! exists $self->senders->{ $sender } ) { -            warn sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id; +            $self->log(sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id);              next;          }          $reporters{ $sender } ||= $sender->new(); -        if ( $reporters{ $sender }->should_skip( $row, $self->debug_mode ) ) { -            $skip = 1; -            $self->debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id); -        } else { -            $self->debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $sender_info->{method}, $row->id); -            push @dear, $body->name; -            $reporters{ $sender }->add_body( $body, $sender_info->{config} ); -        } +        $self->log("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $sender_info->{method}); +        push @dear, $body->name; +        $reporters{ $sender }->add_body( $body, $sender_info->{config} );      }      unless ( keys %reporters ) {          die 'Report not going anywhere for ID ' . $row->id . '!';      } -    return if $skip; -      my $h = $self->h;      $h->{bodies_name} = join(_(' and '), @dear);      if ($h->{category} eq _('Other')) { @@ -228,7 +218,7 @@ sub _send {      my $result = -1;      for my $sender ( keys %{$self->reporters} ) { -        $self->debug_print("sending using " . $sender, $self->report->id); +        $self->log("sending using " . $sender);          $sender = $self->reporters->{$sender};          my $res = $sender->send( $self->report, $self->h );          $result *= $res; @@ -261,7 +251,7 @@ sub _post_send {              $self->h->{sent_confirm_id_ref} = $self->report->$send_confirmation_email;              $self->_send_report_sent_email;          } -        $self->debug_print("send successful: OK", $self->report->id); +        $self->log("send successful: OK");      } else {          my @errors;          for my $sender ( keys %{$self->reporters} ) { @@ -270,7 +260,7 @@ sub _post_send {              }          }          $self->report->update_send_failed( join( '|', @errors ) ); -        $self->debug_print("send FAILED: " . join( '|', @errors ), $self->report->id); +        $self->log("send FAILED: " . join( '|', @errors ));      }  } @@ -297,9 +287,10 @@ sub _send_report_sent_email {      );  } -sub debug_print { -    my $self = shift; -    $self->manager->debug_print(@_); +sub log { +    my ($self, $msg) = @_; +    return unless $self->verbose; +    STDERR->print("[fmsd] [" . $self->report->id . "] $msg\n");  }  1; 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; diff --git a/perllib/FixMyStreet/SendReport.pm b/perllib/FixMyStreet/SendReport.pm index 0d8bea304..c08a7ddbe 100644 --- a/perllib/FixMyStreet/SendReport.pm +++ b/perllib/FixMyStreet/SendReport.pm @@ -18,21 +18,6 @@ has 'error' => ( is => 'rw', isa => Str, default => '' );  has 'unconfirmed_data' => ( 'is' => 'rw', isa => HashRef, default => sub { {} } ); -sub should_skip { -    my $self  = shift; -    my $row   = shift; -    my $debug = shift; - -    return 0 unless $row->send_fail_count; -    return 0 if $debug; - -    my $now = DateTime->now( time_zone => FixMyStreet->local_time_zone ); -    my $diff = $now - $row->send_fail_timestamp; - -    my $backoff = $row->send_fail_count > 1 ? 30 : 5; -    return $diff->in_units( 'minutes' ) < $backoff; -} -  sub get_senders {      my $self = shift; diff --git a/perllib/FixMyStreet/SendReport/Noop.pm b/perllib/FixMyStreet/SendReport/Noop.pm index 60edda373..933291b7c 100644 --- a/perllib/FixMyStreet/SendReport/Noop.pm +++ b/perllib/FixMyStreet/SendReport/Noop.pm @@ -4,9 +4,4 @@ use Moo;  BEGIN { extends 'FixMyStreet::SendReport'; } -# Always skip when using this method -sub should_skip { -    return 1; -} -  1; | 
