summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <daniel.friesel@uos.de>2022-12-08 18:22:09 +0100
committerDaniel Friesel <daniel.friesel@uos.de>2022-12-08 18:22:09 +0100
commit115e07f35c8bbd7409f32aa7f9f89fe222a67b0f (patch)
treea43eb26875adfbcf07cb8b60777a3aa2b0f65242
parentafedcef2663ceda6d4a9b88af702c04eb4eb0f4a (diff)
move grep_unknown_stations to Stations model
-rwxr-xr-xlib/Travelynx.pm17
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm17
-rw-r--r--lib/Travelynx/Model/Stations.pm20
3 files changed, 23 insertions, 31 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 8d661d2..4195001 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -420,21 +420,6 @@ sub startup {
);
$self->helper(
- 'grep_unknown_stations' => sub {
- my ( $self, @stations ) = @_;
-
- my @unknown_stations;
- for my $station (@stations) {
- my $station_info = $self->stations->get_by_name($station);
- if ( not $station_info ) {
- push( @unknown_stations, $station );
- }
- }
- return @unknown_stations;
- }
- );
-
- $self->helper(
'load_icon' => sub {
my ( $self, $load ) = @_;
my $first = $load->{FIRST} // 0;
@@ -750,7 +735,7 @@ sub startup {
$has_arrived = $train->arrival->epoch < $now->epoch ? 1 : 0;
if ($has_arrived) {
my @unknown_stations
- = $self->grep_unknown_stations( $train->route );
+ = $self->stations->grep_unknown( $train->route );
if (@unknown_stations) {
$self->app->log->warn(
sprintf(
diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm
index d9e17f0..2bbc0bc 100755
--- a/lib/Travelynx/Model/Journeys.pm
+++ b/lib/Travelynx/Model/Journeys.pm
@@ -34,19 +34,6 @@ sub epoch_to_dt {
);
}
-sub grep_unknown_stations {
- my ( $self, @stations ) = @_;
-
- my @unknown_stations;
- for my $station (@stations) {
- my $station_info = $self->{stations}->get_by_name($station);
- if ( not $station_info ) {
- push( @unknown_stations, $station );
- }
- }
- return @unknown_stations;
-}
-
sub new {
my ( $class, %opt ) = @_;
@@ -917,8 +904,8 @@ sub sanity_check {
}
if ( $journey->{edited} & 0x0010 and not $lax ) {
my @unknown_stations
- = $self->grep_unknown_stations( map { $_->[0] }
- @{ $journey->{route} } );
+ = $self->{stations}
+ ->grep_unknown( map { $_->[0] } @{ $journey->{route} } );
if (@unknown_stations) {
return 'Unbekannte Station(en): ' . join( ', ', @unknown_stations );
}
diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm
index 6c898b1..62504d3 100644
--- a/lib/Travelynx/Model/Stations.pm
+++ b/lib/Travelynx/Model/Stations.pm
@@ -62,6 +62,16 @@ sub get_by_name {
}
# Slow
+sub get_by_names {
+ my ( $self, @names ) = @_;
+
+ my @ret
+ = $self->{pg}->db->select( 'stations', '*', { name => { '=', \@names } } )
+ ->hashes->each;
+ return @ret;
+}
+
+# Slow
sub get_by_ds100 {
my ( $self, $ds100, %opt ) = @_;
@@ -85,4 +95,14 @@ sub search {
// $self->get_by_name( $identifier, %opt );
}
+# Slow
+sub grep_unknown {
+ my ( $self, @stations ) = @_;
+
+ my %station = map { $_->{name} => 1 } $self->get_by_names(@stations);
+ my @unknown_stations = grep { not $station{$_} } @stations;
+
+ return @unknown_stations;
+}
+
1;