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

journeys: pre-compute travel distances

parent 30c8a672
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -1384,6 +1384,9 @@ sub startup {
					);
				}

				delete $journey->{distance_beeline};
				delete $journey->{distance_route};
				delete $journey->{distance_polyline};
				delete $journey->{edited};
				delete $journey->{id};

+60 −0
Original line number Diff line number Diff line
@@ -3670,6 +3670,66 @@ qq{select distinct checkout_station_id from in_transit where backend_id = 0;}
			}
		);
	},

	# v71 -> v72
	# Add explicit column for journey distances
	sub {
		my ($db) = @_;
		$db->query(
			qq{
				drop view journeys_str;
				alter table journeys
					add column distance_beeline integer,
					add column distance_route integer,
					add column distance_polyline integer;
				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,
					distance_beeline, distance_route, distance_polyline,
					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 = 72;
			}
		);
	},
);

sub sync_dbdb {
+69 −2
Original line number Diff line number Diff line
@@ -251,9 +251,58 @@ sub add {
		return ( undef, 'add_journey failed: ' . $@ );
	}

	# We need lat/lon data in order to determine distances -- we could query
	# the stations table before inserting into journey, or we could just re-use
	# existing code paths. The latter is easier and only slightly slower.
	$self->update_distances(
		db         => $db,
		uid        => $uid,
		journey_id => $journey_id
	);

	return ( $journey_id, undef );
}

sub update_distances {
	my ( $self, %opt ) = @_;
	my $db         = $opt{db};
	my $uid        = $opt{uid};
	my $journey_id = $opt{journey_id};

	my $journey = $self->get_single(
		db            => $db,
		uid           => $uid,
		journey_id    => $journey_id,
		with_polyline => 1
	);
	if ($journey) {
		eval {
			my ( $km_polyline, $km_route, $km_beeline, $skip )
			  = $self->get_travel_distance($journey);

			my $rows = $db->update(
				'journeys',
				{
					distance_beeline => $km_beeline ? int( $km_beeline * 1e3 )
					: undef,
					distance_route => $km_route ? int( $km_route * 1e3 )
					: undef,
					distance_polyline => $km_polyline
					? int( $km_polyline * 1e3 )
					: undef,
				},
				{
					id      => $journey_id,
					user_id => $uid,
				}
			)->rows;
		};
		if ($@) {
			$self->{log}->error("update_distances($journey_id): $@");
		}
	}
}

sub add_from_in_transit {
	my ( $self, %opt ) = @_;
	my $db      = $opt{db};
@@ -269,8 +318,16 @@ sub add_from_in_transit {
	delete $journey->{data};
	$journey->{checkout_time} = DateTime->now( time_zone => 'Europe/Berlin' );

	return $db->insert( 'journeys', $journey, { returning => 'id' } )
	  ->hash->{id};
	my $journey_id
	  = $db->insert( 'journeys', $journey, { returning => 'id' } )->hash->{id};

	$self->update_distances(
		db         => $db,
		uid        => $journey->{user_id},
		journey_id => $journey_id
	);

	return $journey_id;
}

sub update {
@@ -466,6 +523,11 @@ sub update {
			db  => $db,
			uid => $uid,
		);
		$self->update_distances(
			db         => $db,
			uid        => $uid,
			journey_id => $journey_id
		);
		return undef;
	}
	return "update($journey_id): did not match any journey part";
@@ -606,6 +668,11 @@ sub set_polyline {
		);
	}

	$self->update_distances(
		db         => $db,
		uid        => $uid,
		journey_id => $opt{journey_id}
	);
}

sub set_polyline_id {