2023-01-02 04:59:16 +00:00
|
|
|
package Travelynx::Command::traewelling;
|
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
# Copyright (C) 2023 Birte Kristina Friesel
|
2023-01-02 04:59:16 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use Mojo::Base 'Mojolicious::Command';
|
|
|
|
use Mojo::Promise;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use JSON;
|
|
|
|
use List::Util;
|
|
|
|
|
|
|
|
has description => 'Synchronize with Traewelling';
|
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage };
|
|
|
|
|
|
|
|
sub pull_sync {
|
|
|
|
my ($self) = @_;
|
2023-01-07 12:31:14 +00:00
|
|
|
my %pull_result;
|
2023-01-02 04:59:16 +00:00
|
|
|
my $request_count = 0;
|
|
|
|
for my $account_data ( $self->app->traewelling->get_pull_accounts ) {
|
|
|
|
|
|
|
|
my $in_transit = $self->app->in_transit->get(
|
|
|
|
uid => $account_data->{user_id},
|
|
|
|
);
|
|
|
|
if ($in_transit) {
|
|
|
|
$self->app->log->debug(
|
|
|
|
"Skipping Traewelling status pull for UID $account_data->{user_id}: already checked in"
|
|
|
|
);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2024-06-06 04:41:52 +00:00
|
|
|
if ( not defined $account_data->{data}{user_name} ) {
|
|
|
|
$self->app->log->error(
|
|
|
|
"travelynx user $account_data->{user_id} has a Traewellig connection, but no username"
|
|
|
|
);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2023-01-02 04:59:16 +00:00
|
|
|
# $account_data->{user_id} is the travelynx uid
|
|
|
|
# $account_data->{user_name} is the Träwelling username
|
|
|
|
$request_count += 1;
|
|
|
|
$self->app->log->debug(
|
|
|
|
"Scheduling Traewelling status pull for UID $account_data->{user_id}"
|
|
|
|
);
|
|
|
|
|
|
|
|
# In 'work', the event loop is not running,
|
|
|
|
# so there's no need to multiply by $request_count at the moment
|
2023-01-07 12:57:47 +00:00
|
|
|
Mojo::Promise->timer(1)->then(
|
2023-01-02 04:59:16 +00:00
|
|
|
sub {
|
|
|
|
return $self->app->traewelling_api->get_status_p(
|
|
|
|
username => $account_data->{data}{user_name},
|
|
|
|
token => $account_data->{token}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)->then(
|
|
|
|
sub {
|
|
|
|
my ($traewelling) = @_;
|
2023-01-07 12:31:14 +00:00
|
|
|
$pull_result{ $traewelling->{http} } += 1;
|
2023-07-09 12:46:28 +00:00
|
|
|
return $self->app->traewelling_to_travelynx_p(
|
2023-01-02 04:59:16 +00:00
|
|
|
traewelling => $traewelling,
|
|
|
|
user_data => $account_data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
2023-01-07 12:31:14 +00:00
|
|
|
$pull_result{ $err->{http} // 0 } += 1;
|
2023-01-02 04:59:16 +00:00
|
|
|
$self->app->traewelling->log(
|
|
|
|
uid => $account_data->{user_id},
|
2023-01-07 12:31:14 +00:00
|
|
|
message => "Fehler bei der Status-Abfrage: $err->{text}",
|
2023-01-02 04:59:16 +00:00
|
|
|
is_error => 1
|
|
|
|
);
|
2023-01-07 12:31:14 +00:00
|
|
|
$self->app->log->debug("Error $err->{text}");
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
|
|
|
}
|
2023-01-07 12:31:14 +00:00
|
|
|
|
|
|
|
return \%pull_result;
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub push_sync {
|
|
|
|
my ($self) = @_;
|
2023-01-07 12:31:14 +00:00
|
|
|
my %push_result;
|
2023-01-02 04:59:16 +00:00
|
|
|
|
|
|
|
for my $candidate ( $self->app->traewelling->get_pushable_accounts ) {
|
|
|
|
$self->app->log->debug(
|
|
|
|
"Pushing to Traewelling for UID $candidate->{uid}");
|
|
|
|
my $trip_id = $candidate->{journey_data}{trip_id};
|
|
|
|
if ( not $trip_id ) {
|
|
|
|
$self->app->log->debug("... trip_id is missing");
|
|
|
|
$self->app->traewelling->log(
|
|
|
|
uid => $candidate->{uid},
|
|
|
|
message =>
|
|
|
|
"Konnte $candidate->{train_type} $candidate->{train_no} nicht übertragen: Keine trip_id vorhanden",
|
|
|
|
is_error => 1
|
|
|
|
);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ( $candidate->{data}{latest_push_ts}
|
|
|
|
and $candidate->{data}{latest_push_ts} == $candidate->{checkin_ts} )
|
|
|
|
{
|
|
|
|
$self->app->log->debug("... already handled");
|
|
|
|
next;
|
|
|
|
}
|
2023-01-07 12:31:14 +00:00
|
|
|
$self->app->traewelling_api->checkin_p( %{$candidate},
|
|
|
|
trip_id => $trip_id )->then(
|
|
|
|
sub {
|
|
|
|
my ($status) = @_;
|
|
|
|
$push_result{ $status->{http} } += 1;
|
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($status) = @_;
|
|
|
|
$push_result{ $status->{http} // 0 } += 1;
|
|
|
|
}
|
|
|
|
)->wait;
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
2023-01-07 12:31:14 +00:00
|
|
|
|
|
|
|
return \%push_result;
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub run {
|
|
|
|
my ( $self, $direction ) = @_;
|
|
|
|
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
my $started_at = $now;
|
2023-01-07 12:31:14 +00:00
|
|
|
my $push_result;
|
|
|
|
my $pull_result;
|
2023-01-02 04:59:16 +00:00
|
|
|
|
|
|
|
if ( not $direction or $direction eq 'push' ) {
|
2023-01-07 12:31:14 +00:00
|
|
|
$push_result = $self->push_sync;
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $trwl_push_finished_at = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
|
|
|
|
if ( not $direction or $direction eq 'pull' ) {
|
2023-01-07 12:31:14 +00:00
|
|
|
$pull_result = $self->pull_sync;
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $trwl_pull_finished_at = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
|
|
|
|
my $trwl_push_duration = $trwl_push_finished_at->epoch - $started_at->epoch;
|
|
|
|
my $trwl_pull_duration
|
|
|
|
= $trwl_pull_finished_at->epoch - $trwl_push_finished_at->epoch;
|
|
|
|
my $trwl_duration = $trwl_pull_finished_at->epoch - $started_at->epoch;
|
|
|
|
|
|
|
|
if ( $self->app->config->{influxdb}->{url} ) {
|
|
|
|
my $report = "sync_runtime_seconds=${trwl_duration}";
|
|
|
|
if ( not $direction or $direction eq 'push' ) {
|
|
|
|
$report .= ",push_runtime_seconds=${trwl_push_duration}";
|
|
|
|
}
|
|
|
|
if ( not $direction or $direction eq 'pull' ) {
|
|
|
|
$report .= ",pull_runtime_seconds=${trwl_pull_duration}";
|
|
|
|
}
|
2023-01-07 12:07:29 +00:00
|
|
|
if ( $self->app->mode eq 'development' ) {
|
|
|
|
$self->app->log->debug( 'POST '
|
|
|
|
. $self->app->config->{influxdb}->{url}
|
|
|
|
. " traewelling ${report}" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->app->ua->post_p( $self->app->config->{influxdb}->{url},
|
|
|
|
"traewelling ${report}" )->wait;
|
|
|
|
}
|
2023-01-07 12:31:14 +00:00
|
|
|
|
|
|
|
if ($push_result) {
|
|
|
|
for my $status ( keys %{$push_result} ) {
|
|
|
|
my $count = $push_result->{$status};
|
|
|
|
if ( $self->app->mode eq 'development' ) {
|
|
|
|
$self->app->log->debug( 'POST '
|
|
|
|
. $self->app->config->{influxdb}->{url}
|
|
|
|
. " traewelling_push,http=$status count=$count" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->app->ua->post_p(
|
|
|
|
$self->app->config->{influxdb}->{url},
|
|
|
|
"traewelling_push,http=$status count=$count"
|
|
|
|
)->wait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($pull_result) {
|
|
|
|
for my $status ( keys %{$pull_result} ) {
|
|
|
|
my $count = $pull_result->{$status};
|
|
|
|
if ( $self->app->mode eq 'development' ) {
|
|
|
|
$self->app->log->debug( 'POST '
|
|
|
|
. $self->app->config->{influxdb}->{url}
|
|
|
|
. " traewelling_pull,http=$status count=$count" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->app->ua->post_p(
|
|
|
|
$self->app->config->{influxdb}->{url},
|
|
|
|
"traewelling_pull,http=$status count=$count"
|
|
|
|
)->wait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
Usage: index.pl traewelling [direction]
|
|
|
|
|
|
|
|
Performs both push and pull synchronization by default.
|
|
|
|
If "direction" is specified, only synchronizes in the specified direction
|
|
|
|
("push" or "pull")
|
|
|
|
|
|
|
|
Should be called from a cronjob every three to ten minutes.
|