Request polylines for HAFAS journeys as well; only store sensible ones

Decreases severity of #99
Fixes #108
This commit is contained in:
Birte Kristina Friesel 2024-04-07 11:18:04 +02:00
parent c9e5bfae62
commit 875ec1c3e5
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
2 changed files with 92 additions and 2 deletions

View file

@ -536,7 +536,10 @@ sub startup {
my $promise = Mojo::Promise->new;
$self->hafas->get_journey_p( trip_id => $train_id )->then(
$self->hafas->get_journey_p(
trip_id => $train_id,
with_polyline => 1
)->then(
sub {
my ($journey) = @_;
my $found;
@ -579,6 +582,93 @@ sub startup {
data => { trip_id => $journey->id }
);
my $polyline;
if ( $journey->polyline ) {
my @station_list;
my @coordinate_list;
for my $coord ( $journey->polyline ) {
if ( $coord->{name} ) {
push(
@coordinate_list,
[
$coord->{lon}, $coord->{lat},
$coord->{eva}
]
);
push( @station_list, $coord->{name} );
}
else {
push( @coordinate_list,
[ $coord->{lon}, $coord->{lat} ] );
}
}
# equal length → polyline only consists of straight
# lines between stops. that's not helpful.
if ( @station_list == @coordinate_list ) {
$self->log->debug( 'Ignoring polyline for '
. $journey->line
. ' as it only consists of straight lines between stops.'
);
}
else {
$polyline = {
from_eva => ( $journey->route )[0]->loc->eva,
to_eva => ( $journey->route )[-1]->loc->eva,
coords => \@coordinate_list,
};
}
}
if ($polyline) {
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) {
$self->in_transit->set_polyline_id(
uid => $uid,
db => $db,
polyline_id => $polyline_id
);
}
}
# mustn't be called during a transaction
if ( not $opt{in_transaction} ) {
$self->run_hook( $uid, 'checkin' );

View file

@ -172,7 +172,7 @@ sub get_journey_p {
journey => {
id => $opt{trip_id},
},
with_polyline => 0,
with_polyline => $opt{with_polyline},
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(10),