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

Compute delay and duration in the database view rather than in the model

parent c03bd627
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
@@ -3560,6 +3560,61 @@ qq{select distinct checkout_station_id from in_transit where backend_id = 0;}
			}
		);
	},

	# v69 -> v70
	# Compute arr_delay and dep_delay directly in the journey query
	sub {
		my ($db) = @_;
		$db->query(
			qq{
				drop view journeys_str;
				create view journeys_str as select
					journeys.id as journey_id, user_id,
					backend.iris as is_iris, backend.hafas as is_hafas,
					backend.efa as is_efa, backend.dbris as is_dbris,
					backend.motis as is_motis,
					backend.name as backend_name, journeys.backend_id as backend_id,
					train_type, train_line, train_no, train_id,
					extract(epoch from checkin_time) as checkin_ts,
					extract(epoch from sched_departure) as sched_dep_ts,
					extract(epoch from real_departure) as real_dep_ts,
					extract(epoch from real_departure - sched_departure) as delay_dep,
					checkin_station_id as dep_eva,
					dep_station.ds100 as dep_ds100,
					dep_station.name as dep_name,
					dep_station.lat as dep_lat,
					dep_station.lon as dep_lon,
					dep_station_external_id.external_id as dep_external_id,
					extract(epoch from checkout_time) as checkout_ts,
					extract(epoch from sched_arrival) as sched_arr_ts,
					extract(epoch from real_arrival) as real_arr_ts,
					extract(epoch from real_arrival - sched_arrival) as delay_arr,
					checkout_station_id as arr_eva,
					arr_station.ds100 as arr_ds100,
					arr_station.name as arr_name,
					arr_station.lat as arr_lat,
					arr_station.lon as arr_lon,
					arr_station_external_id.external_id as arr_external_id,
					extract(epoch from sched_arrival - sched_departure) as sched_duration,
					extract(epoch from real_arrival - real_departure) as rt_duration,
					polylines.polyline as polyline,
					visibility,
					coalesce(visibility, users.public_level & 127) as effective_visibility,
					cancelled, edited, route, messages, user_data,
					dep_platform, arr_platform
					from journeys
					left join polylines on polylines.id = polyline_id
					left join users on users.id = user_id
					left join stations as dep_station on checkin_station_id = dep_station.eva and journeys.backend_id = dep_station.source
					left join stations as arr_station on checkout_station_id = arr_station.eva and journeys.backend_id = arr_station.source
					left join stations_external_ids as dep_station_external_id on checkin_station_id = dep_station_external_id.eva and journeys.backend_id = dep_station_external_id.backend_id
					left join stations_external_ids as arr_station_external_id on checkout_station_id = arr_station_external_id.eva and journeys.backend_id = arr_station_external_id.backend_id
					left join backends as backend on journeys.backend_id = backend.id
					;
				update schema_version set version = 70;
			}
		);
	},
);

sub sync_dbdb {
+5 −15
Original line number Diff line number Diff line
@@ -642,7 +642,7 @@ sub get {

	my @select
	  = (
		qw(journey_id is_dbris is_iris is_hafas is_motis backend_name backend_id train_type train_line train_no checkin_ts sched_dep_ts real_dep_ts dep_eva dep_ds100 dep_name dep_platform dep_lat dep_lon checkout_ts sched_arr_ts real_arr_ts arr_eva arr_ds100 arr_name arr_platform arr_lat arr_lon cancelled edited route messages user_data visibility effective_visibility)
		qw(journey_id is_dbris is_iris is_hafas is_motis backend_name backend_id train_type train_line train_no checkin_ts sched_dep_ts real_dep_ts delay_dep dep_eva dep_ds100 dep_name dep_platform dep_lat dep_lon checkout_ts sched_arr_ts real_arr_ts delay_arr arr_eva arr_ds100 arr_name arr_platform arr_lat arr_lon rt_duration sched_duration cancelled edited route messages user_data visibility effective_visibility)
	  );
	my %where = (
		user_id   => $uid,
@@ -722,6 +722,7 @@ sub get {
			checkin_ts           => $entry->{checkin_ts},
			sched_dep_ts         => $entry->{sched_dep_ts},
			rt_dep_ts            => $entry->{real_dep_ts},
			delay_dep            => $entry->{delay_dep},
			to_eva               => $entry->{arr_eva},
			to_ds100             => $entry->{arr_ds100},
			to_name              => $entry->{arr_name},
@@ -730,6 +731,9 @@ sub get {
			checkout_ts          => $entry->{checkout_ts},
			sched_arr_ts         => $entry->{sched_arr_ts},
			rt_arr_ts            => $entry->{real_arr_ts},
			delay_arr            => $entry->{delay_arr},
			sched_duration       => $entry->{sched_duration},
			rt_duration          => $entry->{rt_duration},
			messages             => $entry->{messages},
			route                => $entry->{route},
			edited               => $entry->{edited},
@@ -759,12 +763,6 @@ sub get {
			$ref->{checkout}      = epoch_to_dt( $ref->{checkout_ts} );
			$ref->{sched_arrival} = epoch_to_dt( $ref->{sched_arr_ts} );
			$ref->{rt_arrival}    = epoch_to_dt( $ref->{rt_arr_ts} );
			if ( $ref->{rt_dep_ts} and $ref->{sched_dep_ts} ) {
				$ref->{delay_dep} = $ref->{rt_dep_ts} - $ref->{sched_dep_ts};
			}
			if ( $ref->{rt_arr_ts} and $ref->{sched_arr_ts} ) {
				$ref->{delay_arr} = $ref->{rt_arr_ts} - $ref->{sched_arr_ts};
			}
		}
		if ( $opt{with_route_datetime} ) {
			for my $stop ( @{ $ref->{route} } ) {
@@ -798,14 +796,6 @@ sub get {
				push( @parsed_messages, [ epoch_to_dt($ts), $msg ] );
			}
			$ref->{messages} = [ reverse @parsed_messages ];
			$ref->{sched_duration}
			  = defined $ref->{sched_arr_ts}
			  ? $ref->{sched_arr_ts} - $ref->{sched_dep_ts}
			  : undef;
			$ref->{rt_duration}
			  = defined $ref->{rt_arr_ts}
			  ? $ref->{rt_arr_ts} - $ref->{rt_dep_ts}
			  : undef;
			my ( $km_polyline, $km_route, $km_beeline, $skip )
			  = $self->get_travel_distance($ref);
			$ref->{km_route}     = $km_polyline || $km_route || $km_beeline;