summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Model/Journeys.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2022-07-29 10:10:34 +0200
committerDaniel Friesel <derf@finalrewind.org>2022-07-29 10:10:34 +0200
commit17126940115455296706b99655a052ffb60bd763 (patch)
tree0f77fc8c538b5f1918a76a493df31dd87cc95ffa /lib/Travelynx/Model/Journeys.pm
parentadf2df3a3bd2df4b5653845cea1d98f7297bbe4b (diff)
mov get_connection_targets helper to Journeys Model
Diffstat (limited to 'lib/Travelynx/Model/Journeys.pm')
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm67
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm
index 8567d27..e747198 100755
--- a/lib/Travelynx/Model/Journeys.pm
+++ b/lib/Travelynx/Model/Journeys.pm
@@ -1194,4 +1194,71 @@ sub get_stats {
return $stats;
}
+sub get_latest_dest_id {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ if (
+ my $id = $self->{in_transit}->get_checkout_station_id(
+ uid => $uid,
+ db => $db
+ )
+ )
+ {
+ return $id;
+ }
+
+ return $self->get_latest_checkout_station_id(
+ uid => $uid,
+ db => $db
+ );
+}
+
+sub get_connection_targets {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $threshold = $opt{threshold}
+ // DateTime->now( time_zone => 'Europe/Berlin' )->subtract( months => 4 );
+ my $db = $opt{db} //= $self->{pg}->db;
+ my $min_count = $opt{min_count} // 3;
+
+ if ( $opt{destination_name} ) {
+ return ( $opt{destination_name} );
+ }
+
+ my $dest_id = $opt{eva} // $self->get_latest_dest_id(%opt);
+
+ if ( not $dest_id ) {
+ return;
+ }
+
+ my $res = $db->query(
+ qq{
+ select
+ count(checkout_station_id) as count,
+ checkout_station_id as dest
+ from journeys
+ where user_id = ?
+ and checkin_station_id = ?
+ and real_departure > ?
+ group by checkout_station_id
+ order by count desc;
+ },
+ $uid,
+ $dest_id,
+ $threshold
+ );
+ my @destinations
+ = $res->hashes->grep( sub { shift->{count} >= $min_count } )
+ ->map( sub { shift->{dest} } )->each;
+ @destinations
+ = grep { $self->{station_by_eva}{$_} } @destinations;
+ @destinations
+ = map { $self->{station_by_eva}{$_}->[1] } @destinations;
+ return @destinations;
+}
+
1;