refactor polyline insertion into InTransit Model

This commit is contained in:
Birte Kristina Friesel 2024-04-21 19:21:16 +02:00
parent 2ff5f92bcc
commit a0bd839594
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
2 changed files with 64 additions and 95 deletions

View file

@ -621,52 +621,11 @@ sub startup {
} }
if ($polyline) { if ($polyline) {
my $coords = $polyline->{coords}; $self->in_transit->set_polyline(
my $from_eva = $polyline->{from_eva}; uid => $uid,
my $to_eva = $polyline->{to_eva}; db => $db,
polyline => $polyline,
my $polyline_str = JSON->new->encode($coords);
my $pl_res = $db->select(
'polylines',
['id'],
{
origin_eva => $from_eva,
destination_eva => $to_eva,
polyline => $polyline_str
},
{ limit => 1 }
); );
my $polyline_id;
if ( my $h = $pl_res->hash ) {
$polyline_id = $h->{id};
}
else {
eval {
$polyline_id = $db->insert(
'polylines',
{
origin_eva => $from_eva,
destination_eva => $to_eva,
polyline => $polyline_str
},
{ returning => 'id' }
)->hash->{id};
};
if ($@) {
$self->log->warn(
"add_route_timestamps: insert polyline: $@"
);
}
}
if ($polyline_id) {
$self->in_transit->set_polyline_id(
uid => $uid,
db => $db,
polyline_id => $polyline_id
);
}
} }
# mustn't be called during a transaction # mustn't be called during a transaction
@ -1372,57 +1331,12 @@ sub startup {
); );
if ($polyline) { if ($polyline) {
my $coords = $polyline->{coords}; $self->in_transit->set_polyline(
my $from_eva = $polyline->{from_eva}; uid => $uid,
my $to_eva = $polyline->{to_eva}; db => $db,
polyline => $polyline,
my $polyline_str = JSON->new->encode($coords); old_id => $in_transit->{polyline_id},
my $pl_res = $db->select(
'polylines',
['id'],
{
origin_eva => $from_eva,
destination_eva => $to_eva,
polyline => $polyline_str
},
{ limit => 1 }
); );
my $polyline_id;
if ( my $h = $pl_res->hash ) {
$polyline_id = $h->{id};
}
else {
eval {
$polyline_id = $db->insert(
'polylines',
{
origin_eva => $from_eva,
destination_eva => $to_eva,
polyline => $polyline_str
},
{ returning => 'id' }
)->hash->{id};
};
if ($@) {
$self->app->log->warn(
"add_route_timestamps: insert polyline: $@"
);
}
}
if (
$polyline_id
and ( not $in_transit->{polyline_id}
or $polyline_id != $in_transit->{polyline_id} )
)
{
$self->in_transit->set_polyline_id(
uid => $uid,
db => $db,
polyline_id => $polyline_id
);
}
} }
return; return;

View file

@ -517,6 +517,61 @@ sub set_arrival_times {
); );
} }
sub set_polyline {
my ( $self, %opt ) = @_;
my $uid = $opt{uid};
my $db = $opt{db} // $self->{pg}->db;
my $polyline = $opt{polyline};
my $old_id = $opt{old_id};
my $coords = $polyline->{coords};
my $from_eva = $polyline->{from_eva};
my $to_eva = $polyline->{to_eva};
my $polyline_str = JSON->new->encode($coords);
my $pl_res = $db->select(
'polylines',
['id'],
{
origin_eva => $from_eva,
destination_eva => $to_eva,
polyline => $polyline_str,
},
{ limit => 1 }
);
my $polyline_id;
if ( my $h = $pl_res->hash ) {
$polyline_id = $h->{id};
}
else {
eval {
$polyline_id = $db->insert(
'polylines',
{
origin_eva => $from_eva,
destination_eva => $to_eva,
polyline => $polyline_str
},
{ returning => 'id' }
)->hash->{id};
};
if ($@) {
$self->{log}->warn("add_route_timestamps: insert polyline: $@");
}
}
if ( $polyline_id and ( not defined $old_id or $polyline_id != $old_id ) ) {
$self->set_polyline_id(
uid => $uid,
db => $db,
polyline_id => $polyline_id
);
}
}
sub set_polyline_id { sub set_polyline_id {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;