Unverified Commit 17126940 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

mov get_connection_targets helper to Journeys Model

parent adf2df3a
Loading
Loading
Loading
Loading
+2 −73
Original line number Diff line number Diff line
@@ -370,6 +370,7 @@ sub startup {
			state $journeys = Travelynx::Model::Journeys->new(
				log             => $self->app->log,
				pg              => $self->pg,
				in_transit      => $self->in_transit,
				stats_cache     => $self->journey_stats_cache,
				renamed_station => $self->app->renamed_station,
				station_by_eva  => $self->app->station_by_eva,
@@ -1354,30 +1355,6 @@ sub startup {
		}
	);

	$self->helper(
		'get_latest_dest_id' => sub {
			my ( $self, %opt ) = @_;

			my $uid = $opt{uid} // $self->current_user->{id};
			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->journeys->get_latest_checkout_station_id(
				uid => $uid,
				db  => $db
			);
		}
	);

	$self->helper(
		'resolve_sb_template' => sub {
			my ( $self, $template, %opt ) = @_;
@@ -1391,54 +1368,6 @@ sub startup {
		}
	);

	$self->helper(
		'get_connection_targets' => sub {
			my ( $self, %opt ) = @_;

			my $uid       = $opt{uid} //= $self->current_user->{id};
			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->app->station_by_eva->{$_} } @destinations;
			@destinations
			  = map { $self->app->station_by_eva->{$_}->[1] } @destinations;
			return @destinations;
		}
	);

	$self->helper(
		'get_connecting_trains' => sub {
			my ( $self, %opt ) = @_;
@@ -1479,7 +1408,7 @@ sub startup {
				return;
			}

			my @destinations = $self->get_connection_targets(%opt);
			my @destinations = $self->journeys->get_connection_targets(%opt);

			if ($exclude_via) {
				@destinations = grep { $_ ne $exclude_via } @destinations;
+67 −0
Original line number Diff line number Diff line
@@ -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;