2019-04-23 19:30:31 +00:00
|
|
|
package Travelynx::Command::work;
|
|
|
|
use Mojo::Base 'Mojolicious::Command';
|
|
|
|
|
|
|
|
use DateTime;
|
2019-05-26 15:28:21 +00:00
|
|
|
use JSON;
|
2019-04-23 19:30:31 +00:00
|
|
|
use List::Util qw(first);
|
|
|
|
|
|
|
|
has description =>
|
|
|
|
'Perform automatic checkout when users arrive at their destination';
|
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage };
|
|
|
|
|
|
|
|
sub run {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-05-26 15:28:21 +00:00
|
|
|
my $json = JSON->new;
|
2019-04-23 19:30:31 +00:00
|
|
|
|
|
|
|
my $db = $self->app->pg->db;
|
|
|
|
|
|
|
|
for my $entry (
|
|
|
|
$db->select( 'in_transit_str', '*', { cancelled => 0 } )->hashes->each )
|
|
|
|
{
|
|
|
|
|
2019-05-02 08:05:49 +00:00
|
|
|
my $uid = $entry->{user_id};
|
|
|
|
my $dep = $entry->{dep_ds100};
|
|
|
|
my $arr = $entry->{arr_ds100};
|
|
|
|
my $train_id = $entry->{train_id};
|
2019-04-23 19:30:31 +00:00
|
|
|
|
|
|
|
$self->app->log->debug("Processing $uid");
|
|
|
|
|
|
|
|
eval {
|
|
|
|
if ( $now->epoch - $entry->{real_dep_ts} < 300 ) {
|
|
|
|
$self->app->log->debug(" - updating departure");
|
|
|
|
my $status = $self->app->get_departures( $dep, 30, 30 );
|
|
|
|
if ( $status->{errstr} ) {
|
|
|
|
die("get_departures($dep): $status->{errstr}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($train)
|
2019-05-02 08:05:49 +00:00
|
|
|
= first { $_->train_id eq $train_id } @{ $status->{results} };
|
2019-04-23 19:30:31 +00:00
|
|
|
|
|
|
|
if ( not $train ) {
|
2019-05-02 08:05:49 +00:00
|
|
|
die("could not find train $train_id at $dep\n");
|
2019-04-23 19:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$db->update(
|
|
|
|
'in_transit',
|
2019-04-26 20:12:34 +00:00
|
|
|
{
|
2019-05-18 15:10:53 +00:00
|
|
|
dep_platform => $train->platform,
|
2019-04-26 20:12:34 +00:00
|
|
|
real_departure => $train->departure,
|
2019-05-26 15:28:21 +00:00
|
|
|
route =>
|
|
|
|
$json->encode( [ map { [$_] } $train->route ] ),
|
|
|
|
messages => $json->encode(
|
|
|
|
[
|
|
|
|
map { [ $_->[0]->epoch, $_->[1] ] }
|
|
|
|
$train->messages
|
|
|
|
]
|
|
|
|
),
|
2019-04-26 20:12:34 +00:00
|
|
|
},
|
|
|
|
{ user_id => $uid }
|
2019-04-23 19:30:31 +00:00
|
|
|
);
|
2019-05-31 18:18:22 +00:00
|
|
|
$self->app->add_route_timestamps( $uid, $train );
|
2019-04-23 19:30:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->app->log->error("work($uid)/departure: $@");
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
if (
|
|
|
|
$entry->{arr_name}
|
|
|
|
and ( not $entry->{real_arr_ts}
|
|
|
|
or $now->epoch - $entry->{real_arr_ts} < 60 )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$self->app->log->debug(" - updating arrival");
|
|
|
|
my $status = $self->app->get_departures( $arr, 20, 220 );
|
|
|
|
if ( $status->{errstr} ) {
|
|
|
|
die("get_departures($arr): $status->{errstr}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($train)
|
2019-05-02 08:05:49 +00:00
|
|
|
= first { $_->train_id eq $train_id } @{ $status->{results} };
|
2019-04-23 19:30:31 +00:00
|
|
|
|
|
|
|
if ( not $train ) {
|
2019-05-02 08:05:49 +00:00
|
|
|
if ( $entry->{real_arr_ts} ) {
|
|
|
|
die(
|
|
|
|
"could not find train $train_id at $arr -- did it disappear?\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# If we haven't seen the train yet, its arrival is probably
|
|
|
|
# too far in the future. This is not critical.
|
|
|
|
return;
|
|
|
|
}
|
2019-04-23 19:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$db->update(
|
|
|
|
'in_transit',
|
|
|
|
{
|
2019-05-18 15:10:53 +00:00
|
|
|
arr_platform => $train->platform,
|
2019-04-23 19:30:31 +00:00
|
|
|
sched_arrival => $train->sched_arrival,
|
|
|
|
real_arrival => $train->arrival,
|
2019-05-26 15:28:21 +00:00
|
|
|
route =>
|
|
|
|
$json->encode( [ map { [$_] } $train->route ] ),
|
|
|
|
messages => $json->encode(
|
|
|
|
[
|
|
|
|
map { [ $_->[0]->epoch, $_->[1] ] }
|
|
|
|
$train->messages
|
|
|
|
]
|
|
|
|
),
|
2019-04-23 19:30:31 +00:00
|
|
|
},
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
2019-05-31 18:18:22 +00:00
|
|
|
$self->app->add_route_timestamps( $uid, $train );
|
2019-04-23 19:30:31 +00:00
|
|
|
}
|
|
|
|
elsif ( $entry->{real_arr_ts} ) {
|
|
|
|
$self->app->log->debug(" - checking out");
|
|
|
|
my ( undef, $error ) = $self->app->checkout( $arr, 1, $uid );
|
|
|
|
if ($error) {
|
|
|
|
die("${error}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->app->log->error("work($uid)/arrival: $@");
|
|
|
|
}
|
|
|
|
|
|
|
|
eval { }
|
|
|
|
}
|
2019-05-18 15:11:28 +00:00
|
|
|
|
|
|
|
# Computing yearly stats may take a while, but we've got all time in the
|
|
|
|
# world here. This means users won't have to wait when loading their
|
|
|
|
# own by-year journey log.
|
|
|
|
for my $user ( $db->select( 'users', 'id', { status => 1 } )->hashes->each )
|
|
|
|
{
|
|
|
|
$self->app->get_journey_stats(
|
|
|
|
uid => $user->{id},
|
|
|
|
year => $now->year
|
|
|
|
);
|
|
|
|
}
|
2019-04-23 19:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
Usage: index.pl work
|
|
|
|
|
|
|
|
Work Work Work.
|
|
|
|
|
|
|
|
Should be called from a cronjob every three minutes or so.
|