diff options
Diffstat (limited to 'perllib/FixMyStreet/Script')
-rw-r--r-- | perllib/FixMyStreet/Script/Alerts.pm | 28 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/CreateSuperuser.pm | 20 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Inactive.pm | 59 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Questionnaires.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Reports.pm | 415 | ||||
-rwxr-xr-x | perllib/FixMyStreet/Script/UpdateAllReports.pm | 4 |
7 files changed, 198 insertions, 337 deletions
diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm index 55f4b3db5..cb1f022fa 100644 --- a/perllib/FixMyStreet/Script/Alerts.pm +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -40,6 +40,7 @@ sub send() { $item_table.confirmed as item_confirmed, $item_table.photo as item_photo, $item_table.problem_state as item_problem_state, + $item_table.cobrand as item_cobrand, $head_table.* from alert, $item_table, $head_table where alert.parameter::integer = $head_table.id @@ -47,6 +48,7 @@ sub send() { "; } else { $query .= " $item_table.*, + $item_table.cobrand as item_cobrand, $item_table.id as item_id from alert, $item_table where 1 = 1"; @@ -84,6 +86,8 @@ sub send() { next unless FixMyStreet::DB::Result::Problem::visible_states()->{$row->{state}}; + next if $row->{alert_cobrand} ne 'tfl' && $row->{item_cobrand} eq 'tfl'; + $schema->resultset('AlertSent')->create( { alert_id => $row->{alert_id}, parameter => $row->{item_id}, @@ -97,7 +101,7 @@ sub send() { !( $last_problem_state eq '' && $row->{item_problem_state} eq 'confirmed' ) && $last_problem_state ne $row->{item_problem_state} ) { - my $state = FixMyStreet::DB->resultset("State")->display($row->{item_problem_state}, 1, $cobrand); + my $state = FixMyStreet::DB->resultset("State")->display($row->{item_problem_state}, 1, $cobrand->moniker); my $update = _('State changed to:') . ' ' . $state; $row->{item_text} = $row->{item_text} ? $row->{item_text} . "\n\n" . $update : @@ -121,6 +125,13 @@ sub send() { $data{state_message} = _("This report is currently marked as open."); } + if (!$data{alert_user_id}) { + if ($ref eq 'new_updates') { + # Get a report object for its photo and static map + $data{report} = $schema->resultset('Problem')->find({ id => $row->{id} }); + } + } + my $url = $cobrand->base_url_for_report($row); # this is currently only for new_updates if (defined($row->{item_text})) { @@ -138,6 +149,12 @@ sub send() { } } ); $data{problem_url} = $url . "/R/" . $token_obj->token; + + # Also record timestamp on report if it's an update about being fixed... + if (FixMyStreet::DB::Result::Problem::fixed_states()->{$row->{state}} || FixMyStreet::DB::Result::Problem::closed_states()->{$row->{state}}) { + $data{report}->set_extra_metadata_if_undefined('closure_alert_sent_at', time()); + $data{report}->update; + } } else { $data{problem_url} = $url . "/report/" . $row->{id}; } @@ -181,10 +198,6 @@ sub send() { if (!$data{alert_user_id}) { %data = (%data, %$row); - if ($ref eq 'new_updates') { - # Get a report object for its photo and static map - $data{report} = $schema->resultset('Problem')->find({ id => $row->{id} }); - } if ($ref eq 'area_problems') { my $va_info = FixMyStreet::MapIt::call('area', $row->{alert_parameter}); $data{area_name} = $va_info->{name}; @@ -239,7 +252,7 @@ sub send() { cobrand_data => $alert->cobrand_data, schema => $schema, ); - my $q = "select problem.id, problem.bodies_str, problem.postcode, problem.geocode, problem.confirmed, + my $q = "select problem.id, problem.bodies_str, problem.postcode, problem.geocode, problem.confirmed, problem.cobrand, problem.title, problem.detail, problem.photo from problem_find_nearby(?, ?, ?) as nearby, problem, users where nearby.problem_id = problem.id and problem.user_id = users.id @@ -252,6 +265,8 @@ sub send() { $q = FixMyStreet::DB->schema->storage->dbh->prepare($q); $q->execute($latitude, $longitude, $d, $alert->whensubscribed, $alert->id, $alert->user->email); while (my $row = $q->fetchrow_hashref) { + next if $alert->cobrand ne 'tfl' && $row->{cobrand} eq 'tfl'; + $schema->resultset('AlertSent')->create( { alert_id => $alert->id, parameter => $row->{id}, @@ -333,7 +348,6 @@ sub _get_address_from_geocode { my $geocode = shift; return '' unless defined $geocode; - utf8::encode($geocode) if utf8::is_utf8($geocode); my $h = new IO::String($geocode); my $data = RABX::wire_rd($h); diff --git a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm index 0c938682d..7ba763515 100644 --- a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm +++ b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm @@ -11,6 +11,7 @@ use FixMyStreet::Email; my $opts = { commit => 0, + closed_state => 'closed', }; sub query { @@ -161,10 +162,10 @@ sub close_problems { mark_fixed => 0, anonymous => 0, state => 'confirmed', - problem_state => 'closed', + problem_state => $opts->{closed_state}, extra => $extra, } ); - $problem->update({ state => 'closed', send_questionnaire => 0 }); + $problem->update({ state => $opts->{closed_state}, send_questionnaire => 0 }); next if $opts->{retain_alerts}; diff --git a/perllib/FixMyStreet/Script/CreateSuperuser.pm b/perllib/FixMyStreet/Script/CreateSuperuser.pm index 69d165abb..cbbea577a 100644 --- a/perllib/FixMyStreet/Script/CreateSuperuser.pm +++ b/perllib/FixMyStreet/Script/CreateSuperuser.pm @@ -7,19 +7,27 @@ use FixMyStreet; use FixMyStreet::DB; sub createsuperuser { - die "Specify a single email address and optionally password to create a superuser or grant superuser status to." if (@ARGV < 1 || @ARGV > 2); + my ($email, $password) = @_; - my $user = FixMyStreet::DB->resultset('User')->find_or_new({ email => $ARGV[0] }); + unless ($email) { + warn "Specify a single email address and optionally password to create a superuser or grant superuser status to.\n"; + return 1; + } + + my $user = FixMyStreet::DB->resultset('User')->find_or_new({ email => $email }); if ( !$user->in_storage ) { - die "Specify a password for this new user." if (@ARGV < 2); - $user->password($ARGV[1]); + unless ($password) { + warn "Specify a password for this new user.\n"; + return 1; + } + $user->password($password); $user->is_superuser(1); $user->insert; } else { $user->update({ is_superuser => 1 }); } print $user->email . " is now a superuser.\n"; + return 0; } - -1;
\ No newline at end of file +1; diff --git a/perllib/FixMyStreet/Script/Inactive.pm b/perllib/FixMyStreet/Script/Inactive.pm index 0468d2a52..8dd524ce1 100644 --- a/perllib/FixMyStreet/Script/Inactive.pm +++ b/perllib/FixMyStreet/Script/Inactive.pm @@ -12,17 +12,23 @@ use FixMyStreet::Email; has anonymize => ( is => 'ro' ); has close => ( is => 'ro' ); +has delete => ( is => 'ro' ); has email => ( is => 'ro' ); has verbose => ( is => 'ro' ); has dry_run => ( is => 'ro' ); +has cobrand => ( + is => 'ro', + coerce => sub { FixMyStreet::Cobrand->get_class_for_moniker($_[0])->new }, +); + sub BUILDARGS { my ($cls, %args) = @_; $args{dry_run} = delete $args{'dry-run'}; return \%args; } -has cobrand => ( +has base_cobrand => ( is => 'lazy', default => sub { my $base_url = FixMyStreet->config('BASE_URL'); @@ -55,6 +61,7 @@ sub reports { say "DRY RUN" if $self->dry_run; $self->anonymize_reports if $self->anonymize; + $self->delete_reports if $self->delete; $self->close_updates if $self->close; } @@ -66,6 +73,7 @@ sub close_updates { state => [ FixMyStreet::DB::Result::Problem->closed_states(), FixMyStreet::DB::Result::Problem->fixed_states() ], extra => [ undef, { -not_like => '%closed_updates%' } ], }); + $problems = $problems->search({ cobrand => $self->cobrand->moniker }) if $self->cobrand; while (my $problem = $problems->next) { say "Closing updates on problem #" . $problem->id if $self->verbose; @@ -75,18 +83,28 @@ sub close_updates { } } -sub anonymize_reports { - my $self = shift; - - # Need to look though them all each time, in case any new updates/alerts +sub _relevant_reports { + my ($self, $time) = @_; my $problems = FixMyStreet::DB->resultset("Problem")->search({ - lastupdate => { '<', interval($self->anonymize) }, + lastupdate => { '<', interval($time) }, state => [ FixMyStreet::DB::Result::Problem->closed_states(), FixMyStreet::DB::Result::Problem->fixed_states(), FixMyStreet::DB::Result::Problem->hidden_states(), ], }); + if ($self->cobrand) { + $problems = $problems->search({ cobrand => $self->cobrand->moniker }); + $problems = $self->cobrand->call_hook(inactive_reports_filter => $time, $problems) || $problems; + } + return $problems; +} + +sub anonymize_reports { + my $self = shift; + + # Need to look though them all each time, in case any new updates/alerts + my $problems = $self->_relevant_reports($self->anonymize); while (my $problem = $problems->next) { say "Anonymizing problem #" . $problem->id if $self->verbose; @@ -110,10 +128,8 @@ sub anonymize_reports { }); # Remove alerts - could just delete, but of interest how many there were, perhaps? - FixMyStreet::DB->resultset('Alert')->search({ + $problem->alerts->search({ user_id => { '!=' => $self->anonymous_user->id }, - alert_type => 'new_updates', - parameter => $problem->id, })->update({ user_id => $self->anonymous_user->id, whendisabled => \'current_timestamp', @@ -121,11 +137,30 @@ sub anonymize_reports { } } +sub delete_reports { + my $self = shift; + + my $problems = $self->_relevant_reports($self->delete); + + while (my $problem = $problems->next) { + say "Deleting associated data of problem #" . $problem->id if $self->verbose; + next if $self->dry_run; + + $problem->comments->delete; + $problem->questionnaires->delete; + $problem->alerts->delete; + } + say "Deleting all matching problems" if $self->verbose; + return if $self->dry_run; + $problems->delete; +} + sub anonymize_users { my $self = shift; my $users = FixMyStreet::DB->resultset("User")->search({ last_active => { '<', interval($self->anonymize) }, + email => { -not_like => 'removed-%@' . FixMyStreet->config('EMAIL_DOMAIN') }, }); while (my $user = $users->next) { @@ -154,10 +189,10 @@ sub email_inactive_users { email_from => $self->email, anonymize_from => $self->anonymize, user => $user, - url => $self->cobrand->base_url_with_lang . '/my', + url => $self->base_cobrand->base_url_with_lang . '/my', }, - { To => [ $user->email, $user->name ] }, - undef, 0, $self->cobrand, + { To => [ [ $user->email, $user->name ] ] }, + undef, 0, $self->base_cobrand, ); $user->set_extra_metadata('inactive_email_sent', 1); diff --git a/perllib/FixMyStreet/Script/Questionnaires.pm b/perllib/FixMyStreet/Script/Questionnaires.pm index aab4b9b75..5db66ff7c 100644 --- a/perllib/FixMyStreet/Script/Questionnaires.pm +++ b/perllib/FixMyStreet/Script/Questionnaires.pm @@ -33,8 +33,8 @@ sub send_questionnaires_period { }; $q_params->{'-or'} = [ - '(select max(whensent) from questionnaire where me.id=problem_id)' => undef, - '(select max(whenanswered) from questionnaire where me.id=problem_id)' => { '<', \"current_timestamp - '$period'::interval" } + \'(select max(whensent) from questionnaire where me.id=problem_id) IS NULL', + \"(select max(whenanswered) from questionnaire where me.id=problem_id) < current_timestamp - '$period'::interval", ]; my $unsent = FixMyStreet::DB->resultset('Problem')->search( $q_params, { diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm index ecd461cd9..3d5afe216 100644 --- a/perllib/FixMyStreet/Script/Reports.pm +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -1,340 +1,143 @@ package FixMyStreet::Script::Reports; -use strict; -use warnings; - +use Moo; use CronFns; -use DateTime::Format::Pg; - -use Utils; -use Utils::OpenStreetMap; - use FixMyStreet; -use FixMyStreet::Cobrand; use FixMyStreet::DB; -use FixMyStreet::Email; -use FixMyStreet::Map; -use FixMyStreet::SendReport; - -sub send(;$) { - my ($site_override) = @_; - my $rs = FixMyStreet::DB->resultset('Problem'); - - # Set up site, language etc. - my ($verbose, $nomail, $debug_mode) = CronFns::options(); - my $test_data; - - my $base_url = FixMyStreet->config('BASE_URL'); - my $site = $site_override || CronFns::site($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( { - state => $states, - whensent => undef, - bodies_str => { '!=', undef }, - } ); - my (%notgot, %note); +use FixMyStreet::Queue::Item::Report; - my $send_report = FixMyStreet::SendReport->new(); - my $senders = $send_report->get_senders; +has verbose => ( is => 'ro' ); - my $debug_unsent_count = 0; - debug_print("starting to loop through unsent problem reports...") if $debug_mode; - while (my $row = $unsent->next) { +has unconfirmed_data => ( is => 'ro', default => sub { {} } ); +has test_data => ( is => 'ro', default => sub { {} } ); - my $cobrand = $row->get_cobrand_logged; - FixMyStreet::DB->schema->cobrand($cobrand); - - if ($debug_mode) { - $debug_unsent_count++; - print "\n"; - debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id); - } - - # Cobranded and non-cobranded messages can share a database. In this case, the conf file - # should specify a vhost to send the reports for each cobrand, so that they don't get sent - # 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 (! $cobrand->email_host()) { - debug_print("skipping because this host does not send reports for cobrand " . $cobrand->moniker, $row->id) if $debug_mode; - next; - } +# Static method, used by send-reports cron script and tests. +# Creates a manager object from provided data and processes it. +sub send { + my ($verbose, $nomail, $debug) = @_; - $cobrand->set_lang_and_domain($row->lang, 1); - FixMyStreet::Map::set_map_class($cobrand->map_type); - if ( $row->is_from_abuser) { - $row->update( { state => 'hidden' } ); - debug_print("hiding because its sender is flagged as an abuser", $row->id) if $debug_mode; - next; - } elsif ( $row->title =~ /app store test/i ) { - $row->update( { state => 'hidden' } ); - debug_print("hiding because it is an app store test message", $row->id) if $debug_mode; - next; - } - - # Template variables for the email - my $email_base_url = $cobrand->base_url_for_report($row); - my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/; - $h{report} = $row; - $h{cobrand} = $cobrand; - map { $h{$_} = $row->user->$_ || '' } qw/email phone/; - $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) ) - if $row->confirmed; - - $h{query} = $row->postcode; - $h{url} = $email_base_url . $row->url; - $h{admin_url} = $row->admin_url($cobrand); - if ($row->photo) { - $h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n"; - $h{image_url} = $email_base_url . $row->photos->[0]->{url_full}; - my @all_images = map { $email_base_url . $_->{url_full} } @{ $row->photos }; - $h{all_image_urls} = \@all_images; - } else { - $h{has_photo} = ''; - $h{image_url} = ''; - } - $h{fuzzy} = $row->used_map ? _('To view a map of the precise location of this issue') - : _('The user could not locate the problem on a map, but to see the area around the location they entered'); - $h{closest_address} = ''; - - $h{osm_url} = Utils::OpenStreetMap::short_url($h{latitude}, $h{longitude}); - if ( $row->used_map ) { - $h{closest_address} = $cobrand->find_closest($row); - $h{osm_url} .= '?m'; - } - - if ( $cobrand->allow_anonymous_reports && - $row->user->email eq $cobrand->anonymous_account->{'email'} - ) { - $h{anonymous_report} = 1; - } - - $cobrand->call_hook(process_additional_metadata_for_email => $row, \%h); - - my $bodies = FixMyStreet::DB->resultset('Body')->search( - { id => $row->bodies_str_ids }, - { order_by => 'name' }, - ); - - my $missing; - if ($row->bodies_missing) { - my @missing = FixMyStreet::DB->resultset("Body")->search( - { id => [ split /,/, $row->bodies_missing ] }, - { order_by => 'name' } - )->get_column('name')->all; - $missing = join(' / ', @missing) if @missing; - } - - my $send_confirmation_email = $cobrand->report_sent_confirmation_email; - - my @dear; - my %reporters = (); - my $skip = 0; - while (my $body = $bodies->next) { - # See if this body wants confirmation email (in case report made on national site, for example) - if (my $cobrand_body = $body->get_cobrand_handler) { - if (my $id_ref = $cobrand_body->report_sent_confirmation_email) { - $send_confirmation_email = $id_ref; - } - } - - my $sender_info = $cobrand->get_body_sender( $body, $row->category ); - my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; - - if ( ! exists $senders->{ $sender } ) { - warn sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id; - next; - } - $reporters{ $sender } ||= $sender->new(); + my $manager = __PACKAGE__->new( + verbose => $verbose, + ); - my $inspection_required = $sender_info->{contact} - ? $sender_info->{contact}->get_extra_metadata('inspection_required') - : undef; - if ( $inspection_required ) { - my $reputation_threshold = $sender_info->{contact}->get_extra_metadata('reputation_threshold') || 0; - my $reputation_threshold_met = 0; - if ( $reputation_threshold > 0 ) { - my $user_reputation = $row->user->get_extra_metadata('reputation') || 0; - $reputation_threshold_met = $user_reputation >= $reputation_threshold; - } - unless ( - $row->get_extra_metadata('inspected') || - $row->user->has_permission_to( trusted => $row->bodies_str_ids ) || - $reputation_threshold_met - ) { - $skip = 1; - debug_print("skipped because not yet inspected", $row->id) if $debug_mode; - } - } + my $params = construct_query($debug); + my $db = FixMyStreet::DB->schema->storage; - if ( $reporters{ $sender }->should_skip( $row, $debug_mode ) ) { - $skip = 1; - debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id) if $debug_mode; - } else { - debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $sender_info->{method}, $row->id) if $debug_mode; - push @dear, $body->name; - $reporters{ $sender }->add_body( $body, $sender_info->{config} ); - } + $db->txn_do(sub { + my $unsent = FixMyStreet::DB->resultset('Problem')->search($params, { + for => \'UPDATE SKIP LOCKED', + }); - # If we are in the UK include eastings and northings - if ( $cobrand->country eq 'GB' && !$h{easting} ) { - ( $h{easting}, $h{northing}, $h{coordsyst} ) = $row->local_coords; - } + $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, + ); + $item->process; } - unless ( keys %reporters ) { - die 'Report not going anywhere for ID ' . $row->id . '!'; - } + $manager->end_line($unsent_count); + $manager->end_summary_unconfirmed; + }); - next if $skip; + return $manager->test_data; +} - if ($h{category} eq _('Other')) { - $h{category_footer} = _('this type of local problem'); - } else { - $h{category_footer} = "'" . $h{category} . "'"; - } +sub construct_query { + my ($debug) = @_; + 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'; - $h{bodies_name} = join(_(' and '), @dear); - if ($h{category} eq _('Other')) { - $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n" - : ''; - } else { - $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n" - : ''; - } - $h{missing} = ''; - if ($missing) { - $h{missing} = '[ ' - . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $missing) - . " ]\n\n"; - } + # 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 } ]; + } - if (FixMyStreet->staging_flag('send_reports', 0)) { - # on a staging server send emails to ourselves rather than the bodies - %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::Email/ } keys %reporters; - unless (%reporters) { - %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); - } - } + # 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 ] ]; - # Multiply results together, so one success counts as a success. - my $result = -1; + 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 @methods; - for my $sender ( keys %reporters ) { - debug_print("sending using " . $sender, $row->id) if $debug_mode; - $sender = $reporters{$sender}; - my $res = $sender->send( $row, \%h ); - $result *= $res; - push @methods, $sender if !$res; - if ( $sender->unconfirmed_counts) { - foreach my $e (keys %{ $sender->unconfirmed_counts } ) { - foreach my $c (keys %{ $sender->unconfirmed_counts->{$e} }) { - $notgot{$e}{$c} += $sender->unconfirmed_counts->{$e}{$c}; - } - } - %note = (%note, %{ $sender->unconfirmed_notes }); - } - $test_data->{test_req_used} = $sender->open311_test_req_used - if FixMyStreet->test_mode && $sender->can('open311_test_req_used'); - } + return $params; +} - # Add the send methods now because e.g. Open311 - # send() calls $row->discard_changes - foreach (@methods) { - $row->add_send_method($_); - } +sub end_line { + my ($self, $unsent_count) = @_; + return unless $self->verbose; - unless ($result) { - $row->update( { - whensent => \'current_timestamp', - lastupdate => \'current_timestamp', - } ); - if ($send_confirmation_email && !$h{anonymous_report}) { - $h{sent_confirm_id_ref} = $row->$send_confirmation_email; - _send_report_sent_email( $row, \%h, $nomail, $cobrand ); - } - debug_print("send successful: OK", $row->id) if $debug_mode; - } else { - my @errors; - for my $sender ( keys %reporters ) { - unless ( $reporters{ $sender }->success ) { - push @errors, $reporters{ $sender }->error; - } - } - $row->update_send_failed( join( '|', @errors ) ); - debug_print("send FAILED: " . join( '|', @errors ), $row->id) if $debug_mode; - } - } - if ($debug_mode) { - print "\n"; - if ($debug_unsent_count) { - debug_print("processed all unsent reports (total: $debug_unsent_count)"); - } else { - debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); - } + if ($unsent_count) { + $self->log("processed all unsent reports (total: $unsent_count)"); + } else { + $self->log("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); } +} - if ($verbose || $debug_mode) { - print "Council email addresses that need checking:\n" if keys %notgot; - foreach my $e (keys %notgot) { - foreach my $c (keys %{$notgot{$e}}) { - print " " . $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n"; - } - } - my $sending_errors = ''; - my $unsent = $rs->search( { - state => [ FixMyStreet::DB::Result::Problem::open_states() ], - whensent => undef, - bodies_str => { '!=', undef }, - send_fail_count => { '>', 0 } - } ); - while (my $row = $unsent->next) { - my $base_url = FixMyStreet->config('BASE_URL'); - $sending_errors .= "\n" . '=' x 80 . "\n\n" . "* " . $base_url . "/report/" . $row->id . ", failed " - . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp - . ", reason " . $row->send_fail_reason . "\n"; - } - if ($sending_errors) { - print "The following reports had problems sending:\n$sending_errors"; +sub end_summary_unconfirmed { + my $self = shift; + return unless $self->verbose; + + my %unconfirmed_data = %{$self->unconfirmed_data}; + print "Council email addresses that need checking:\n" if keys %unconfirmed_data; + foreach my $e (keys %unconfirmed_data) { + foreach my $c (keys %{$unconfirmed_data{$e}}) { + my $data = $unconfirmed_data{$e}{$c}; + print " " . $data->{count} . " problem, to $e category $c (" . $data->{note} . ")\n"; } } - - return $test_data; } -sub _send_report_sent_email { - my $row = shift; - my $h = shift; - my $nomail = shift; - my $cobrand = shift; - - # Don't send 'report sent' text - return unless $row->user->email_verified; +sub end_summary_failures { + my $self = shift; - my $contributed_as = $row->get_extra_metadata('contributed_as') || ''; - return if $contributed_as eq 'body' || $contributed_as eq 'anonymous_user'; - - FixMyStreet::Email::send_cron( - $row->result_source->schema, - 'confirm_report_sent.txt', - $h, - { - To => $row->user->email, - }, - undef, - $nomail, - $cobrand, - $row->lang, - ); + my $sending_errors = ''; + my $unsent = FixMyStreet::DB->resultset('Problem')->search( { + state => [ FixMyStreet::DB::Result::Problem::open_states() ], + whensent => undef, + bodies_str => { '!=', undef }, + send_fail_count => { '>', 0 } + } ); + while (my $row = $unsent->next) { + my $base_url = FixMyStreet->config('BASE_URL'); + $sending_errors .= "\n" . '=' x 80 . "\n\n" . "* " . $base_url . "/report/" . $row->id . ", failed " + . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp + . ", reason " . $row->send_fail_reason . "\n"; + } + if ($sending_errors) { + print "The following reports had problems sending:\n$sending_errors"; + } } -sub debug_print { - 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/Script/UpdateAllReports.pm b/perllib/FixMyStreet/Script/UpdateAllReports.pm index 33665b9da..b23ed5b6a 100755 --- a/perllib/FixMyStreet/Script/UpdateAllReports.pm +++ b/perllib/FixMyStreet/Script/UpdateAllReports.pm @@ -36,8 +36,8 @@ sub generate { { columns => [ 'id', 'bodies_str', 'state', 'areas', 'cobrand', 'category', - { duration => { extract => "epoch from current_timestamp-lastupdate" } }, - { age => { extract => "epoch from current_timestamp-$age_column" } }, + { duration => { extract => \"epoch from current_timestamp-lastupdate" } }, + { age => { extract => \"epoch from current_timestamp-$age_column" } }, ] } ); |