summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Model
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Travelynx/Model')
-rw-r--r--lib/Travelynx/Model/InTransit.pm132
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm11
-rw-r--r--lib/Travelynx/Model/Stations.pm82
-rw-r--r--lib/Travelynx/Model/Users.pm9
4 files changed, 210 insertions, 24 deletions
diff --git a/lib/Travelynx/Model/InTransit.pm b/lib/Travelynx/Model/InTransit.pm
index 43ecb90..2b9832c 100644
--- a/lib/Travelynx/Model/InTransit.pm
+++ b/lib/Travelynx/Model/InTransit.pm
@@ -104,6 +104,8 @@ sub add {
my $json = JSON->new;
if ($train) {
+
+ # IRIS
$db->insert(
'in_transit',
{
@@ -134,7 +136,9 @@ sub add {
}
);
}
- elsif ( $journey and $stop ) {
+ elsif ( $journey and $stop and $journey->can('id') ) {
+
+ # HAFAS
my @route;
my $product = $journey->product_at( $stop->loc->eva )
// $journey->product;
@@ -188,6 +192,56 @@ sub add {
}
);
}
+ elsif ( $journey and $stop ) {
+
+ # DBRIS
+ my @route;
+ for my $j_stop ( $journey->route ) {
+ push(
+ @route,
+ [
+ $j_stop->name,
+ $j_stop->eva,
+ {
+ sched_arr => _epoch( $j_stop->sched_arr ),
+ sched_dep => _epoch( $j_stop->sched_dep ),
+ rt_arr => _epoch( $j_stop->rt_arr ),
+ rt_dep => _epoch( $j_stop->rt_dep ),
+ arr_delay => $j_stop->arr_delay,
+ dep_delay => $j_stop->dep_delay,
+ load => undef,
+ lat => $j_stop->lat,
+ lon => $j_stop->lon,
+ }
+ ]
+ );
+ }
+ $db->insert(
+ 'in_transit',
+ {
+ user_id => $uid,
+ cancelled => $stop->{dep_cancelled}
+ ? 1
+ : 0,
+ checkin_station_id => $stop->eva,
+ checkin_time => DateTime->now( time_zone => 'Europe/Berlin' ),
+ dep_platform => $stop->platform,
+ train_type => $journey->type,
+ train_no => $journey->number,
+ train_id => $data->{trip_id},
+ sched_departure => $stop->sched_dep,
+ real_departure => $stop->rt_dep // $stop->sched_dep,
+ route => $json->encode( \@route ),
+ data => JSON->new->encode(
+ {
+ rt => $stop->{rt_dep} ? 1 : 0,
+ %{ $data // {} }
+ }
+ ),
+ backend_id => $backend_id,
+ }
+ );
+ }
else {
die('neither train nor journey specified');
}
@@ -731,6 +785,33 @@ sub update_departure_cancelled {
return $rows;
}
+sub update_departure_dbris {
+ my ( $self, %opt ) = @_;
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $dep_eva = $opt{dep_eva};
+ my $arr_eva = $opt{arr_eva};
+ my $journey = $opt{journey};
+ my $stop = $opt{stop};
+ my $json = JSON->new;
+
+ # selecting on user_id and train_no avoids a race condition if a user checks
+ # into a new train while we are fetching data for their previous journey. In
+ # this case, the new train would receive data from the previous journey.
+ $db->update(
+ 'in_transit',
+ {
+ real_departure => $stop->{rt_dep},
+ },
+ {
+ user_id => $uid,
+ train_id => $opt{train_id},
+ checkin_station_id => $dep_eva,
+ checkout_station_id => $arr_eva,
+ }
+ );
+}
+
sub update_departure_hafas {
my ( $self, %opt ) = @_;
my $uid = $opt{uid};
@@ -800,6 +881,55 @@ sub update_arrival {
return $rows;
}
+sub update_arrival_dbris {
+ my ( $self, %opt ) = @_;
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $dep_eva = $opt{dep_eva};
+ my $arr_eva = $opt{arr_eva};
+ my $journey = $opt{journey};
+ my $stop = $opt{stop};
+ my $json = JSON->new;
+
+ my @route;
+ for my $j_stop ( $journey->route ) {
+ push(
+ @route,
+ [
+ $j_stop->name,
+ $j_stop->eva,
+ {
+ sched_arr => _epoch( $j_stop->sched_arr ),
+ sched_dep => _epoch( $j_stop->sched_dep ),
+ rt_arr => _epoch( $j_stop->rt_arr ),
+ rt_dep => _epoch( $j_stop->rt_dep ),
+ arr_delay => $j_stop->arr_delay,
+ dep_delay => $j_stop->dep_delay,
+ lat => $j_stop->lat,
+ lon => $j_stop->lon,
+ }
+ ]
+ );
+ }
+
+ # selecting on user_id and train_no avoids a race condition if a user checks
+ # into a new train while we are fetching data for their previous journey. In
+ # this case, the new train would receive data from the previous journey.
+ $db->update(
+ 'in_transit',
+ {
+ real_arrival => $stop->{rt_arr},
+ route => $json->encode( [@route] ),
+ },
+ {
+ user_id => $uid,
+ train_id => $opt{train_id},
+ checkin_station_id => $dep_eva,
+ checkout_station_id => $arr_eva,
+ }
+ );
+}
+
sub update_arrival_hafas {
my ( $self, %opt ) = @_;
my $uid = $opt{uid};
diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm
index 905c426..f5bc9f1 100755
--- a/lib/Travelynx/Model/Journeys.pm
+++ b/lib/Travelynx/Model/Journeys.pm
@@ -549,7 +549,7 @@ sub get {
my @select
= (
- qw(journey_id is_iris is_hafas 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_lat dep_lon checkout_ts sched_arr_ts real_arr_ts arr_eva arr_ds100 arr_name arr_lat arr_lon cancelled edited route messages user_data visibility effective_visibility)
+ qw(journey_id is_dbris is_iris is_hafas 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_lat dep_lon checkout_ts sched_arr_ts real_arr_ts arr_eva arr_ds100 arr_name arr_lat arr_lon cancelled edited route messages user_data visibility effective_visibility)
);
my %where = (
user_id => $uid,
@@ -607,6 +607,7 @@ sub get {
my $ref = {
id => $entry->{journey_id},
+ is_dbris => $entry->{is_dbris},
is_iris => $entry->{is_iris},
is_hafas => $entry->{is_hafas},
backend_name => $entry->{backend_name},
@@ -870,8 +871,8 @@ sub get_latest_checkout_stations {
my $res = $db->select(
'journeys_str',
[
- 'arr_name', 'arr_eva', 'train_id', 'backend_id',
- 'backend_name', 'is_hafas'
+ 'arr_name', 'arr_eva', 'train_id', 'backend_id',
+ 'backend_name', 'is_dbris', 'is_hafas'
],
{
user_id => $uid,
@@ -895,6 +896,7 @@ sub get_latest_checkout_stations {
{
name => $row->{arr_name},
eva => $row->{arr_eva},
+ dbris => $row->{is_dbris} ? $row->{backend_name} : 0,
hafas => $row->{is_hafas} ? $row->{backend_name} : 0,
backend_id => $row->{backend_id},
}
@@ -1883,7 +1885,8 @@ sub get_connection_targets {
);
my @destinations
= $res->hashes->grep( sub { shift->{count} >= $min_count } )
- ->map( sub { shift->{dest} } )->each;
+ ->map( sub { shift->{dest} } )
+ ->each;
@destinations = $self->{stations}->get_by_evas(
backend_id => $opt{backend_id},
evas => [@destinations]
diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm
index 76fd452..3d6549f 100644
--- a/lib/Travelynx/Model/Stations.pm
+++ b/lib/Travelynx/Model/Stations.pm
@@ -25,11 +25,25 @@ sub get_backend_id {
if ( $opt{hafas} and $self->{backend_id}{hafas}{ $opt{hafas} } ) {
return $self->{backend_id}{hafas}{ $opt{hafas} };
}
+ if ( $opt{dbris} and $self->{backend_id}{dbris}{ $opt{dbris} } ) {
+ return $self->{backend_id}{dbris}{ $opt{dbris} };
+ }
my $db = $opt{db} // $self->{pg}->db;
my $backend_id = 0;
- if ( $opt{hafas} ) {
+ if ( $opt{dbris} ) {
+ $backend_id = $db->select(
+ 'backends',
+ ['id'],
+ {
+ dbris => 1,
+ name => $opt{dbris}
+ }
+ )->hash->{id};
+ $self->{backend_id}{dbris}{ $opt{dbris} } = $backend_id;
+ }
+ elsif ( $opt{hafas} ) {
$backend_id = $db->select(
'backends',
['id'],
@@ -44,31 +58,25 @@ sub get_backend_id {
return $backend_id;
}
-sub get_hafas_name {
+sub get_backend {
my ( $self, %opt ) = @_;
- if ( exists $self->{hafas_name}{ $opt{backend_id} } ) {
- return $self->{hafas_name}{ $opt{backend_id} };
+ if ( $self->{backend_cache}{ $opt{backend_id} } ) {
+ return $self->{backend_cache}{ $opt{backend_id} };
}
- my $db = $opt{db} // $self->{pg}->db;
- my $hafas_name;
+ my $db = $opt{db} // $self->{pg}->db;
my $ret = $db->select(
'backends',
- ['name'],
+ '*',
{
- hafas => 1,
- id => $opt{backend_id},
+ id => $opt{backend_id},
}
)->hash;
- if ($ret) {
- $hafas_name = $ret->{name};
- }
-
- $self->{hafas_name}{ $opt{backend_id} } = $hafas_name;
+ $self->{backend_cache}{ $opt{backend_id} } = $ret;
- return $hafas_name;
+ return $ret;
}
sub get_backends {
@@ -76,7 +84,8 @@ sub get_backends {
$opt{db} //= $self->{pg}->db;
- my $res = $opt{db}->select( 'backends', [ 'id', 'name', 'iris', 'hafas' ] );
+ my $res = $opt{db}
+ ->select( 'backends', [ 'id', 'name', 'iris', 'hafas', 'dbris' ] );
my @ret;
while ( my $row = $res->hash ) {
@@ -86,6 +95,7 @@ sub get_backends {
id => $row->{id},
name => $row->{name},
iris => $row->{iris},
+ dbris => $row->{dbris},
hafas => $row->{hafas},
}
);
@@ -97,11 +107,49 @@ sub get_backends {
sub add_or_update {
my ( $self, %opt ) = @_;
my $stop = $opt{stop};
- my $loc = $stop->loc;
$opt{db} //= $self->{pg}->db;
$opt{backend_id} //= $self->get_backend_id(%opt);
+ if ( $opt{dbris} ) {
+ if (
+ my $s = $self->get_by_eva(
+ $stop->eva,
+ db => $opt{db},
+ backend_id => $opt{backend_id}
+ )
+ )
+ {
+ $opt{db}->update(
+ 'stations',
+ {
+ name => $stop->name,
+ lat => $stop->lat,
+ lon => $stop->lon,
+ archived => 0
+ },
+ {
+ eva => $stop->eva,
+ source => $opt{backend_id}
+ }
+ );
+ return;
+ }
+ $opt{db}->insert(
+ 'stations',
+ {
+ eva => $stop->eva,
+ name => $stop->name,
+ lat => $stop->lat,
+ lon => $stop->lon,
+ source => $opt{backend_id},
+ archived => 0
+ }
+ );
+ return;
+ }
+
+ my $loc = $stop->loc;
if (
my $s = $self->get_by_eva(
$loc->eva,
diff --git a/lib/Travelynx/Model/Users.pm b/lib/Travelynx/Model/Users.pm
index 7d3777b..e3d6f7a 100644
--- a/lib/Travelynx/Model/Users.pm
+++ b/lib/Travelynx/Model/Users.pm
@@ -209,7 +209,11 @@ sub set_backend {
my ( $self, %opt ) = @_;
$opt{db} //= $self->{pg}->db;
- $opt{db}->update('users', {backend_id => $opt{backend_id}}, {id => $opt{uid}});
+ $opt{db}->update(
+ 'users',
+ { backend_id => $opt{backend_id} },
+ { id => $opt{uid} }
+ );
}
sub set_privacy {
@@ -414,7 +418,7 @@ sub get {
. 'extract(epoch from registered_at) as registered_at_ts, '
. 'extract(epoch from last_seen) as last_seen_ts, '
. 'extract(epoch from deletion_requested) as deletion_requested_ts, '
- . 'backend_id, backend_name, hafas',
+ . 'backend_id, backend_name, hafas, dbris',
{ id => $uid }
)->hash;
if ($user) {
@@ -453,6 +457,7 @@ sub get {
: undef,
backend_id => $user->{backend_id},
backend_name => $user->{backend_name},
+ backend_dbris => $user->{dbris},
backend_hafas => $user->{hafas},
};
}