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

postprocess timeline; move user-related parts to get_user_status

parent 5006bf62
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -1442,6 +1442,42 @@ sub startup {
					}
				}

				my $stop_after_dep
				  = scalar @{ $ret->{route_after} }
				  ? $ret->{route_after}[0][0]
				  : undef;
				my $stop_before_dest
				  = scalar @{ $ret->{route_after} }
				  ? $ret->{route_after}[-1][0]
				  : undef;

				my ($dep_platform_number)
				  = ( ( $ret->{dep_platform} // 0 ) =~ m{(\d+)} );
				if ( $dep_platform_number
					and
					exists $ret->{data}{stationinfo_dep}{$dep_platform_number} )
				{
					$ret->{dep_direction} = $self->stationinfo_to_direction(
						$ret->{data}{stationinfo_dep}{$dep_platform_number},
						$ret->{data}{wagonorder_dep},
						undef, $stop_after_dep
					);
				}

				my ($arr_platform_number)
				  = ( ( $ret->{arr_platform} // 0 ) =~ m{(\d+)} );
				if ( $arr_platform_number
					and
					exists $ret->{data}{stationinfo_arr}{$arr_platform_number} )
				{
					$ret->{arr_direction} = $self->stationinfo_to_direction(
						$ret->{data}{stationinfo_arr}{$arr_platform_number},
						$ret->{data}{wagonorder_arr},
						$stop_before_dest,
						undef
					);
				}

				if (    $ret->{departure_countdown} > 0
					and $in_transit->{data}{wagonorder_dep} )
				{
+131 −152
Original line number Diff line number Diff line
@@ -134,53 +134,16 @@ sub delete_incomplete_checkins {
		{ checkin_time => { '<', $opt{earlier_than} } } )->rows;
}

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

	my $uid = $opt{uid};
	my $db  = $opt{db} // $self->{pg}->db;

	my $table = 'in_transit';

	if ( $opt{with_timestamps} ) {
		$table = 'in_transit_str';
	}

	my $res = $db->select( $table, '*', { user_id => $uid } );
	my $ret;

	if ( $opt{with_data} ) {
		$ret = $res->expand->hash;
	}
	else {
		$ret = $res->hash;
	}

	if ( $opt{with_visibility} and $ret ) {
		$ret->{visibility_str}
		  = $ret->{visibility}
		  ? $visibility_itoa{ $ret->{visibility} }
		  : 'default';
		$ret->{effective_visibility_str}
		  = $visibility_itoa{ $ret->{effective_visibility} };
	}

	if ( $opt{postprocess} and $ret ) {
sub postprocess {
	my ( $self, $ret ) = @_;
	my $now   = DateTime->now( time_zone => 'Europe/Berlin' );
	my $epoch = $now->epoch;
	my @route = @{ $ret->{route} // [] };
	my @route_after;
	my $dep_info;
		my $stop_before_dest;
	my $is_after = 0;
		for my $station (@route) {

			if (    $ret->{arr_name}
				and @route_after
				and $station->[0] eq $ret->{arr_name} )
			{
				$stop_before_dest = $route_after[-1][0];
			}
	for my $station (@route) {
		if ($is_after) {
			push( @route_after, $station );
		}
@@ -193,7 +156,6 @@ sub get {
			}
		}
	}
		my $stop_after_dep = @route_after ? $route_after[0][0] : undef;

	my $ts          = $ret->{checkout_ts} // $ret->{checkin_ts};
	my $action_time = epoch_to_dt($ts);
@@ -210,6 +172,13 @@ sub get {
	$ret->{extra_data}         = $ret->{data};
	$ret->{comment}            = $ret->{user_data}{comment};

	$ret->{visibility_str}
	  = $ret->{visibility}
	  ? $visibility_itoa{ $ret->{visibility} }
	  : 'default';
	$ret->{effective_visibility_str}
	  = $visibility_itoa{ $ret->{effective_visibility} };

	my @parsed_messages;
	for my $message ( @{ $ret->{messages} // [] } ) {
		my ( $ts, $msg ) = @{$message};
@@ -267,12 +236,10 @@ sub get {
		}
	}

		$ret->{departure_countdown}
		  = $ret->{real_departure}->epoch - $now->epoch;
	$ret->{departure_countdown} = $ret->{real_departure}->epoch - $now->epoch;

	if ( $ret->{real_arr_ts} ) {
			$ret->{arrival_countdown}
			  = $ret->{real_arrival}->epoch - $now->epoch;
		$ret->{arrival_countdown} = $ret->{real_arrival}->epoch - $now->epoch;
		$ret->{journey_duration}
		  = $ret->{real_arrival}->epoch - $ret->{real_departure}->epoch;
		$ret->{journey_completion}
@@ -286,37 +253,49 @@ sub get {
			$ret->{journey_completion} = 0;
		}

			my ($dep_platform_number)
			  = ( ( $ret->{dep_platform} // 0 ) =~ m{(\d+)} );
			if ( $dep_platform_number
				and exists $ret->{data}{stationinfo_dep}{$dep_platform_number} )
			{
				$ret->{dep_direction} = $self->stationinfo_to_direction(
					$ret->{data}{stationinfo_dep}{$dep_platform_number},
					$ret->{data}{wagonorder_dep},
					undef, $stop_after_dep
				);
	}
	else {
		$ret->{arrival_countdown}  = undef;
		$ret->{journey_duration}   = undef;
		$ret->{journey_completion} = undef;
	}

			my ($arr_platform_number)
			  = ( ( $ret->{arr_platform} // 0 ) =~ m{(\d+)} );
			if ( $arr_platform_number
				and exists $ret->{data}{stationinfo_arr}{$arr_platform_number} )
			{
				$ret->{arr_direction} = $self->stationinfo_to_direction(
					$ret->{data}{stationinfo_arr}{$arr_platform_number},
					$ret->{data}{wagonorder_arr},
					$stop_before_dest, undef
				);
	return $ret;
}

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

	my $uid = $opt{uid};
	my $db  = $opt{db} // $self->{pg}->db;

	my $table = 'in_transit';

	if ( $opt{with_timestamps} ) {
		$table = 'in_transit_str';
	}

	my $res = $db->select( $table, '*', { user_id => $uid } );
	my $ret;

	if ( $opt{with_data} ) {
		$ret = $res->expand->hash;
	}
	else {
			$ret->{arrival_countdown}  = undef;
			$ret->{journey_duration}   = undef;
			$ret->{journey_completion} = undef;
		$ret = $res->hash;
	}

	if ( $opt{with_visibility} and $ret ) {
		$ret->{visibility_str}
		  = $ret->{visibility}
		  ? $visibility_itoa{ $ret->{visibility} }
		  : 'default';
		$ret->{effective_visibility_str}
		  = $visibility_itoa{ $ret->{effective_visibility} };
	}

	if ( $opt{postprocess} and $ret ) {
		return $self->postprocess($ret);
	}

	return $ret;
@@ -347,7 +326,7 @@ sub get_timeline {
	my $ret;

	if ( $opt{with_data} ) {
		return $res->expand->hashes->each;
		return map { $self->postprocess($_) } $res->expand->hashes->each;
	}
	else {
		return $res->hashes->each;