From 0a9d2e4c88f46fb8f5ea91009b16c84403d546e2 Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Wed, 20 Mar 2019 18:33:51 +0100
Subject: [PATCH] journey: show average speed

---
 index.pl                  | 31 ++++++++++++++++++++++++++++---
 templates/journey.html.ep | 15 ++++++++++-----
 2 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/index.pl b/index.pl
index de1fc080..0cc6d148 100755
--- a/index.pl
+++ b/index.pl
@@ -877,6 +877,31 @@ helper 'get_user_travels' => sub {
 					push( @parsed_messages, [ epoch_to_dt($ts), $msg ] );
 				}
 				$ref->{messages} = [ reverse @parsed_messages ];
+				$ref->{sched_duration}
+				  = $ref->{sched_arrival}
+				  ? $ref->{sched_arrival}->epoch
+				  - $ref->{sched_departure}->epoch
+				  : undef;
+				$ref->{rt_duration}
+				  = $ref->{rt_arrival}
+				  ? $ref->{rt_arrival}->epoch - $ref->{rt_departure}->epoch
+				  : undef;
+				$ref->{km_route}
+				  = $self->get_travel_distance( $ref->{from_name},
+					$ref->{to_name}, $ref->{route} );
+				$ref->{km_beeline}
+				  = $self->get_travel_distance( $ref->{from_name},
+					$ref->{to_name}, [ $ref->{from_name}, $ref->{to_name} ] );
+				$ref->{kmh_route}
+				  = $ref->{km_route}
+				  / (
+					( $ref->{rt_duration} // $ref->{sched_duration} // 999999 )
+					/ 3600 );
+				$ref->{kmh_beeline}
+				  = $ref->{km_beeline}
+				  / (
+					( $ref->{rt_duration} // $ref->{sched_duration} // 999999 )
+					/ 3600 );
 			}
 			if (    $opt{checkin_epoch}
 				and $action == $action_type{cancelled_from} )
@@ -1117,9 +1142,9 @@ get '/api/v0/:action/:token' => sub {
 					line => $status->{train_line},
 					no   => $status->{train_no},
 				},
-				actionTime => $status->{timestamp}->epoch,
-				scheduledTime  => $status->{sched_ts}->epoch,
-				realTime   => $status->{real_ts}->epoch,
+				actionTime    => $status->{timestamp}->epoch,
+				scheduledTime => $status->{sched_ts}->epoch,
+				realTime      => $status->{real_ts}->epoch,
 			},
 		);
 	}
diff --git a/templates/journey.html.ep b/templates/journey.html.ep
index a9c0c0bb..4050e610 100644
--- a/templates/journey.html.ep
+++ b/templates/journey.html.ep
@@ -78,12 +78,17 @@
 				<tr>
 					<th scope="row">Entfernung</th>
 					<td>
-						% my $distance = get_travel_distance($journey->{from_name}, $journey->{to_name}, $journey->{route});
-						% my $beeline = get_travel_distance($journey->{from_name}, $journey->{to_name}, [$journey->{from_name}, $journey->{to_name}]);
-						% if ($distance > 0.1) {
-							ca. <%= sprintf('%.f', $distance) %> km
+						% if ($journey->{km_route} > 0.1) {
+							ca. <%= sprintf('%.f', $journey->{km_route}) %> km
 						% }
-						(Luftlinie: <%= sprintf('%.f', $beeline) %> km)
+						(Luftlinie: <%= sprintf('%.f', $journey->{km_beeline}) %> km)
+					</td>
+				</tr>
+				<tr>
+					<th scope="row">Geschwindigkeit</th>
+					<td>
+						∅ <%= sprintf('%.f', $journey->{kmh_route}) %> km/h
+						(<%= sprintf('%.f', $journey->{kmh_beeline}) %> km/h)
 					</td>
 				</tr>
 				<tr>
-- 
GitLab