Move get_departures to a separate IRIS helper

This commit is contained in:
Daniel Friesel 2020-08-06 16:04:12 +02:00
parent adaf65dc63
commit 717cc18a40
4 changed files with 122 additions and 63 deletions

View file

@ -19,6 +19,7 @@ use Travel::Status::DE::DBWagenreihung;
use Travel::Status::DE::IRIS;
use Travel::Status::DE::IRIS::Stations;
use Travelynx::Helper::HAFAS;
use Travelynx::Helper::IRIS;
use Travelynx::Helper::Sendmail;
use Travelynx::Model::Users;
use XML::LibXML;
@ -277,6 +278,18 @@ sub startup {
}
);
$self->helper(
iris => sub {
my ($self) = @_;
state $hafas = Travelynx::Helper::IRIS->new(
log => $self->app->log,
main_cache => $self->app->cache_iris_main,
realtime_cache => $self->app->cache_iris_rt,
version => $self->app->config->{version},
);
}
);
$self->helper(
pg => sub {
my ($self) = @_;
@ -332,62 +345,6 @@ sub startup {
}
);
$self->helper(
'get_departures' => sub {
my ( $self, $station, $lookbehind, $lookahead, $with_related ) = @_;
$lookbehind //= 180;
$lookahead //= 30;
$with_related //= 0;
my @station_matches
= Travel::Status::DE::IRIS::Stations::get_station($station);
if ( @station_matches == 1 ) {
$station = $station_matches[0][0];
my $status = Travel::Status::DE::IRIS->new(
station => $station,
main_cache => $self->app->cache_iris_main,
realtime_cache => $self->app->cache_iris_rt,
keep_transfers => 1,
lookbehind => 20,
datetime => DateTime->now( time_zone => 'Europe/Berlin' )
->subtract( minutes => $lookbehind ),
lookahead => $lookbehind + $lookahead,
lwp_options => {
timeout => 10,
agent => 'travelynx/' . $self->app->config->{version},
},
with_related => $with_related,
);
return {
results => [ $status->results ],
errstr => $status->errstr,
station_ds100 =>
( $status->station ? $status->station->{ds100} : undef ),
station_eva =>
( $status->station ? $status->station->{uic} : undef ),
station_name =>
( $status->station ? $status->station->{name} : undef ),
related_stations => [ $status->related_stations ],
};
}
elsif ( @station_matches > 1 ) {
return {
results => [],
errstr => 'Mehrdeutiger Stationsname. Mögliche Eingaben: '
. join( q{, }, map { $_->[1] } @station_matches ),
};
}
else {
return {
results => [],
errstr => 'Unbekannte Station',
};
}
}
);
$self->helper(
'grep_unknown_stations' => sub {
my ( $self, @stations ) = @_;
@ -529,7 +486,11 @@ sub startup {
$uid //= $self->current_user->{id};
my $status = $self->get_departures( $station, 140, 40, 0 );
my $status = $self->iris->get_departures(
station => $station,
lookbehind => 140,
lookahead => 40
);
if ( $status->{errstr} ) {
return ( undef, $status->{errstr} );
}
@ -708,7 +669,11 @@ sub startup {
my ( $self, $station, $force, $uid ) = @_;
my $db = $self->pg->db;
my $status = $self->get_departures( $station, 120, 120, 0 );
my $status = $self->iris->get_departures(
station => $station,
lookbehind => 120,
lookahead => 120
);
$uid //= $self->current_user->{id};
my $user = $self->get_user_status($uid);
my $train_id = $user->{train_id};
@ -750,7 +715,12 @@ sub startup {
# While at it, we increase the lookahead to handle long journeys as
# well.
if ( not $train ) {
$status = $self->get_departures( $station, 120, 180, 1 );
$status = $self->iris->get_departures(
station => $station,
lookbehind => 120,
lookahead => 180,
with_related => 1
);
($train) = List::Util::first { $_->train_id eq $train_id }
@{ $status->{results} };
if ( $train
@ -2183,7 +2153,12 @@ sub startup {
return;
}
my $stationboard = $self->get_departures( $eva, 10, 40, 1 );
my $stationboard = $self->iris->get_departures(
station => $eva,
lookbehind => 10,
lookahead => 40,
with_related => 1
);
if ( $stationboard->{errstr} ) {
return;
}

View file

@ -35,7 +35,11 @@ sub run {
eval {
if ( $now->epoch - $entry->{real_dep_ts} < 900 ) {
my $status = $self->app->get_departures( $dep, 30, 30 );
my $status = $self->app->iris->get_departures(
station => $dep,
lookbehind => 30,
lookahead => 30
);
if ( $status->{errstr} ) {
die("get_departures($dep): $status->{errstr}\n");
}
@ -123,7 +127,11 @@ sub run {
or $now->epoch - $entry->{real_arr_ts} < 600 )
)
{
my $status = $self->app->get_departures( $arr, 20, 220 );
my $status = $self->app->iris->get_departures(
station => $arr,
lookbehind => 20,
lookahead => 220
);
if ( $status->{errstr} ) {
die("get_departures($arr): $status->{errstr}\n");
}

View file

@ -423,7 +423,12 @@ sub station {
my $station = $self->stash('station');
my $train = $self->param('train');
my $status = $self->get_departures( $station, 120, 30, 1 );
my $status = $self->iris->get_departures(
station => $station,
lookbehind => 120,
lookahead => 30,
with_related => 1
);
if ( $status->{errstr} ) {
$self->render(

View file

@ -0,0 +1,71 @@
package Travelynx::Helper::IRIS;
use strict;
use warnings;
use 5.020;
use Travel::Status::DE::IRIS;
sub new {
my ( $class, %opt ) = @_;
return bless( \%opt, $class );
}
sub get_departures {
my ( $self, %opt ) = @_;
my $station = $opt{station};
my $lookbehind = $opt{lookbehind} // 180;
my $lookahead = $opt{lookahead} // 30;
my $with_related = $opt{with_related} // 0;
my @station_matches
= Travel::Status::DE::IRIS::Stations::get_station($station);
if ( @station_matches == 1 ) {
$station = $station_matches[0][0];
my $status = Travel::Status::DE::IRIS->new(
station => $station,
main_cache => $self->{main_cache},
realtime_cache => $self->{realtime_cache},
keep_transfers => 1,
lookbehind => 20,
datetime => DateTime->now( time_zone => 'Europe/Berlin' )
->subtract( minutes => $lookbehind ),
lookahead => $lookbehind + $lookahead,
lwp_options => {
timeout => 10,
agent => 'travelynx/'
. $self->{version}
. ' +https://travelynx.de',
},
with_related => $with_related,
);
return {
results => [ $status->results ],
errstr => $status->errstr,
station_ds100 =>
( $status->station ? $status->station->{ds100} : undef ),
station_eva =>
( $status->station ? $status->station->{uic} : undef ),
station_name =>
( $status->station ? $status->station->{name} : undef ),
related_stations => [ $status->related_stations ],
};
}
elsif ( @station_matches > 1 ) {
return {
results => [],
errstr => 'Mehrdeutiger Stationsname. Mögliche Eingaben: '
. join( q{, }, map { $_->[1] } @station_matches ),
};
}
else {
return {
results => [],
errstr => 'Unbekannte Station',
};
}
}
1;