From 683b188b288fe43526e1649c784fa44435559655 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 30 Apr 2020 13:51:42 +0100 Subject: Move per-row Contact lookup to the database. On admin report lists, and in front-end lists when an inspector, each row was querying the database for `category_display`. We create a new relationship for this query, and join/prefetch it wherever we request this data. Include staff joins on /around page, copying what happens on /reports to prevent more lookups there too. Also add some joins for user email in admin report list. --- perllib/FixMyStreet/DB/Result/Problem.pm | 42 +++++++++++++++++--------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'perllib/FixMyStreet/DB/Result/Problem.pm') diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 37563d327..42983c9ad 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -193,6 +193,27 @@ __PACKAGE__->might_have( cascade_copy => 0, cascade_delete => 1 }, ); +# Add a possible join for the Contact object associated with +# this report (based on bodies_str and category). If the report +# was sent to multiple bodies, only returns the first. +__PACKAGE__->belongs_to( + contact => "FixMyStreet::DB::Result::Contact", + sub { + my $args = shift; + return { + "$args->{foreign_alias}.category" => { -ident => "$args->{self_alias}.category" }, + -and => [ + \[ "CAST($args->{foreign_alias}.body_id AS text) = (regexp_split_to_array($args->{self_alias}.bodies_str, ','))[1]" ], + ] + }; + }, + { + join_type => "LEFT", + on_delete => "NO ACTION", + on_update => "NO ACTION", + }, +); + __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); __PACKAGE__->rabx_column('extra'); __PACKAGE__->rabx_column('geocode'); @@ -407,30 +428,11 @@ sub confirm { sub category_display { my $self = shift; - my $contact = $self->category_row; + my $contact = $self->contact; return $self->category unless $contact; # Fallback; shouldn't happen, but some tests return $contact->category_display; } -=head2 category_row - -Returns the corresponding Contact object for this problem's category and body. -If the report was sent to multiple bodies, only returns the first. - -=cut - -sub category_row { - my $self = shift; - my $schema = $self->result_source->schema; - my $body_id = $self->bodies_str_ids->[0]; - return unless $body_id && $body_id =~ /^[0-9]+$/; - my $contact = $schema->resultset("Contact")->find({ - body_id => $body_id, - category => $self->category, - }); - return $contact; -} - sub bodies_str_ids { my $self = shift; return [] unless $self->bodies_str; -- cgit v1.2.3 From c7dbb65e2d01e37f276af3db0372123366b3a1a1 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Mon, 22 Jun 2020 17:20:10 +0100 Subject: Rewrite open311-update-reports to share code. Make GetUpdates and GetServiceRequestUpdates share a common base; spot all visible states. --- perllib/FixMyStreet/DB/Result/Problem.pm | 81 -------------------------------- 1 file changed, 81 deletions(-) (limited to 'perllib/FixMyStreet/DB/Result/Problem.pm') diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 42983c9ad..0653de32b 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -838,87 +838,6 @@ sub local_coords { } } -=head2 update_from_open311_service_request - - $p->update_from_open311_service_request( $request, $body, $system_user ); - -Updates the problem based on information in the passed in open311 request -(standard, not the extension that uses GetServiceRequestUpdates) . If the -request has an older update time than the problem's lastupdate time then -nothing happens. - -Otherwise a comment will be created if there is status update text in the -open311 request. If the open311 request has a state of closed then the problem -will be marked as fixed. - -NB: a comment will always be created if the problem is being marked as fixed. - -Fixed problems will not be re-opened by this method. - -=cut - -sub update_from_open311_service_request { - my ( $self, $request, $body, $system_user ) = @_; - - my ( $updated, $status_notes ); - - if ( ! ref $request->{updated_datetime} ) { - $updated = $request->{updated_datetime}; - } - - if ( ! ref $request->{status_notes} ) { - $status_notes = $request->{status_notes}; - } - - my $update = $self->new_related(comments => { - state => 'confirmed', - created => $updated || \'current_timestamp', - confirmed => \'current_timestamp', - text => $status_notes, - mark_open => 0, - mark_fixed => 0, - user => $system_user, - anonymous => 0, - name => $body->name, - }); - - my $w3c = DateTime::Format::W3CDTF->new; - my $req_time = $w3c->parse_datetime( $request->{updated_datetime} ); - - # set a timezone here as the $req_time will have one and if we don't - # use a timezone then the date comparisons are invalid. - # of course if local timezone is not the one that went into the data - # base then we're also in trouble - my $lastupdate = $self->lastupdate; - $lastupdate->set_time_zone( FixMyStreet->local_time_zone ); - - # update from open311 is older so skip - if ( $req_time < $lastupdate ) { - return 0; - } - - if ( $request->{status} eq 'closed' ) { - if ( $self->state ne 'fixed' ) { - $self->state('fixed'); - $update->mark_fixed(1); - - if ( !$status_notes ) { - # FIXME - better text here - $status_notes = _('Closed by council'); - } - } - } - - if ( $status_notes ) { - $update->text( $status_notes ); - $self->lastupdate( $req_time ); - $self->update; - $update->insert; - } - - return 1; -} - sub update_send_failed { my $self = shift; my $msg = shift; -- cgit v1.2.3 From 615ddb00265386849f32405426c5232b9127669a Mon Sep 17 00:00:00 2001 From: M Somerville Date: Wed, 5 Aug 2020 17:36:18 +0100 Subject: Remove use of $c from various functions. This means these functions can more easily be used in a non-web context. --- perllib/FixMyStreet/DB/Result/Problem.pm | 40 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'perllib/FixMyStreet/DB/Result/Problem.pm') diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 0653de32b..705f07f79 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -631,13 +631,14 @@ meta data about the report. =cut sub meta_line { - my ( $problem, $c ) = @_; + my ( $problem, $user ) = @_; + my $cobrand = $problem->result_source->schema->cobrand; my $date_time = Utils::prettify_dt( $problem->confirmed ); my $meta = ''; my $category = $problem->category_display; - $category = $c->cobrand->call_hook(change_category_text => $category) || $category; + $category = $cobrand->call_hook(change_category_text => $category) || $category; if ( $problem->anonymous ) { if ( $problem->service and $category && $category ne _('Other') ) { @@ -656,8 +657,8 @@ sub meta_line { } else { my $problem_name = $problem->name; - if ($c->user_exists and - $c->user->has_permission_to('view_body_contribute_details', $problem->bodies_str_ids) and + if ($user and + $user->has_permission_to('view_body_contribute_details', $problem->bodies_str_ids) and $problem->name ne $problem->user->name) { $problem_name = sprintf('%s (%s)', $problem->name, $problem->user->name ); } @@ -692,12 +693,12 @@ sub nearest_address { } sub body { - my ( $problem, $c ) = @_; + my ( $problem, $link ) = @_; my $body; if ($problem->external_body) { if ($problem->cobrand eq 'zurich') { my $cache = $problem->result_source->schema->cache; - return $cache->{bodies}{$problem->external_body} //= $c->model('DB::Body')->find({ id => $problem->external_body }); + return $cache->{bodies}{$problem->external_body} //= FixMyStreet::DB->resultset('Body')->find({ id => $problem->external_body }); } else { $body = FixMyStreet::Template::html_filter($problem->external_body); } @@ -705,7 +706,7 @@ sub body { my $bodies = $problem->bodies; my @body_names = sort map { my $name = $_->name; - if ($c and FixMyStreet->config('AREA_LINKS_FROM_PROBLEMS')) { + if ($link and FixMyStreet->config('AREA_LINKS_FROM_PROBLEMS')) { '' . FixMyStreet::Template::html_filter($name) . ''; } else { FixMyStreet::Template::html_filter($name); @@ -810,9 +811,10 @@ sub can_display_external_id { # This can return HTML and is safe, so returns a FixMyStreet::Template::SafeString sub duration_string { - my ( $problem, $c ) = @_; - my $body = $c->cobrand->call_hook(link_to_council_cobrand => $problem) || $problem->body($c); - my $handler = $c->cobrand->call_hook(get_body_handler_for_problem => $problem); + my $problem = shift; + my $cobrand = $problem->result_source->schema->cobrand; + my $body = $cobrand->call_hook(link_to_council_cobrand => $problem) || $problem->body(1); + my $handler = $cobrand->call_hook(get_body_handler_for_problem => $problem); if ( $handler && $handler->call_hook('is_council_with_case_management') ) { my $s = sprintf(_('Received by %s moments later'), $body); return FixMyStreet::Template::SafeString->new($s); @@ -896,7 +898,8 @@ sub resend { } sub as_hashref { - my ($self, $c, $cols) = @_; + my ($self, $cols) = @_; + my $cobrand = $self->result_source->schema->cobrand; my $state_t = FixMyStreet::DB->resultset("State")->display($self->state); @@ -916,11 +919,11 @@ sub as_hashref { }; $out->{is_fixed} = $self->fixed_states->{ $self->state } ? 1 : 0 if !$cols || $cols->{is_fixed}; $out->{photos} = [ map { $_->{url} } @{$self->photos} ] if !$cols || $cols->{photos}; - $out->{meta} = $self->confirmed ? $self->meta_line( $c ) : '' if !$cols || $cols->{meta}; - $out->{created_pp} = $c->cobrand->prettify_dt( $self->created ) if !$cols || $cols->{created_pp}; + $out->{meta} = $self->confirmed ? $self->meta_line : '' if !$cols || $cols->{meta}; + $out->{created_pp} = $cobrand->prettify_dt( $self->created ) if !$cols || $cols->{created_pp}; if ($self->confirmed) { $out->{confirmed} = $self->confirmed if !$cols || $cols->{confirmed}; - $out->{confirmed_pp} = $c->cobrand->prettify_dt( $self->confirmed ) if !$cols || $cols->{confirmed_pp}; + $out->{confirmed_pp} = $cobrand->prettify_dt( $self->confirmed ) if !$cols || $cols->{confirmed_pp}; } return $out; } @@ -973,10 +976,11 @@ has get_cobrand_logged => ( sub pin_data { - my ($self, $c, $page, %opts) = @_; - my $colour = $c->cobrand->pin_colour($self, $page); + my ($self, $page, %opts) = @_; + my $cobrand = $self->result_source->schema->cobrand; + my $colour = $cobrand->pin_colour($self, $page); my $title = $opts{private} ? $self->title : $self->title_safe; - $title = $c->cobrand->call_hook(pin_hover_title => $self, $title) || $title; + $title = $cobrand->call_hook(pin_hover_title => $self, $title) || $title; { latitude => $self->latitude, longitude => $self->longitude, @@ -986,7 +990,7 @@ sub pin_data { problem => $self, draggable => $opts{draggable}, type => $opts{type}, - base_url => $c->cobrand->relative_url_for_report($self), + base_url => $cobrand->relative_url_for_report($self), } }; -- cgit v1.2.3 From e3e1e9d54999692dc01f3fdd32693547af8deb7b Mon Sep 17 00:00:00 2001 From: M Somerville Date: Thu, 1 Oct 2020 14:44:07 +0100 Subject: Do not display deleted priorities in inspect form. --- perllib/FixMyStreet/DB/Result/Problem.pm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'perllib/FixMyStreet/DB/Result/Problem.pm') diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 705f07f79..cd5e876b4 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -780,7 +780,11 @@ alphabetical order of name. sub response_priorities { my $self = shift; - return $self->result_source->schema->resultset('ResponsePriority')->for_bodies($self->bodies_str_ids, $self->category); + my $rs = $self->result_source->schema->resultset('ResponsePriority')->for_bodies($self->bodies_str_ids, $self->category); + $rs->search([ + 'me.deleted' => 0, + 'me.id' => $self->response_priority_id, + ]); } =head2 defect_types -- cgit v1.2.3 From f4a9c33b3deeab5b17721ff42b54cad6a4713317 Mon Sep 17 00:00:00 2001 From: M Somerville Date: Thu, 8 Oct 2020 16:33:24 +0100 Subject: [Oxfordshire] Traffic Management out of base. --- perllib/FixMyStreet/DB/Result/Problem.pm | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'perllib/FixMyStreet/DB/Result/Problem.pm') diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index cd5e876b4..ceb41b40f 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -1125,17 +1125,6 @@ has duplicates => ( }, ); -has traffic_management_options => ( - is => 'ro', - lazy => 1, - default => sub { - my $self = shift; - my $cobrand = $self->get_cobrand_logged; - $cobrand = $cobrand->call_hook(get_body_handler_for_problem => $self) || $cobrand; - return $cobrand->traffic_management_options; - }, -); - has inspection_log_entry => ( is => 'ro', lazy => 1, -- cgit v1.2.3