Use one row per journey instead of split checkin/checkout entries

Whether a user is in transit or not is now determined by an entry in the
in_transit table instead of a dangling checkin.

All completed journeys are stored in the "journeys" table.

This does most of the work needed for automatic checkout. However, note that
the corresponding worker process is not implemented yet.
This commit is contained in:
Daniel Friesel 2019-04-23 18:08:07 +02:00
parent 812be4f0cb
commit e168d9cd39
15 changed files with 782 additions and 588 deletions

View file

@ -191,9 +191,10 @@ sub startup {
$self->helper( $self->helper(
'get_departures' => sub { 'get_departures' => sub {
my ( $self, $station, $lookbehind ) = @_; my ( $self, $station, $lookbehind, $lookahead ) = @_;
$lookbehind //= 180; $lookbehind //= 180;
$lookahead //= 30;
my @station_matches my @station_matches
= Travel::Status::DE::IRIS::Stations::get_station($station); = Travel::Status::DE::IRIS::Stations::get_station($station);
@ -207,7 +208,7 @@ sub startup {
lookbehind => 20, lookbehind => 20,
datetime => DateTime->now( time_zone => 'Europe/Berlin' ) datetime => DateTime->now( time_zone => 'Europe/Berlin' )
->subtract( minutes => $lookbehind ), ->subtract( minutes => $lookbehind ),
lookahead => $lookbehind + 10, lookahead => $lookbehind + $lookahead,
); );
return { return {
results => [ $status->results ], results => [ $status->results ],
@ -241,6 +242,10 @@ sub startup {
'add_journey' => sub { 'add_journey' => sub {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
$self->app->log->error(
"add_journey is not implemented at the moment");
return ( undef, undef, 'not implemented' );
my $user_status = $self->get_user_status; my $user_status = $self->get_user_status;
if ( $user_status->{checked_in} or $user_status->{cancelled} ) { if ( $user_status->{checked_in} or $user_status->{cancelled} ) {
@ -326,11 +331,9 @@ sub startup {
$self->helper( $self->helper(
'checkin' => sub { 'checkin' => sub {
my ( $self, $station, $train_id, $action_id ) = @_; my ( $self, $station, $train_id ) = @_;
$action_id //= $self->app->action_type->{checkin}; my $status = $self->get_departures( $station, 140, 30 );
my $status = $self->get_departures($station);
if ( $status->{errstr} ) { if ( $status->{errstr} ) {
return ( undef, $status->{errstr} ); return ( undef, $status->{errstr} );
} }
@ -343,38 +346,33 @@ sub startup {
else { else {
my $user = $self->get_user_status; my $user = $self->get_user_status;
if ( $user->{checked_in} ) { if ( $user->{checked_in} or $user->{cancelled} ) {
# If a user is already checked in, we assume that they forgot to # If a user is already checked in, we assume that they forgot to
# check out and do it for them. # check out and do it for them.
$self->checkout( $station, 1 ); $self->checkout( $station, 1 );
} }
elsif ( $user->{cancelled} ) {
# Same
$self->checkout( $station, 1,
$self->app->action_type->{cancelled_to} );
}
eval { eval {
$self->pg->db->insert( $self->pg->db->insert(
'user_actions', 'in_transit',
{ {
user_id => $self->current_user->{id}, user_id => $self->current_user->{id},
action_id => $action_id, cancelled => $train->departure_is_cancelled
station_id => $self->get_station_id( ? 1
: 0,
checkin_station_id => $self->get_station_id(
ds100 => $status->{station_ds100}, ds100 => $status->{station_ds100},
name => $status->{station_name} name => $status->{station_name}
), ),
action_time => checkin_time =>
DateTime->now( time_zone => 'Europe/Berlin' ), DateTime->now( time_zone => 'Europe/Berlin' ),
edited => 0,
train_type => $train->type, train_type => $train->type,
train_line => $train->line_no, train_line => $train->line_no,
train_no => $train->train_no, train_no => $train->train_no,
train_id => $train->train_id, train_id => $train->train_id,
sched_time => $train->sched_departure, sched_departure => $train->sched_departure,
real_time => $train->departure, real_departure => $train->departure,
route => join( '|', $train->route ), route => join( '|', $train->route ),
messages => join( messages => join(
'|', '|',
@ -389,7 +387,7 @@ sub startup {
if ($@) { if ($@) {
my $uid = $self->current_user->{id}; my $uid = $self->current_user->{id};
$self->app->log->error( $self->app->log->error(
"Checkin($uid, $action_id): INSERT failed: $@"); "Checkin($uid): INSERT failed: $@");
return ( undef, 'INSERT failed: ' . $@ ); return ( undef, 'INSERT failed: ' . $@ );
} }
return ( $train, undef ); return ( $train, undef );
@ -400,43 +398,86 @@ sub startup {
$self->helper( $self->helper(
'undo' => sub { 'undo' => sub {
my ( $self, $action_id ) = @_; my ( $self, $journey_id ) = @_;
my $uid = $self->current_user->{id};
my $status = $self->get_user_status; if ( $journey_id eq 'in_transit' ) {
eval {
if ( $action_id < 1 or $status->{action_id} != $action_id ) { $self->pg->db->delete( 'in_transit', { user_id => $uid } );
return };
"Invalid action ID: $action_id != $status->{action_id}. Note that you can only undo your latest action."; if ($@) {
$self->app->log->error("Undo($uid, $journey_id): $@");
return "Undo($journey_id): $@";
}
return undef;
}
if ( $journey_id !~ m{ ^ \d+ $ }x ) {
return 'Invalid Journey ID';
} }
eval { eval {
$self->pg->db->delete( 'user_actions', { id => $action_id } ); my $db = $self->pg->db;
my $tx = $db->begin;
my $journey = $db->select(
'journeys',
'*',
{
user_id => $uid,
id => $journey_id
}
)->hash;
$db->delete(
'journeys',
{
user_id => $uid,
id => $journey_id
}
);
if ( $journey->{edited} ) {
die(
"Cannot undo a journey which has already been edited. Please delete manually.\n"
);
}
delete $journey->{edited};
delete $journey->{id};
$db->insert( 'in_transit', $journey );
my $cache_ts = DateTime->now( time_zone => 'Europe/Berlin' );
if ( $journey->{real_departure}
=~ m{ ^ (?<year> \d{4} ) - (?<month> \d{2} ) }x )
{
$cache_ts->set(
year => $+{year},
month => $+{month}
);
}
$self->invalidate_stats_cache( $cache_ts, $db );
$tx->commit;
}; };
if ($@) { if ($@) {
my $uid = $self->current_user->{id}; $self->app->log->error("Undo($uid, $journey_id): $@");
$self->app->log->error( return "Undo($journey_id): $@";
"Undo($uid, $action_id): DELETE failed: $@");
return 'DELETE failed: ' . $@;
} }
return; return undef;
} }
); );
# Statistics are partitioned by real_departure, which must be provided
# when calling this function e.g. after journey deletion or editing.
# If a joureny's real_departure has been edited, this function must be
# called twice: once with the old and once with the new value.
$self->helper( $self->helper(
'invalidate_stats_cache' => sub { 'invalidate_stats_cache' => sub {
my ( $self, $ts ) = @_; my ( $self, $ts, $db ) = @_;
my $uid = $self->current_user->{id}; my $uid = $self->current_user->{id};
$ts //= DateTime->now( time_zone => 'Europe/Berlin' ); $db //= $self->pg->db;
# ts is the checkout timestamp or (for manual entries) the
# time of arrival. As the journey may span a month or year boundary,
# there is a total of five cache entries we need to invalidate:
# * year, month
# * year
# * (year, month) - 1 month (with wraparound)
# * (year) - 1 year
# * total stats
$self->pg->db->delete( $self->pg->db->delete(
'journey_stats', 'journey_stats',
@ -454,105 +495,82 @@ sub startup {
month => 0, month => 0,
} }
); );
$ts->subtract( months => 1 );
$self->pg->db->delete(
'journey_stats',
{
user_id => $uid,
year => $ts->year,
month => $ts->month,
}
);
$ts->subtract( months => 11 );
$self->pg->db->delete(
'journey_stats',
{
user_id => $uid,
year => $ts->year,
month => 0,
}
);
$self->pg->db->delete(
'journey_stats',
{
user_id => $uid,
year => 0,
month => 0,
}
);
} }
); );
$self->helper( $self->helper(
'checkout' => sub { 'checkout' => sub {
my ( $self, $station, $force, $action_id ) = @_; my ( $self, $station, $force ) = @_;
$action_id //= $self->app->action_type->{checkout}; my $db = $self->pg->db;
my $uid = $self->current_user->{id};
my $status = $self->get_departures( $station, 180 ); my $status = $self->get_departures( $station, 120, 120 );
my $user = $self->get_user_status; my $user = $self->get_user_status;
my $train_id = $user->{train_id}; my $train_id = $user->{train_id};
if ( not $user->{checked_in} and not $user->{cancelled} ) { if ( not $user->{checked_in} and not $user->{cancelled} ) {
return 'You are not checked into any train'; return ( 0, 'You are not checked into any train' );
} }
if ( $status->{errstr} and not $force ) { if ( $status->{errstr} and not $force ) {
return $status->{errstr}; return ( 1, $status->{errstr} );
} }
my $now = DateTime->now( time_zone => 'Europe/Berlin' ); my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my $journey
= $db->select( 'in_transit', '*', { user_id => $uid } )->hash;
my ($train) my ($train)
= first { $_->train_id eq $train_id } @{ $status->{results} }; = first { $_->train_id eq $train_id } @{ $status->{results} };
if ( not defined $train ) {
if ($force) { # Store the intended checkout station regardless of this operation's
eval { # success.
$self->pg->db->insert( my $new_checkout_station_id = $self->get_station_id(
'user_actions',
{
user_id => $self->current_user->{id},
action_id => $action_id,
station_id => $self->get_station_id(
ds100 => $status->{station_ds100}, ds100 => $status->{station_ds100},
name => $status->{station_name} name => $status->{station_name}
),
action_time => $now,
edited => 0
}
); );
}; $db->update(
if ($@) { 'in_transit',
my $uid = $self->current_user->{id};
$self->app->log->error(
"Force checkout($uid, $action_id): INSERT failed: $@"
);
return 'INSERT failed: ' . $@;
}
$self->invalidate_stats_cache;
return;
}
else {
return "Train ${train_id} not found";
}
}
else {
eval {
$self->pg->db->insert(
'user_actions',
{ {
user_id => $self->current_user->{id}, checkout_station_id => $new_checkout_station_id,
action_id => $action_id, },
station_id => $self->get_station_id( { user_id => $uid }
ds100 => $status->{station_ds100}, );
name => $status->{station_name}
), # If in_transit already contains arrival data for another estimated
action_time => $now, # destination, we must invalidate it.
edited => 0, if ( defined $journey->{checkout_station_id}
train_type => $train->type, and $journey->{checkout_station_id}
train_line => $train->line_no, != $new_checkout_station_id )
train_no => $train->train_no, {
train_id => $train->train_id, $db->update(
sched_time => $train->sched_arrival, 'in_transit',
real_time => $train->arrival, {
checkout_time => undef,
sched_arrival => undef,
real_arrival => undef,
},
{ user_id => $uid }
);
}
if ( not( defined $train or $force ) ) {
return ( 1, undef );
}
my $has_arrived = 0;
eval {
my $tx = $db->begin;
if ( defined $train ) {
$has_arrived = $train->arrival->epoch < $now->epoch ? 1 : 0;
$db->update(
'in_transit',
{
checkout_time => $now,
sched_arrival => $train->sched_arrival,
real_arrival => $train->arrival,
cancelled => $train->arrival_is_cancelled ? 1 : 0,
route => join( '|', $train->route ), route => join( '|', $train->route ),
messages => join( messages => join(
'|', '|',
@ -560,90 +578,124 @@ sub startup {
( $_->[0] ? $_->[0]->epoch : q{} ) . ':' ( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
. $_->[1] . $_->[1]
} $train->messages } $train->messages
) ),
} },
{ user_id => $uid }
); );
}
$journey
= $db->select( 'in_transit', '*', { user_id => $uid } )->hash;
if ( $has_arrived or $force ) {
$journey->{edited} = 0;
$journey->{checkout_time} = $now;
$db->insert( 'journeys', $journey );
$db->delete( 'in_transit', { user_id => $uid } );
my $cache_ts = $now->clone;
if ( $journey->{real_departure}
=~ m{ ^ (?<year> \d{4} ) - (?<month> \d{2} ) }x )
{
$cache_ts->set(
year => $+{year},
month => $+{month}
);
}
$self->invalidate_stats_cache( $cache_ts, $db );
}
$tx->commit;
}; };
if ($@) { if ($@) {
my $uid = $self->current_user->{id}; $self->app->log->error("Checkout($uid): $@");
$self->app->log->error( return ( 1, 'Checkout error: ' . $@ );
"Checkout($uid, $action_id): INSERT failed: $@");
return 'INSERT failed: ' . $@;
} }
$self->invalidate_stats_cache;
return; if ( $has_arrived or $force ) {
return ( 0, undef );
} }
return ( 1, undef );
} }
); );
$self->helper( $self->helper(
'update_journey_part' => sub { 'update_journey_part' => sub {
my ( $self, $db, $checkin_id, $checkout_id, $key, $value ) = @_; my ( $self, $db, $journey_id, $key, $value ) = @_;
my $rows; my $rows;
my $journey = $self->get_journey(
db => $db,
journey_id => $journey_id,
);
eval { eval {
if ( $key eq 'sched_departure' ) { if ( $key eq 'sched_departure' ) {
$rows = $db->update( $rows = $db->update(
'user_actions', 'journeys',
{ {
sched_time => $value, sched_departure => $value,
edited => $journey->{edited} | 0x0001,
}, },
{ {
id => $checkin_id, id => $journey_id,
action_id => $self->app->action_type->{checkin},
} }
)->rows; )->rows;
} }
elsif ( $key eq 'rt_departure' ) { elsif ( $key eq 'rt_departure' ) {
$rows = $db->update( $rows = $db->update(
'user_actions', 'journeys',
{ {
real_time => $value, real_departure => $value,
edited => $journey->{edited} | 0x0002,
}, },
{ {
id => $checkin_id, id => $journey_id,
action_id => $self->app->action_type->{checkin},
} }
)->rows; )->rows;
# stats are partitioned by rt_departure -> both the cache for
# the old value (see bottom of this function) and the new value
# (here) must be invalidated.
$self->invalidate_stats_cache( $value, $db );
} }
elsif ( $key eq 'sched_arrival' ) { elsif ( $key eq 'sched_arrival' ) {
$rows = $db->update( $rows = $db->update(
'user_actions', 'journeys',
{ {
sched_time => $value, sched_arrival => $value,
edited => $journey->{edited} | 0x0100,
}, },
{ {
id => $checkout_id, id => $journey_id,
action_id => $self->app->action_type->{checkout},
} }
)->rows; )->rows;
} }
elsif ( $key eq 'rt_arrival' ) { elsif ( $key eq 'rt_arrival' ) {
$rows = $db->update( $rows = $db->update(
'user_actions', 'journeys',
{ {
real_time => $value, real_arrival => $value,
edited => $journey->{edited} | 0x0200,
}, },
{ {
id => $checkout_id, id => $journey_id,
action_id => $self->app->action_type->{checkout},
} }
)->rows; )->rows;
} }
else { else {
$self->app->log->error( die("Invalid key $key\n");
"update_journey_part($checkin_id, $checkout_id): Invalid key $key"
);
} }
}; };
if ($@) { if ($@) {
$self->app->log->error( $self->app->log->error(
"update_journey_part($checkin_id, $checkout_id): UPDATE failed: $@" "update_journey_part($journey_id, $key): $@");
); return "update_journey_part($key): $@";
return 'UPDATE failed: ' . $@;
} }
if ( $rows == 1 ) { if ( $rows == 1 ) {
$self->invalidate_stats_cache( $journey->{rt_departure}, $db );
return undef; return undef;
} }
return 'UPDATE failed: did not match any journey part'; return 'UPDATE failed: did not match any journey part';
@ -930,14 +982,12 @@ sub startup {
$self->helper( $self->helper(
'delete_journey' => sub { 'delete_journey' => sub {
my ( $self, $checkin_id, $checkout_id, $checkin_epoch, my ( $self, $journey_id, $checkin_epoch, $checkout_epoch ) = @_;
$checkout_epoch )
= @_;
my $uid = $self->current_user->{id}; my $uid = $self->current_user->{id};
my @journeys = $self->get_user_travels( my @journeys = $self->get_user_travels(
uid => $uid, uid => $uid,
checkout_id => $checkout_id journey_id => $journey_id
); );
if ( @journeys == 0 ) { if ( @journeys == 0 ) {
return 'Journey not found'; return 'Journey not found';
@ -947,8 +997,7 @@ sub startup {
# Double-check (comparing both ID and action epoch) to make sure we # Double-check (comparing both ID and action epoch) to make sure we
# are really deleting the right journey and the user isn't just # are really deleting the right journey and the user isn't just
# playing around with POST requests. # playing around with POST requests.
if ( $journey->{ids}[0] != $checkin_id if ( $journey->{id} != $journey_id
or $journey->{ids}[1] != $checkout_id
or $journey->{checkin}->epoch != $checkin_epoch or $journey->{checkin}->epoch != $checkin_epoch
or $journey->{checkout}->epoch != $checkout_epoch ) or $journey->{checkout}->epoch != $checkout_epoch )
{ {
@ -958,26 +1007,24 @@ sub startup {
my $rows; my $rows;
eval { eval {
$rows = $self->pg->db->delete( $rows = $self->pg->db->delete(
'user_actions', 'journeys',
{ {
user_id => $uid, user_id => $uid,
id => [ $checkin_id, $checkout_id ] id => $journey_id,
} }
)->rows; )->rows;
}; };
if ($@) { if ($@) {
$self->app->log->error( $self->app->log->error("Delete($uid, $journey_id): $@");
"Delete($uid, $checkin_id, $checkout_id): DELETE failed: $@"
);
return 'DELETE failed: ' . $@; return 'DELETE failed: ' . $@;
} }
if ( $rows == 2 ) { if ( $rows == 1 ) {
$self->invalidate_stats_cache( $journey->{checkout} ); $self->invalidate_stats_cache( $journey->{rt_departure} );
return undef; return undef;
} }
return sprintf( 'Deleted %d rows, expected 2', $rows ); return sprintf( 'Deleted %d rows, expected 1', $rows );
} }
); );
@ -1075,146 +1122,67 @@ sub startup {
# Otherwise, we grab a fresh one. # Otherwise, we grab a fresh one.
my $db = $opt{db} // $self->pg->db; my $db = $opt{db} // $self->pg->db;
my $selection = qq{ my %where = (
user_actions.id as action_log_id, action_id, user_id => $uid,
extract(epoch from action_time) as action_time_ts, cancelled => 0
stations.ds100 as ds100, stations.name as name, );
train_type, train_line, train_no, train_id,
extract(epoch from sched_time) as sched_time_ts,
extract(epoch from real_time) as real_time_ts,
route, messages, edited
};
$selection =~ tr{\n}{}d;
my %where = ( user_id => $uid );
my %order = ( my %order = (
order_by => { order_by => {
-desc => 'action_time', -desc => 'real_dep_ts',
} }
); );
if ( $opt{cancelled} ) {
$where{cancelled} = 1;
}
if ( $opt{limit} ) { if ( $opt{limit} ) {
$order{limit} = 10; $order{limit} = $opt{limit};
} }
if ( $opt{checkout_id} ) { if ( $opt{journey_id} ) {
$where{'user_actions.id'} = { '<=', $opt{checkout_id} }; $where{journey_id} = $opt{journey_id};
$order{limit} = 2; delete $where{cancelled};
} }
elsif ( $opt{after} and $opt{before} ) { elsif ( $opt{after} and $opt{before} ) {
$where{real_dep_ts} = {
# Each journey consists of exactly two database entries: one for -between => [ $opt{after}->epoch, $opt{before}->epoch, ] };
# checkin, one for checkout. A simple query using e.g.
# after = YYYY-01-01T00:00:00 and before YYYY-02-01T00:00:00
# will miss journeys where checkin and checkout take place in
# different months.
# We therefore add one day to the before timestamp and filter out
# journeys whose checkin lies outside the originally requested
# time range afterwards.
# For an additional twist, get_interval_actions_query filters based
# on the action time, not actual departure, as force
# checkout actions lack sched_time and real_time data. By
# subtracting one day from "after" (i.e., moving it one day into
# the past), we make sure not to miss journeys where the real departure
# time falls into the interval, but the checkin time does not.
# Again, this is addressed in postprocessing at the bottom of this
# helper.
# This works under the assumption that there are no DB trains whose
# journey takes more than 24 hours. If this no longer holds,
# please adjust the intervals accordingly.
$where{action_time} = {
-between => [
$opt{after}->clone->subtract( days => 1 ),
$opt{before}->clone->add( days => 1 )
]
};
}
my @match_actions = (
$self->app->action_type->{checkout},
$self->app->action_type->{checkin}
);
if ( $opt{cancelled} ) {
@match_actions = (
$self->app->action_type->{cancelled_to},
$self->app->action_type->{cancelled_from}
);
} }
my @travels; my @travels;
my $prev_action = 0;
my $res = $db->select( my $res = $db->select( 'journeys_str', '*', \%where, \%order );
[
'user_actions',
[
-left => 'stations',
id => 'station_id'
]
],
$selection,
\%where,
\%order
);
for my $entry ( $res->hashes->each ) { for my $entry ( $res->hashes->each ) {
if ( $entry->{action_id} == $match_actions[0] my $ref = {
or ( $opt{checkout_id} and not @travels ) ) id => $entry->{journey_id},
{
push(
@travels,
{
ids => [ undef, $entry->{action_log_id} ],
to_name => $entry->{name},
sched_arrival =>
epoch_to_dt( $entry->{sched_time_ts} ),
rt_arrival => epoch_to_dt( $entry->{real_time_ts} ),
checkout => epoch_to_dt( $entry->{action_time_ts} ),
type => $entry->{train_type}, type => $entry->{train_type},
line => $entry->{train_line}, line => $entry->{train_line},
no => $entry->{train_no}, no => $entry->{train_no},
from_name => $entry->{dep_name},
checkin => epoch_to_dt( $entry->{checkin_ts} ),
sched_departure => epoch_to_dt( $entry->{sched_dep_ts} ),
rt_departure => epoch_to_dt( $entry->{real_dep_ts} ),
to_name => $entry->{arr_name},
checkout => epoch_to_dt( $entry->{checkout_ts} ),
sched_arrival => epoch_to_dt( $entry->{sched_arr_ts} ),
rt_arrival => epoch_to_dt( $entry->{real_arr_ts} ),
messages => $entry->{messages} messages => $entry->{messages}
? [ split( qr{[|]}, $entry->{messages} ) ] ? [ split( qr{[|]}, $entry->{messages} ) ]
: undef, : undef,
route => $entry->{route} route => $entry->{route}
? [ split( qr{[|]}, $entry->{route} ) ] ? [ split( qr{[|]}, $entry->{route} ) ]
: undef, : undef,
completed => 0, edited => $entry->{edited},
edited => $entry->{edited} << 8, };
}
);
}
elsif (
(
$entry->{action_id} == $match_actions[1]
and $prev_action == $match_actions[0]
)
or $opt{checkout_id}
)
{
my $ref = $travels[-1];
$ref->{ids}->[0] = $entry->{action_log_id};
$ref->{from_name} = $entry->{name};
$ref->{completed} = 1;
$ref->{sched_departure}
= epoch_to_dt( $entry->{sched_time_ts} );
$ref->{rt_departure}
= epoch_to_dt( $entry->{real_time_ts} );
$ref->{checkin} = epoch_to_dt( $entry->{action_time_ts} );
$ref->{type} //= $entry->{train_type};
$ref->{line} //= $entry->{train_line};
$ref->{no} //= $entry->{train_no};
$ref->{messages}
//= [ split( qr{[|]}, $entry->{messages} ) ];
$ref->{route} //= [ split( qr{[|]}, $entry->{route} ) ];
$ref->{edited} |= $entry->{edited};
if ( $opt{verbose} ) { if ( $opt{verbose} ) {
$ref->{cancelled} = $entry->{cancelled};
my @parsed_messages; my @parsed_messages;
for my $message ( @{ $ref->{messages} // [] } ) { for my $message ( @{ $ref->{messages} // [] } ) {
my ( $ts, $msg ) = split( qr{:}, $message ); my ( $ts, $msg ) = split( qr{:}, $message );
push( @parsed_messages, push( @parsed_messages, [ epoch_to_dt($ts), $msg ] );
[ epoch_to_dt($ts), $msg ] );
} }
$ref->{messages} = [ reverse @parsed_messages ]; $ref->{messages} = [ reverse @parsed_messages ];
$ref->{sched_duration} $ref->{sched_duration}
@ -1224,8 +1192,7 @@ sub startup {
: undef; : undef;
$ref->{rt_duration} $ref->{rt_duration}
= $ref->{rt_arrival} = $ref->{rt_arrival}
? $ref->{rt_arrival}->epoch ? $ref->{rt_arrival}->epoch - $ref->{rt_departure}->epoch
- $ref->{rt_departure}->epoch
: undef; : undef;
my ( $km, $skip ) my ( $km, $skip )
= $self->get_travel_distance( $ref->{from_name}, = $self->get_travel_distance( $ref->{from_name},
@ -1248,30 +1215,10 @@ sub startup {
? $ref->{km_beeline} / $kmh_divisor ? $ref->{km_beeline} / $kmh_divisor
: -1; : -1;
} }
if ( $opt{checkout_id}
and $entry->{action_id}
== $self->app->action_type->{cancelled_from} )
{
$ref->{cancelled} = 1;
}
}
$prev_action = $entry->{action_id};
}
if ( $opt{before} and $opt{after} ) { push( @travels, $ref );
@travels = grep {
$_->{rt_departure} >= $opt{after}
and $_->{rt_departure} < $opt{before}
} @travels;
} }
# user_actions are sorted by action_time. As users are allowed to check
# into trains in arbitrary order, action_time does not always
# correspond to departure/arrival time, so we ensure a proper sort
# order here.
@travels
= sort { $b->{rt_departure} <=> $a->{rt_departure} } @travels;
return @travels; return @travels;
} }
); );
@ -1280,11 +1227,9 @@ sub startup {
'get_journey' => sub { 'get_journey' => sub {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
$opt{cancelled} = 'any';
my @journeys = $self->get_user_travels(%opt); my @journeys = $self->get_user_travels(%opt);
if ( @journeys == 0 if ( @journeys == 0 ) {
or not $journeys[0]{completed}
or $journeys[0]{ids}[1] != $opt{checkout_id} )
{
return undef; return undef;
} }
@ -1298,86 +1243,96 @@ sub startup {
$uid //= $self->current_user->{id}; $uid //= $self->current_user->{id};
my $selection = qq{ my $db = $self->pg->db;
user_actions.id as action_log_id, action_id,
extract(epoch from action_time) as action_time_ts,
stations.ds100 as ds100, stations.name as name,
train_type, train_line, train_no, train_id,
extract(epoch from sched_time) as sched_time_ts,
extract(epoch from real_time) as real_time_ts,
route
};
$selection =~ tr{\n}{}d;
my $res = $self->pg->db->select(
[
'user_actions',
[
-left => 'stations',
id => 'station_id'
]
],
$selection,
{
user_id => $uid,
},
{
order_by => {
-desc => 'action_time',
},
limit => 1,
}
);
my $status = $res->hash;
if ($status) {
my $now = DateTime->now( time_zone => 'Europe/Berlin' ); my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my $action_ts = epoch_to_dt( $status->{action_time_ts} ); my $in_transit
my $sched_ts = epoch_to_dt( $status->{sched_time_ts} ); = $db->select( 'in_transit_str', '*', { user_id => $uid } )->hash;
my $real_ts = epoch_to_dt( $status->{real_time_ts} );
my $checkin_station_name = $status->{name}; if ($in_transit) {
my @route = split( qr{[|]}, $status->{route} // q{} );
my @route = split( qr{[|]}, $in_transit->{route} // q{} );
my @route_after; my @route_after;
my $is_after = 0; my $is_after = 0;
for my $station (@route) { for my $station (@route) {
if ( $station eq $checkin_station_name ) { if ( $station eq $in_transit->{dep_name} ) {
$is_after = 1; $is_after = 1;
} }
if ($is_after) { if ($is_after) {
push( @route_after, $station ); push( @route_after, $station );
} }
} }
my $ts = $in_transit->{checkout_ts}
// $in_transit->{checkin_ts};
my $action_time = epoch_to_dt($ts);
return { return {
checked_in => ( checked_in => !$in_transit->{cancelled},
$status->{action_id} cancelled => $in_transit->{cancelled},
== $self->app->action_type->{checkin} timestamp => $action_time,
), timestamp_delta => $now->epoch - $action_time->epoch,
cancelled => ( train_type => $in_transit->{train_type},
$status->{action_id} train_line => $in_transit->{train_line},
== $self->app->action_type->{cancelled_from} train_no => $in_transit->{train_no},
), train_id => $in_transit->{train_id},
timestamp => $action_ts, sched_departure =>
timestamp_delta => $now->epoch - $action_ts->epoch, epoch_to_dt( $in_transit->{sched_dep_ts} ),
action_id => $status->{action_log_id}, real_departure => epoch_to_dt( $in_transit->{real_dep_ts} ),
sched_ts => $sched_ts, dep_ds100 => $in_transit->{dep_ds100},
real_ts => $real_ts, dep_name => $in_transit->{dep_name},
station_ds100 => $status->{ds100}, sched_arrival => epoch_to_dt( $in_transit->{sched_arr_ts} ),
station_name => $checkin_station_name, real_arrival => epoch_to_dt( $in_transit->{real_arr_ts} ),
train_type => $status->{train_type}, arr_ds100 => $in_transit->{arr_ds100},
train_line => $status->{train_line}, arr_name => $in_transit->{arr_name},
train_no => $status->{train_no},
train_id => $status->{train_id},
route => \@route,
route_after => \@route_after, route_after => \@route_after,
}; };
} }
my $latest = $db->select(
'journeys_str',
'*',
{
user_id => $uid,
cancelled => 0
},
{
order_by => { -desc => 'journey_id' },
limit => 1
}
)->hash;
if ($latest) {
my $ts = $latest->{checkout_ts};
my $action_time = epoch_to_dt($ts);
return { return {
checked_in => 0, checked_in => 0,
cancelled => 0,
journey_id => $latest->{journey_id},
timestamp => $action_time,
timestamp_delta => $now->epoch - $action_time->epoch,
train_type => $latest->{train_type},
train_line => $latest->{train_line},
train_no => $latest->{train_no},
train_id => $latest->{train_id},
sched_departure => epoch_to_dt( $latest->{sched_dep_ts} ),
real_departure => epoch_to_dt( $latest->{real_dep_ts} ),
dep_ds100 => $latest->{dep_ds100},
dep_name => $latest->{dep_name},
sched_arrival => epoch_to_dt( $latest->{sched_arr_ts} ),
real_arrival => epoch_to_dt( $latest->{real_arr_ts} ),
arr_ds100 => $latest->{arr_ds100},
arr_name => $latest->{arr_name},
};
}
return {
checked_in => 0,
cancelled => 0,
no_journeys_yet => 1,
timestamp => epoch_to_dt(0), timestamp => epoch_to_dt(0),
sched_ts => epoch_to_dt(0), timestamp_delta => $now->epoch,
real_ts => epoch_to_dt(0),
}; };
} }
); );

View file

@ -12,9 +12,11 @@ sub get_schema_version {
my $version; my $version;
eval { eval {
$version = $db->select( 'schema_version', ['version'] )->hash->{version}; $version
= $db->select( 'schema_version', ['version'] )->hash->{version};
}; };
if ($@) { if ($@) {
# If it failed, the version table does not exist -> run setup first. # If it failed, the version table does not exist -> run setup first.
return undef; return undef;
} }
@ -124,6 +126,210 @@ my @migrations = (
} }
); );
}, },
# v3 -> v4
# Introduces "journeys", containing one row for each complete
# journey, and "in_transit", containing the journey which is currently
# in progress (if any). "user_actions" is no longer used, but still kept
# as a backup for now.
sub {
my ($db) = @_;
$db->query(
qq{
create table journeys (
id serial not null primary key,
user_id integer not null references users (id),
train_type varchar(16) not null,
train_line varchar(16),
train_no varchar(16) not null,
train_id varchar(128) not null,
checkin_station_id integer not null references stations (id),
checkin_time timestamptz not null,
sched_departure timestamptz not null,
real_departure timestamptz not null,
checkout_station_id integer not null references stations (id),
checkout_time timestamptz not null,
sched_arrival timestamptz,
real_arrival timestamptz,
cancelled boolean not null,
edited smallint not null,
route text,
messages text
);
create table in_transit (
user_id integer not null references users (id) primary key,
train_type varchar(16) not null,
train_line varchar(16),
train_no varchar(16) not null,
train_id varchar(128) not null,
checkin_station_id integer not null references stations (id),
checkin_time timestamptz not null,
sched_departure timestamptz not null,
real_departure timestamptz not null,
checkout_station_id int references stations (id),
checkout_time timestamptz,
sched_arrival timestamptz,
real_arrival timestamptz,
cancelled boolean not null,
route text,
messages text
);
create view journeys_str as select
journeys.id as journey_id, user_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,
dep_stations.ds100 as dep_ds100,
dep_stations.name as dep_name,
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,
arr_stations.ds100 as arr_ds100,
arr_stations.name as arr_name,
cancelled, edited, route, messages
from journeys
join stations as dep_stations on dep_stations.id = checkin_station_id
join stations as arr_stations on arr_stations.id = checkout_station_id
;
create view in_transit_str as select
user_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,
dep_stations.ds100 as dep_ds100,
dep_stations.name as dep_name,
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,
arr_stations.ds100 as arr_ds100,
arr_stations.name as arr_name,
cancelled, route, messages
from in_transit
join stations as dep_stations on dep_stations.id = checkin_station_id
left join stations as arr_stations on arr_stations.id = checkout_station_id
;
}
);
my @uids
= $db->select( 'users', ['id'] )->hashes->map( sub { shift->{id} } )
->each;
my $count = 0;
for my $uid (@uids) {
my %cache;
my $prev_action_type = 0;
my $actions = $db->select(
'user_actions', '*',
{ user_id => $uid },
{ order_by => { -asc => 'id' } }
);
for my $action ( $actions->hashes->each ) {
my $action_type = $action->{action_id};
my $id = $action->{id};
if ( $action_type == 2 and $prev_action_type != 1 ) {
die(
"Inconsistent data at uid ${uid} action ${id}: Illegal transition $prev_action_type -> $action_type.\n"
);
}
if ( $action_type == 5 and $prev_action_type != 4 ) {
die(
"Inconsistent data at uid ${uid} action ${id}: Illegal transition $prev_action_type -> $action_type.\n"
);
}
if ( $action_type == 1 or $action_type == 4 ) {
%cache = (
train_type => $action->{train_type},
train_line => $action->{train_line},
train_no => $action->{train_no},
train_id => $action->{train_id},
checkin_station_id => $action->{station_id},
checkin_time => $action->{action_time},
sched_departure => $action->{sched_time},
real_departure => $action->{real_time},
route => $action->{route},
messages => $action->{messages},
cancelled => $action->{action_id} == 4 ? 1 : 0,
edited => $action->{edited},
);
}
elsif ( $action_type == 2 or $action_type == 5 ) {
$cache{checkout_station_id} = $action->{station_id};
$cache{checkout_time} = $action->{action_time};
$cache{sched_arrival} = $action->{sched_time};
$cache{real_arrival} = $action->{real_time};
$cache{edited} |= $action->{edited} << 8;
if ( $action->{route} ) {
$cache{route} = $action->{route};
}
if ( $action->{messages} ) {
$cache{messages} = $action->{messages};
}
$db->insert(
'journeys',
{
user_id => $uid,
train_type => $cache{train_type},
train_line => $cache{train_line},
train_no => $cache{train_no},
train_id => $cache{train_id},
checkin_station_id => $cache{checkin_station_id},
checkin_time => $cache{checkin_time},
sched_departure => $cache{sched_departure},
real_departure => $cache{real_departure},
checkout_station_id => $cache{checkout_station_id},
checkout_time => $cache{checkout_time},
sched_arrival => $cache{sched_arrival},
real_arrival => $cache{real_arrival},
cancelled => $cache{cancelled},
edited => $cache{edited},
route => $cache{route},
messages => $cache{messages}
}
);
%cache = ();
}
$prev_action_type = $action_type;
}
if (%cache) {
# user is currently in transit
$db->insert(
'in_transit',
{
user_id => $uid,
train_type => $cache{train_type},
train_line => $cache{train_line},
train_no => $cache{train_no},
train_id => $cache{train_id},
checkin_station_id => $cache{checkin_station_id},
checkin_time => $cache{checkin_time},
sched_departure => $cache{sched_departure},
real_departure => $cache{real_departure},
cancelled => $cache{cancelled},
route => $cache{route},
messages => $cache{messages}
}
);
}
$count++;
printf( " journey storage migration: %3.0f%% complete\n",
$count * 100 / @uids );
}
$db->update( 'schema_version', { version => 4 } );
},
); );
sub setup_db { sub setup_db {

View file

@ -287,44 +287,17 @@ sub account {
sub json_export { sub json_export {
my ($self) = @_; my ($self) = @_;
my $uid = $self->current_user->{id}; my $uid = $self->current_user->{id};
my $query = $self->app->get_all_actions_query;
$query->execute($uid); my $db = $self->pg->db;
my @entries;
while ( my @row = $query->fetchrow_array ) {
my (
$action_id, $action, $raw_ts, $ds100,
$name, $train_type, $train_line, $train_no,
$train_id, $raw_sched_ts, $raw_real_ts, $raw_route,
$raw_messages
) = @row;
push(
@entries,
{
action => $self->app->action_types->[ $action - 1 ],
action_ts => $raw_ts,
station_ds100 => $ds100,
station_name => $name,
train_type => $train_type,
train_line => $train_line,
train_no => $train_no,
train_id => $train_id,
scheduled_ts => $raw_sched_ts,
realtime_ts => $raw_real_ts,
messages => $raw_messages
? [ map { [ split(qr{:}) ] } split( qr{[|]}, $raw_messages ) ]
: undef,
route => $raw_route ? [ split( qr{[|]}, $raw_route ) ]
: undef,
}
);
}
$self->render( $self->render(
json => [@entries], json => {
account => $db->select( 'users', '*', { id => $uid } )->hash,
journeys => [
$db->select( 'journeys', '*', { user_id => $uid } )
->hashes->each
],
}
); );
} }

View file

@ -48,10 +48,10 @@ sub get_v0 {
my $station_lon = undef; my $station_lon = undef;
my $station_lat = undef; my $station_lat = undef;
if ( $status->{station_ds100} ) { if ( $status->{arr_ds100} // $status->{dep_ds100} ) {
@station_descriptions @station_descriptions
= Travel::Status::DE::IRIS::Stations::get_station( = Travel::Status::DE::IRIS::Stations::get_station(
$status->{station_ds100} ); $status->{arr_ds100} // $status->{dep_ds100} );
} }
if ( @station_descriptions == 1 ) { if ( @station_descriptions == 1 ) {
( undef, undef, $station_eva, $station_lon, $station_lat ) ( undef, undef, $station_eva, $station_lon, $station_lat )
@ -59,14 +59,14 @@ sub get_v0 {
} }
$self->render( $self->render(
json => { json => {
deprecated => \0, deprecated => \1,
checked_in => ( checked_in => (
$status->{checked_in} $status->{checked_in}
or $status->{cancelled} or $status->{cancelled}
) ? \1 : \0, ) ? \1 : \0,
station => { station => {
ds100 => $status->{station_ds100}, ds100 => $status->{arr_ds100} // $status->{dep_ds100},
name => $status->{station_name}, name => $status->{arr_ds100} // $status->{dep_ds100},
uic => $station_eva, uic => $station_eva,
longitude => $station_lon, longitude => $station_lon,
latitude => $station_lat, latitude => $station_lat,
@ -77,8 +77,10 @@ sub get_v0 {
no => $status->{train_no}, no => $status->{train_no},
}, },
actionTime => $status->{timestamp}->epoch, actionTime => $status->{timestamp}->epoch,
scheduledTime => $status->{sched_ts}->epoch, scheduledTime => $status->{sched_arrival}->epoch
realTime => $status->{real_ts}->epoch, || $status->{sched_departure}->epoch,
realTime => $status->{real_arrival}->epoch
|| $status->{real_departure}->epoch,
}, },
); );
} }

View file

@ -6,7 +6,8 @@ my $travelynx_version = qx{git describe --dirty} || 'experimental';
sub about { sub about {
my ($self) = @_; my ($self) = @_;
$self->render( 'about', version => $self->app->config->{version} // 'UNKNOWN' ); $self->render( 'about',
version => $self->app->config->{version} // 'UNKNOWN' );
} }
sub imprint { sub imprint {

View file

@ -98,12 +98,15 @@ sub log_action {
$self->render( $self->render(
json => { json => {
success => 1, success => 1,
redirect_to => '/',
}, },
); );
} }
} }
elsif ( $params->{action} eq 'checkout' ) { elsif ( $params->{action} eq 'checkout' ) {
my $error = $self->checkout( $params->{station}, $params->{force} ); my ( $still_checked_in, $error )
= $self->checkout( $params->{station}, $params->{force} );
my $station_link = '/s/' . $params->{station};
if ($error) { if ($error) {
$self->render( $self->render(
@ -117,6 +120,7 @@ sub log_action {
$self->render( $self->render(
json => { json => {
success => 1, success => 1,
redirect_to => $still_checked_in ? '/' : $station_link,
}, },
); );
} }
@ -135,6 +139,7 @@ sub log_action {
$self->render( $self->render(
json => { json => {
success => 1, success => 1,
redirect_to => '/',
}, },
); );
} }
@ -156,12 +161,14 @@ sub log_action {
$self->render( $self->render(
json => { json => {
success => 1, success => 1,
redirect_to => '/',
}, },
); );
} }
} }
elsif ( $params->{action} eq 'cancelled_to' ) { elsif ( $params->{action} eq 'cancelled_to' ) {
my $error = $self->checkout( $params->{station}, 1, my ( undef, $error )
= $self->checkout( $params->{station}, 1,
$self->app->action_type->{cancelled_to} ); $self->app->action_type->{cancelled_to} );
if ($error) { if ($error) {
@ -176,13 +183,13 @@ sub log_action {
$self->render( $self->render(
json => { json => {
success => 1, success => 1,
redirect_to => '/',
}, },
); );
} }
} }
elsif ( $params->{action} eq 'delete' ) { elsif ( $params->{action} eq 'delete' ) {
my ( $from, $to ) = split( qr{,}, $params->{ids} ); my $error = $self->delete_journey( $params->{id}, $params->{checkin},
my $error = $self->delete_journey( $from, $to, $params->{checkin},
$params->{checkout} ); $params->{checkout} );
if ($error) { if ($error) {
$self->render( $self->render(
@ -196,6 +203,7 @@ sub log_action {
$self->render( $self->render(
json => { json => {
success => 1, success => 1,
redirect_to => '/history',
}, },
); );
} }
@ -215,7 +223,7 @@ sub station {
my $station = $self->stash('station'); my $station = $self->stash('station');
my $train = $self->param('train'); my $train = $self->param('train');
my $status = $self->get_departures($station); my $status = $self->get_departures( $station, 120, 30 );
if ( $status->{errstr} ) { if ( $status->{errstr} ) {
$self->render( $self->render(
@ -382,11 +390,13 @@ sub monthly_history {
sub journey_details { sub journey_details {
my ($self) = @_; my ($self) = @_;
my ( $uid, $checkout_id ) = split( qr{-}, $self->stash('id') ); my $journey_id = $self->stash('id');
$self->param( journey_id => $checkout_id ); my $uid = $self->current_user->{id};
if ( not( $uid == $self->current_user->{id} and $checkout_id ) ) { $self->param( journey_id => $journey_id );
if ( not($journey_id) ) {
$self->render( $self->render(
'journey', 'journey',
error => 'notfound', error => 'notfound',
@ -395,36 +405,35 @@ sub journey_details {
return; return;
} }
my @journeys = $self->get_user_travels( my $journey = $self->get_journey(
uid => $uid, uid => $uid,
checkout_id => $checkout_id, journey_id => $journey_id,
verbose => 1, verbose => 1,
); );
if ( @journeys == 0
or not $journeys[0]{completed}
or $journeys[0]{ids}[1] != $checkout_id )
{
$self->render(
'journey',
error => 'notfound',
journey => {}
);
return;
}
if ($journey) {
$self->render( $self->render(
'journey', 'journey',
error => undef, error => undef,
journey => $journeys[0] journey => $journey,
); );
} }
else {
$self->render(
'journey',
error => 'notfound',
journey => {}
);
}
}
sub edit_journey { sub edit_journey {
my ($self) = @_; my ($self) = @_;
my $checkout_id = $self->param('journey_id'); my $journey_id = $self->param('journey_id');
my $uid = $self->current_user->{id}; my $uid = $self->current_user->{id};
if ( not( $uid == $self->current_user->{id} and $checkout_id ) ) { if ( not( $journey_id =~ m{ ^ \d+ $ }x ) ) {
$self->render( $self->render(
'edit_journey', 'edit_journey',
error => 'notfound', error => 'notfound',
@ -435,7 +444,7 @@ sub edit_journey {
my $journey = $self->get_journey( my $journey = $self->get_journey(
uid => $uid, uid => $uid,
checkout_id => $checkout_id journey_id => $journey_id
); );
if ( not $journey ) { if ( not $journey ) {
@ -449,11 +458,6 @@ sub edit_journey {
my $error = undef; my $error = undef;
if ( $self->param('action') and $self->param('action') eq 'cancel' ) {
$self->redirect_to("/journey/${uid}-${checkout_id}");
return;
}
if ( $self->param('action') and $self->param('action') eq 'save' ) { if ( $self->param('action') and $self->param('action') eq 'save' ) {
my $parser = DateTime::Format::Strptime->new( my $parser = DateTime::Format::Strptime->new(
pattern => '%d.%m.%Y %H:%M', pattern => '%d.%m.%Y %H:%M',
@ -468,12 +472,8 @@ sub edit_journey {
{ {
my $datetime = $parser->parse_datetime( $self->param($key) ); my $datetime = $parser->parse_datetime( $self->param($key) );
if ( $datetime and $datetime->epoch ne $journey->{$key}->epoch ) { if ( $datetime and $datetime->epoch ne $journey->{$key}->epoch ) {
$error = $self->update_journey_part( $error = $self->update_journey_part( $db, $journey->{id},
$db, $key, $datetime );
$journey->{ids}[0],
$journey->{ids}[1],
$key, $datetime
);
if ($error) { if ($error) {
last; last;
} }
@ -484,15 +484,14 @@ sub edit_journey {
$journey = $self->get_journey( $journey = $self->get_journey(
uid => $uid, uid => $uid,
db => $db, db => $db,
checkout_id => $checkout_id, journey_id => $journey_id,
verbose => 1 verbose => 1
); );
$error = $self->journey_sanity_check($journey); $error = $self->journey_sanity_check($journey);
} }
if ( not $error ) { if ( not $error ) {
$tx->commit; $tx->commit;
$self->redirect_to("/journey/${uid}-${checkout_id}"); $self->redirect_to("/journey/${journey_id}");
$self->invalidate_stats_cache( $journey->{checkout} );
return; return;
} }
} }

View file

@ -1,11 +1,11 @@
function tvly_run(link, req, redir, err_callback) { function tvly_run(link, req, err_callback) {
var error_icon = '<i class="material-icons">error</i>'; var error_icon = '<i class="material-icons">error</i>';
var progressbar = $('<div class="progress"><div class="indeterminate"></div></div>'); var progressbar = $('<div class="progress"><div class="indeterminate"></div></div>');
link.hide(); link.hide();
link.after(progressbar); link.after(progressbar);
$.post('/action', req, function(data) { $.post('/action', req, function(data) {
if (data.success) { if (data.success) {
$(location).attr('href', redir); $(location).attr('href', data.redirect_to);
} else { } else {
M.toast({html: error_icon + ' ' + data.error}); M.toast({html: error_icon + ' ' + data.error});
progressbar.remove(); progressbar.remove();
@ -25,7 +25,7 @@ $(document).ready(function() {
station: link.data('station'), station: link.data('station'),
train: link.data('train'), train: link.data('train'),
}; };
tvly_run(link, req, '/'); tvly_run(link, req);
}); });
$('.action-checkout').click(function() { $('.action-checkout').click(function() {
var link = $(this); var link = $(this);
@ -34,7 +34,7 @@ $(document).ready(function() {
station: link.data('station'), station: link.data('station'),
force: link.data('force'), force: link.data('force'),
}; };
tvly_run(link, req, '/s/' + req.station, function() { tvly_run(link, req, function() {
link.append(' Ohne Echtzeitdaten auschecken?') link.append(' Ohne Echtzeitdaten auschecken?')
link.data('force', true); link.data('force', true);
}); });
@ -45,7 +45,7 @@ $(document).ready(function() {
action: 'undo', action: 'undo',
undo_id: link.data('id'), undo_id: link.data('id'),
}; };
tvly_run(link, req, '/'); tvly_run(link, req);
}); });
$('.action-cancelled-from').click(function() { $('.action-cancelled-from').click(function() {
var link = $(this); var link = $(this);
@ -54,7 +54,7 @@ $(document).ready(function() {
station: link.data('station'), station: link.data('station'),
train: link.data('train'), train: link.data('train'),
}; };
tvly_run(link, req, '/'); tvly_run(link, req);
}); });
$('.action-cancelled-to').click(function() { $('.action-cancelled-to').click(function() {
var link = $(this); var link = $(this);
@ -63,19 +63,19 @@ $(document).ready(function() {
station: link.data('station'), station: link.data('station'),
force: true, force: true,
}; };
tvly_run(link, req, '/'); tvly_run(link, req);
}); });
$('.action-delete').click(function() { $('.action-delete').click(function() {
var link = $(this); var link = $(this);
var req = { var req = {
action: 'delete', action: 'delete',
ids: link.data('id'), id: link.data('id'),
checkin: link.data('checkin'), checkin: link.data('checkin'),
checkout: link.data('checkout'), checkout: link.data('checkout'),
}; };
really_delete = confirm("Diese Zugfahrt wirklich löschen? Der Eintrag wird sofort aus der Datenbank entfernt und kann nicht wiederhergestellt werden."); really_delete = confirm("Diese Zugfahrt wirklich löschen? Der Eintrag wird sofort aus der Datenbank entfernt und kann nicht wiederhergestellt werden.");
if (really_delete) { if (really_delete) {
tvly_run(link, req, '/history'); tvly_run(link, req);
} }
}); });
}); });

View file

@ -1 +1 @@
function tvly_run(n,t,a,c){var i='<i class="material-icons">error</i>',e=$('<div class="progress"><div class="indeterminate"></div></div>');n.hide(),n.after(e),$.post("/action",t,function(t){t.success?$(location).attr("href",a):(M.toast({html:i+" "+t.error}),e.remove(),c&&c(),n.append(" "+i),n.show())})}$(document).ready(function(){$(".action-checkin").click(function(){var t=$(this);tvly_run(t,{action:"checkin",station:t.data("station"),train:t.data("train")},"/")}),$(".action-checkout").click(function(){var t=$(this),n={action:"checkout",station:t.data("station"),force:t.data("force")};tvly_run(t,n,"/s/"+n.station,function(){t.append(" Ohne Echtzeitdaten auschecken?"),t.data("force",!0)})}),$(".action-undo").click(function(){var t=$(this);tvly_run(t,{action:"undo",undo_id:t.data("id")},"/")}),$(".action-cancelled-from").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_from",station:t.data("station"),train:t.data("train")},"/")}),$(".action-cancelled-to").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_to",station:t.data("station"),force:!0},"/")}),$(".action-delete").click(function(){var t=$(this),n={action:"delete",ids:t.data("id"),checkin:t.data("checkin"),checkout:t.data("checkout")};really_delete=confirm("Diese Zugfahrt wirklich löschen? Der Eintrag wird sofort aus der Datenbank entfernt und kann nicht wiederhergestellt werden."),really_delete&&tvly_run(t,n,"/history")})}); function tvly_run(t,n,a){var c='<i class="material-icons">error</i>',i=$('<div class="progress"><div class="indeterminate"></div></div>');t.hide(),t.after(i),$.post("/action",n,function(n){n.success?$(location).attr("href",n.redirect_to):(M.toast({html:c+" "+n.error}),i.remove(),a&&a(),t.append(" "+c),t.show())})}$(document).ready(function(){$(".action-checkin").click(function(){var t=$(this);tvly_run(t,{action:"checkin",station:t.data("station"),train:t.data("train")})}),$(".action-checkout").click(function(){var t=$(this),n={action:"checkout",station:t.data("station"),force:t.data("force")};tvly_run(t,n,function(){t.append(" Ohne Echtzeitdaten auschecken?"),t.data("force",!0)})}),$(".action-undo").click(function(){var t=$(this);tvly_run(t,{action:"undo",undo_id:t.data("id")})}),$(".action-cancelled-from").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_from",station:t.data("station"),train:t.data("train")})}),$(".action-cancelled-to").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_to",station:t.data("station"),force:!0})}),$(".action-delete").click(function(){var t=$(this),n={action:"delete",id:t.data("id"),checkin:t.data("checkin"),checkout:t.data("checkout")};really_delete=confirm("Diese Zugfahrt wirklich löschen? Der Eintrag wird sofort aus der Datenbank entfernt und kann nicht wiederhergestellt werden."),really_delete&&tvly_run(t,n)})});

View file

@ -11,8 +11,7 @@
</thead> </thead>
<tbody> <tbody>
% for my $travel (@{$journeys}) { % for my $travel (@{$journeys}) {
% if ($travel->{completed}) { % my $detail_link = '/journey/' . $travel->{id};
% my $detail_link = '/journey/' . current_user()->{id} . '-' . $travel->{ids}->[1];
<tr> <tr>
<td><%= $travel->{sched_departure}->strftime('%d.%m.') %></td> <td><%= $travel->{sched_departure}->strftime('%d.%m.') %></td>
<td><a href="<%= $detail_link %>"><%= $travel->{type} %> <%= $travel->{line} // $travel->{no} %></a></td> <td><a href="<%= $detail_link %>"><%= $travel->{type} %> <%= $travel->{line} // $travel->{no} %></a></td>
@ -51,7 +50,6 @@
</a></td> </a></td>
</tr> </tr>
% } % }
% }
</tbody> </tbody>
</table> </table>
</div> </div>

View file

@ -6,10 +6,10 @@
<div class="card-content white-text"> <div class="card-content white-text">
<span class="card-title">Aktuell eingecheckt</span> <span class="card-title">Aktuell eingecheckt</span>
<p>In <%= $status->{train_type} %> <%= $status->{train_no} %> <p>In <%= $status->{train_type} %> <%= $status->{train_no} %>
ab <%= $status->{station_name} %></p> ab <%= $status->{dep_name} %></p>
</div> </div>
<div class="card-action"> <div class="card-action">
<a class="action-checkout" data-station="<%= $ds100 %>"> <a class="action-checkout" data-station="<%= $ds100 %>" data-force="1">
Hier auschecken Hier auschecken
</a> </a>
</div> </div>
@ -20,10 +20,10 @@
<div class="card-content white-text"> <div class="card-content white-text">
<span class="card-title">Ausgecheckt</span> <span class="card-title">Ausgecheckt</span>
<p>Aus <%= $status->{train_type} %> <%= $status->{train_no} %> <p>Aus <%= $status->{train_type} %> <%= $status->{train_no} %>
bis <%= $status->{station_name} %></p> bis <%= $status->{arr_name} %></p>
</div> </div>
<div class="card-action"> <div class="card-action">
<a class="action-undo" data-id="<%= $status->{action_id} %>"> <a class="action-undo" data-id="<%= $status->{journey_id} %>">
<i class="material-icons">undo</i> Rückgängig? <i class="material-icons">undo</i> Rückgängig?
</a> </a>
</div> </div>
@ -38,8 +38,8 @@
Zug auswählen zum Einchecken. Zug auswählen zum Einchecken.
% } % }
% else { % else {
Keine Abfahrten gefunden. Ein Checkin ist frühestens 10 Minuten vor Keine Abfahrten gefunden. Ein Checkin ist frühestens 30 Minuten vor
und maximal 180 Minuten nach Abfahrt möglich. und maximal 120 Minuten nach Abfahrt möglich.
% } % }
<br/> <br/>
<table class="striped"> <table class="striped">

View file

@ -80,7 +80,7 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col s6 m6 l6 center-align"> <div class="col s6 m6 l6 center-align">
<a href="/journey/<%= current_user()->{id} %>-<%= param('journey_id') %>" class="waves-effect waves-light btn"> <a href="/journey/<%= param('journey_id') %>" class="waves-effect waves-light btn">
Abbrechen Abbrechen
</a> </a>
</div> </div>

View file

@ -161,7 +161,7 @@
<div class="row"> <div class="row">
<div class="col s6 m6 l6 center-align"> <div class="col s6 m6 l6 center-align">
<a class="waves-effect waves-light red btn action-delete" <a class="waves-effect waves-light red btn action-delete"
data-id="<%= join(q{,}, @{$journey->{ids}}) %>" data-id="<%= $journey->{id} %>"
data-checkin="<%= $journey->{checkin}->epoch %>" data-checkin="<%= $journey->{checkin}->epoch %>"
data-checkout="<%= $journey->{checkout}->epoch %>"> data-checkout="<%= $journey->{checkout}->epoch %>">
<i class="material-icons left">delete_forever</i> <i class="material-icons left">delete_forever</i>

View file

@ -14,26 +14,84 @@
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
% my $status = get_user_status(); % my $status = get_user_status();
% my $now = DateTime->now(time_zone => 'Europe/Berlin');
% my $dep_wait = ($status->{real_departure}->epoch - $now->epoch)/60;
% my $arr_wait = undef;
% if ($status->{real_arrival}->epoch) {
% $arr_wait = ($status->{real_arrival}->epoch - $now->epoch)/60;
% }
% if ($status->{checked_in}) { % if ($status->{checked_in}) {
<div class="card green darken-4"> <div class="card green darken-4">
<div class="card-content white-text"> <div class="card-content white-text">
<span class="card-title">Hallo, <%= current_user()->{name} %>!</span> <span class="card-title">Eingecheckt</span>
<p>Du bist gerade eingecheckt in <p>
<%= $status->{train_type} %> <%= $status->{train_no} %> In <b><%= $status->{train_type} %> <%= $status->{train_no} %></b>
ab <%= $status->{station_name} %>. % if ($status->{arr_name}) {
% if ($status->{timestamp_delta} < 180) { von <b><%= $status->{dep_name} %></b> nach <b><%= $status->{arr_name} %></b>.
<a class="action-undo" data-id="<%= $status->{action_id} %>"><i class="material-icons">undo</i> Rückgängig</a> % }
% else {
ab <b><%= $status->{dep_name} %></b>.
% } % }
</p> </p>
<p>Bei Ankunft: Station auswählen zum Auschecken.</p> <p>
Abfahrt
% if ($dep_wait > 0) {
in <%= int(($status->{real_departure}->epoch - $now->epoch)/60) %> Minute<%= $dep_wait >= 2 ? 'n' : '' %>
% }
um <b><%= $status->{real_departure}->strftime('%H:%M') %></b>
% if ($status->{real_departure}->epoch != $status->{sched_departure}->epoch) {
(+<%= int(($status->{real_departure}->epoch - $status->{sched_departure}->epoch)/60) %>)
% }
</p>
<p>
% if ($status->{real_arrival}->epoch) {
Voraussichtliche Ankunft um <b><%= $status->{real_arrival}->strftime('%H:%M') %></b>
% if ($status->{real_arrival}->epoch != $status->{sched_arrival}->epoch) {
(+<%= int(($status->{real_arrival}->epoch - $status->{sched_arrival}->epoch)/60) %>)
% }
% }
% else {
Ankunft: noch nicht bekannt
% }
</p>
<p>
<b>Achtung:</b> Automatischer Checkout ist noch nicht
implementiert. Bitte spätestens eine Stunde nach Ankunft
am Ziel manuell auschecken.
</p>
% if ($status->{arr_name}) {
<p>Zielstation ändern?</p>
% }
% else {
<p>Zielstation wählen:</p>
% }
<table> <table>
<tbody> <tbody>
% my $is_after = 0; % my $is_after = 0;
% for my $station (@{$status->{route_after}}) { % for my $station (@{$status->{route_after}}) {
% if ($status->{arr_name} and $station eq $status->{arr_name}) {
<tr><td><b><a class="action-checkout" data-station="<%= $station %>"><%= $station %></a></b></td></tr>
% }
% else {
<tr><td><a class="action-checkout" data-station="<%= $station %>"><%= $station %></a></td></tr> <tr><td><a class="action-checkout" data-station="<%= $station %>"><%= $station %></a></td></tr>
% } % }
% }
</tbody> </tbody>
</table> </table>
% if ($status->{arr_name}) {
<p>
Falls das Backend ausgefallen ist oder der Zug aus anderen
Gründen verloren ging: <a class="action-checkout"
data-force="1" data-station="<%= $status->{arr_name}
%>">Ohne Echtzeitdaten in <%= $status->{arr_name} %>
auschecken</a>.
</p>
% }
</div>
<div class="card-action">
<a class="action-undo" data-id="in_transit">
<i class="material-icons">undo</i> Checkin Rückgängig?
</a>
</div> </div>
</div> </div>
% } % }
@ -44,9 +102,6 @@
<p>Prinzipiell wärest du nun eingecheckt in <p>Prinzipiell wärest du nun eingecheckt in
<%= $status->{train_type} %> <%= $status->{train_no} %> <%= $status->{train_type} %> <%= $status->{train_no} %>
ab <%= $status->{station_name} %>, doch dieser Zug fällt aus. ab <%= $status->{station_name} %>, doch dieser Zug fällt aus.
% if ($status->{timestamp_delta} < 180) {
<a class="action-undo" data-id="<%= $status->{action_id} %>"><i class="material-icons">undo</i> Checkinversuch rückgängig</a>
% }
</p> </p>
<p>Falls du den Zugausfall z.B. für ein Fahrgastrechteformular <p>Falls du den Zugausfall z.B. für ein Fahrgastrechteformular
dokumentieren möchtest, wähle bitte jetzt deine geplante dokumentieren möchtest, wähle bitte jetzt deine geplante
@ -62,6 +117,11 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<div class="card-action">
<a class="action-undo" data-id="in_transit">
<i class="material-icons">undo</i> Checkinversuch Rückgängig?
</a>
</div>
</div> </div>
% } % }
% else { % else {
@ -91,7 +151,7 @@
</div> </div>
</div> </div>
<h1>Letzte Fahrten</h1> <h1>Letzte Fahrten</h1>
%= include '_history_trains', journeys => [get_user_travels(limit => 1)]; %= include '_history_trains', journeys => [get_user_travels(limit => 5)];
% } % }
% else { % else {
<div class="row"> <div class="row">

View file

@ -5,7 +5,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#673ab7"> <meta name="theme-color" content="#673ab7">
% my $av = 'v5'; # asset version % my $av = 'v6'; # asset version
%= stylesheet "/static/${av}/css/materialize.min.css" %= stylesheet "/static/${av}/css/materialize.min.css"
%= stylesheet "/static/${av}/css/material-icons.css" %= stylesheet "/static/${av}/css/material-icons.css"
%= stylesheet "/static/${av}/css/local.css" %= stylesheet "/static/${av}/css/local.css"