diff options
Diffstat (limited to 'lib/Travelynx')
-rw-r--r-- | lib/Travelynx/Command/work.pm | 28 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Profile.pm | 67 | ||||
-rw-r--r-- | lib/Travelynx/Helper/DBDB.pm | 29 | ||||
-rw-r--r-- | lib/Travelynx/Helper/HAFAS.pm | 22 | ||||
-rw-r--r-- | lib/Travelynx/Model/InTransit.pm | 105 |
5 files changed, 167 insertions, 84 deletions
diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 10b1b69..8ae918e 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -60,7 +60,7 @@ sub run { return; } - if ( $found_dep->{rt_dep} ) { + if ( $found_dep->rt_dep ) { $self->app->in_transit->update_departure_hafas( uid => $uid, journey => $journey, @@ -68,9 +68,17 @@ sub run { dep_eva => $dep, arr_eva => $arr ); + if ( $journey->class <= 16 + and $found_dep->rt_dep->epoch > $now->epoch ) + { + $self->app->add_wagonorder( $uid, 1, $train_id, + $found_dep->sched_dep, $journey->number ); + $self->app->add_stationinfo( $uid, 1, $journey->id, + $found_dep->loc->eva ); + } } - if ( $found_arr and $found_arr->{rt_arr} ) { + if ( $found_arr and $found_arr->rt_arr ) { $self->app->in_transit->update_arrival_hafas( uid => $uid, journey => $journey, @@ -78,6 +86,14 @@ sub run { dep_eva => $dep, arr_eva => $arr ); + if ( $journey->class <= 16 + and $found_arr->rt_arr->epoch - $now->epoch < 600 ) + { + $self->app->add_wagonorder( $uid, 0, $train_id, + $found_dep->sched_dep, $journey->number ); + $self->app->add_stationinfo( $uid, 0, $journey->id, + $found_dep->loc->eva, $found_arr->loc->eva ); + } } } )->catch( @@ -171,6 +187,10 @@ sub run { } else { $self->app->add_route_timestamps( $uid, $train, 1 ); + $self->app->add_wagonorder( $uid, 1, $train->train_id, + $train->sched_departure, $train->train_no ); + $self->app->add_stationinfo( $uid, 1, $train->train_id, + $dep, $arr ); } } }; @@ -243,6 +263,10 @@ sub run { and $now->epoch > $entry->{real_arr_ts} ) ? 1 : 0 ); + $self->app->add_wagonorder( $uid, 0, $train->train_id, + $train->sched_departure, $train->train_no ); + $self->app->add_stationinfo( $uid, 0, $train->train_id, + $dep, $arr ); } } elsif ( $entry->{real_arr_ts} ) { diff --git a/lib/Travelynx/Controller/Profile.pm b/lib/Travelynx/Controller/Profile.pm index fc2d38c..a5f394f 100755 --- a/lib/Travelynx/Controller/Profile.pm +++ b/lib/Travelynx/Controller/Profile.pm @@ -152,34 +152,45 @@ sub profile { @journeys = $self->journeys->get(%opt); } - $self->render( - 'profile', - title => "travelynx: $name", - name => $name, - uid => $user->{id}, - privacy => $user, - bio => $profile->{bio}{html}, - metadata => $profile->{metadata}, - is_self => $is_self, - following => ( $relation and $relation eq 'follows' ) ? 1 : 0, - follow_requested => ( $relation and $relation eq 'requests_follow' ) - ? 1 - : 0, - can_follow => ( $my_user and $user->{accept_follows} and not $relation ) - ? 1 - : 0, - can_request_follow => - ( $my_user and $user->{accept_follow_requests} and not $relation ) - ? 1 - : 0, - follows_me => ( $inverse_relation and $inverse_relation eq 'follows' ) - ? 1 - : 0, - follow_reqs_me => - ( $inverse_relation and $inverse_relation eq 'requests_follow' ) ? 1 - : 0, - journey => $status, - journeys => [@journeys], + $self->respond_to( + json => { + json => { + name => $name, + uid => $user->{id}, + bio => $profile->{bio}{html}, + metadata => $profile->{metadata}, + } + }, + any => { + template => 'profile', + title => "travelynx: $name", + name => $name, + uid => $user->{id}, + privacy => $user, + bio => $profile->{bio}{html}, + metadata => $profile->{metadata}, + is_self => $is_self, + following => ( $relation and $relation eq 'follows' ) ? 1 : 0, + follow_requested => ( $relation and $relation eq 'requests_follow' ) + ? 1 + : 0, + can_follow => + ( $my_user and $user->{accept_follows} and not $relation ) ? 1 + : 0, + can_request_follow => ( + $my_user and $user->{accept_follow_requests} and not $relation + ) ? 1 + : 0, + follows_me => + ( $inverse_relation and $inverse_relation eq 'follows' ) ? 1 + : 0, + follow_reqs_me => ( + $inverse_relation and $inverse_relation eq 'requests_follow' + ) ? 1 + : 0, + journey => $status, + journeys => [@journeys], + } ); } diff --git a/lib/Travelynx/Helper/DBDB.pm b/lib/Travelynx/Helper/DBDB.pm index b98a372..da3bfb1 100644 --- a/lib/Travelynx/Helper/DBDB.pm +++ b/lib/Travelynx/Helper/DBDB.pm @@ -31,14 +31,18 @@ sub has_wagonorder_p { my $api_ts = $ts->strftime('%Y%m%d%H%M'); my $url = "https://ist-wr.noncd.db.de/wagenreihung/1.0/${train_no}/${api_ts}"; - my $cache = $self->{cache}; + my $cache = $self->{realtime_cache}; my $promise = Mojo::Promise->new; if ( my $content = $cache->get("HEAD $url") ) { if ( $content eq 'n' ) { + $self->{log} + ->debug("has_wagonorder_p(${train_no}/${api_ts}): n (cached)"); return $promise->reject; } else { + $self->{log} + ->debug("has_wagonorder_p(${train_no}/${api_ts}): y (cached)"); return $promise->resolve($content); } } @@ -48,10 +52,14 @@ sub has_wagonorder_p { sub { my ($tx) = @_; if ( $tx->result->is_success ) { + $self->{log} + ->debug("has_wagonorder_p(${train_no}/${api_ts}): a"); $cache->set( "HEAD $url", 'a' ); $promise->resolve('a'); } else { + $self->{log} + ->debug("has_wagonorder_p(${train_no}/${api_ts}): n"); $cache->set( "HEAD $url", 'n' ); $promise->reject; } @@ -59,6 +67,7 @@ sub has_wagonorder_p { } )->catch( sub { + $self->{log}->debug("has_wagonorder_p(${train_no}/${api_ts}): n"); $cache->set( "HEAD $url", 'n' ); $promise->reject; return; @@ -73,10 +82,12 @@ sub get_wagonorder_p { my $url = "https://ist-wr.noncd.db.de/wagenreihung/1.0/${train_no}/${api_ts}"; - my $cache = $self->{cache}; + my $cache = $self->{realtime_cache}; my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { + $self->{log} + ->debug("get_wagonorder_p(${train_no}/${api_ts}): (cached)"); $promise->resolve($content); return $promise; } @@ -89,11 +100,15 @@ sub get_wagonorder_p { if ( $tx->result->is_success ) { my $body = decode( 'utf-8', $tx->res->body ); my $json = JSON->new->decode($body); + $self->{log} + ->debug("get_wagonorder_p(${train_no}/${api_ts}): success"); $cache->freeze( $url, $json ); $promise->resolve($json); } else { my $code = $tx->code; + $self->{log}->debug( + "get_wagonorder_p(${train_no}/${api_ts}): HTTP ${code}"); $promise->reject("HTTP ${code}"); } return; @@ -101,6 +116,8 @@ sub get_wagonorder_p { )->catch( sub { my ($err) = @_; + $self->{log} + ->debug("get_wagonorder_p(${train_no}/${api_ts}): error ${err}"); $promise->reject($err); return; } @@ -113,10 +130,11 @@ sub get_stationinfo_p { my $url = "https://lib.finalrewind.org/dbdb/s/${eva}.json"; - my $cache = $self->{cache}; + my $cache = $self->{main_cache}; my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { + $self->{log}->debug("get_stationinfo_p(${eva}): (cached)"); return $promise->resolve($content); } @@ -126,12 +144,16 @@ sub get_stationinfo_p { my ($tx) = @_; if ( my $err = $tx->error ) { + $self->{log}->debug( +"get_stationinfo_p(${eva}): HTTP $err->{code} $err->{message}" + ); $cache->freeze( $url, {} ); $promise->reject("HTTP $err->{code} $err->{message}"); return; } my $json = $tx->result->json; + $self->{log}->debug("get_stationinfo_p(${eva}): success"); $cache->freeze( $url, $json ); $promise->resolve($json); return; @@ -139,6 +161,7 @@ sub get_stationinfo_p { )->catch( sub { my ($err) = @_; + $self->{log}->debug("get_stationinfo_p(${eva}): Error ${err}"); $cache->freeze( $url, {} ); $promise->reject($err); return; diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index 7671d78..538c8ea 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -199,7 +199,7 @@ sub get_journey_p { return $promise; } -sub get_route_timestamps_p { +sub get_route_p { my ( $self, %opt ) = @_; my $promise = Mojo::Promise->new; @@ -219,13 +219,12 @@ sub get_route_timestamps_p { sub { my ($hafas) = @_; my $journey = $hafas->result; - my $ret = {}; + my $ret = []; my $polyline; my $station_is_past = 1; for my $stop ( $journey->route ) { - my $name = $stop->loc->name; - $ret->{$name} = $ret->{ $stop->loc->eva } = { + my $entry = { name => $stop->loc->name, eva => $stop->loc->eva, sched_arr => _epoch( $stop->sched_arr ), @@ -237,26 +236,27 @@ sub get_route_timestamps_p { load => $stop->load }; if ( $stop->tz_offset ) { - $ret->{$name}{tz_offset} = $stop->tz_offset; + $entry->{tz_offset} = $stop->tz_offset; } if ( ( $stop->arr_cancelled or not $stop->sched_arr ) and ( $stop->dep_cancelled or not $stop->sched_dep ) ) { - $ret->{$name}{isCancelled} = 1; + $entry->{isCancelled} = 1; } if ( $station_is_past - and not $ret->{$name}{isCancelled} + and not $entry->{isCancelled} and $now->epoch < ( - $ret->{$name}{rt_arr} // $ret->{$name}{rt_dep} - // $ret->{$name}{sched_arr} - // $ret->{$name}{sched_dep} // $now->epoch + $entry->{rt_arr} // $entry->{rt_dep} + // $entry->{sched_arr} // $entry->{sched_dep} + // $now->epoch ) ) { $station_is_past = 0; } - $ret->{$name}{isPast} = $station_is_past; + $entry->{isPast} = $station_is_past; + push( @{$ret}, $entry ); } if ( $journey->polyline ) { diff --git a/lib/Travelynx/Model/InTransit.pm b/lib/Travelynx/Model/InTransit.pm index 69026ac..0d548c3 100644 --- a/lib/Travelynx/Model/InTransit.pm +++ b/lib/Travelynx/Model/InTransit.pm @@ -47,6 +47,16 @@ sub epoch_to_dt { ); } +sub epoch_or_dt_to_dt { + my ($input) = @_; + + if ( ref($input) eq 'DateTime' ) { + return $input; + } + + return epoch_to_dt($input); +} + sub new { my ( $class, %opt ) = @_; @@ -211,8 +221,15 @@ sub postprocess { if ($is_after) { push( @route_after, $station ); } - if ( $ret->{dep_name} - and $station->[0] eq $ret->{dep_name} ) + + # Note that the departure stop may be present more than once in @route, + # e.g. when traveling along ring lines such as S41 / S42 in Berlin. + if ( + $ret->{dep_name} + and $station->[0] eq $ret->{dep_name} + and not($station->[2]{sched_dep} + and $station->[2]{sched_dep} < $ret->{sched_dep_ts} ) + ) { $is_after = 1; if ( @{$station} > 1 and not $dep_info ) { @@ -273,31 +290,25 @@ sub postprocess { # station is present several times in a train's route, e.g. # for Frankfurt Flughafen in some nightly connections. my $times = $station->[2] // {}; - if ( $times->{sched_arr} - and ref( $times->{sched_arr} ) ne 'DateTime' ) - { - $times->{sched_arr} - = epoch_to_dt( $times->{sched_arr} ); - if ( $times->{rt_arr} ) { - $times->{rt_arr} - = epoch_to_dt( $times->{rt_arr} ); - $times->{arr_delay} - = $times->{rt_arr}->epoch - $times->{sched_arr}->epoch; + for my $key (qw(sched_arr rt_arr sched_dep rt_dep)) { + if ( $times->{$key} ) { + $times->{$key} + = epoch_or_dt_to_dt( $times->{$key} ); } + } + if ( $times->{sched_arr} and $times->{rt_arr} ) { + $times->{arr_delay} + = $times->{rt_arr}->epoch - $times->{sched_arr}->epoch; + } + if ( $times->{sched_arr} or $times->{rt_arr} ) { $times->{arr} = $times->{rt_arr} || $times->{sched_arr}; $times->{arr_countdown} = $times->{arr}->epoch - $epoch; } - if ( $times->{sched_dep} - and ref( $times->{sched_dep} ) ne 'DateTime' ) - { - $times->{sched_dep} - = epoch_to_dt( $times->{sched_dep} ); - if ( $times->{rt_dep} ) { - $times->{rt_dep} - = epoch_to_dt( $times->{rt_dep} ); - $times->{dep_delay} - = $times->{rt_dep}->epoch - $times->{sched_dep}->epoch; - } + if ( $times->{sched_dep} and $times->{rt_dep} ) { + $times->{dep_delay} + = $times->{rt_dep}->epoch - $times->{sched_dep}->epoch; + } + if ( $times->{sched_dep} or $times->{rt_dep} ) { $times->{dep} = $times->{rt_dep} || $times->{sched_dep}; $times->{dep_countdown} = $times->{dep}->epoch - $epoch; } @@ -566,7 +577,8 @@ sub set_polyline { $self->set_polyline_id( uid => $uid, db => $db, - polyline_id => $polyline_id + polyline_id => $polyline_id, + train_id => $opt{train_id}, ); } @@ -579,11 +591,13 @@ sub set_polyline_id { my $db = $opt{db} // $self->{pg}->db; my $polyline_id = $opt{polyline_id}; - $db->update( - 'in_transit', - { polyline_id => $polyline_id }, - { user_id => $uid } - ); + my %where = ( user_id => $uid ); + + if ( $opt{train_id} ) { + $where{train_id} = $opt{train_id}; + } + + $db->update( 'in_transit', { polyline_id => $polyline_id }, \%where ); } sub set_route_data { @@ -596,6 +610,12 @@ sub set_route_data { my $qos_msg = $opt{qos_messages}; my $him_msg = $opt{him_messages}; + my %where = ( user_id => $uid ); + + if ( $opt{train_id} ) { + $where{train_id} = $opt{train_id}; + } + my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } ) ->expand->hash; @@ -612,7 +632,7 @@ sub set_route_data { route => JSON->new->encode($route), data => JSON->new->encode($data) }, - { user_id => $uid } + \%where ); } @@ -839,6 +859,12 @@ sub update_data { my $db = $opt{db} // $self->{pg}->db; my $new_data = $opt{data} // {}; + my %where = ( user_id => $uid ); + + if ( $opt{train_id} ) { + $where{train_id} = $opt{train_id}; + } + my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } ) ->expand->hash; @@ -848,11 +874,7 @@ sub update_data { $data->{$k} = $v; } - $db->update( - 'in_transit', - { data => JSON->new->encode($data) }, - { user_id => $uid } - ); + $db->update( 'in_transit', { data => JSON->new->encode($data) }, \%where ); } sub update_user_data { @@ -862,6 +884,12 @@ sub update_user_data { my $db = $opt{db} // $self->{pg}->db; my $new_data = $opt{user_data} // {}; + my %where = ( user_id => $uid ); + + if ( $opt{train_id} ) { + $where{train_id} = $opt{train_id}; + } + my $res_h = $db->select( 'in_transit', ['user_data'], { user_id => $uid } ) ->expand->hash; @@ -871,11 +899,8 @@ sub update_user_data { $data->{$k} = $v; } - $db->update( - 'in_transit', - { user_data => JSON->new->encode($data) }, - { user_id => $uid } - ); + $db->update( 'in_transit', + { user_data => JSON->new->encode($data) }, \%where ); } sub update_visibility { |