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

backend selection: show frequently used backends

Closes #288
parent 7d37905a
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -1055,6 +1055,7 @@ sub backend_form {
	my ($self) = @_;
	my $user = $self->current_user;

	my %backend_by_id;
	my @backends = $self->stations->get_backends;
	my @suggested_backends;

@@ -1272,8 +1273,13 @@ sub backend_form {
			}
		}
		$backend->{type} = $type;

		$backend_by_id{ $backend->{id} } = $backend;
	}

	my @frequent_backends = map { $backend_by_id{$_} }
	  $self->journeys->get_frequent_backend_ids( uid => $user->{id} );

	@backends = map { $_->[1] }
	  sort { $a->[0] cmp $b->[0] }
	  map { [ lc( $_->{name} ), $_ ] } grep { $_->{type} } @backends;
@@ -1281,6 +1287,7 @@ sub backend_form {
	$self->render(
		'select_backend',
		suggestions => \@suggested_backends,
		frequent    => \@frequent_backends,
		backends    => \@backends,
		user        => $user,
		redirect_to => $self->req->param('redirect_to') // '/',
+30 −0
Original line number Diff line number Diff line
@@ -1943,6 +1943,36 @@ sub get_latest_dest_ids {
	);
}

sub get_frequent_backend_ids {
	my ( $self, %opt ) = @_;

	my $uid       = $opt{uid};
	my $threshold = $opt{threshold}
	  // DateTime->now( time_zone => 'Europe/Berlin' )->subtract( months => 4 );
	my $limit = $opt{limit} // 5;
	my $db    = $opt{db} //= $self->{pg}->db;

	my $res = $db->select(
		'journeys',
		'count(*) as count, backend_id',
		{
			user_id        => $uid,
			real_departure => { '>', $threshold },
		},
		{
			group_by => ['backend_id'],
			order_by => { -desc => 'count' },
			limit    => $limit,
		}
	);

	my @backend_ids = $res->hashes->map( sub { shift->{backend_id} } )->each;

	say join( ' ', @backend_ids );

	return @backend_ids;
}

# Returns a listref of {eva, name} hashrefs for the specified backend.
sub get_connection_targets {
	my ( $self, %opt ) = @_;
+14 −0
Original line number Diff line number Diff line
@@ -11,6 +11,20 @@
	% if (stash('redirect_to')) {
		%= hidden_field 'redirect_to' => stash('redirect_to')
	% }
	% if (@{stash('frequent') // []}) {
		<div class="row">
			<div class="col s12">
				<h3>Häufig genutzt</h3>
				<p style="text-align: justify;">
					Die folgenden Backends wurden innerhalb der letzten vier
					Monate für Checkins verwendet.
				</p>
			</div>
		</div>
		% for my $backend (@{ stash('frequent') // [] }) {
			%= include '_backend_line', user => $user, backend => $backend
		% }
	% }
	% if (@{stash('suggestions') // []}) {
		<div class="row">
			<div class="col s12">