log traewelling sync HTTP status to influxdb

This commit is contained in:
Daniel Friesel 2023-01-07 13:31:14 +01:00
parent 628bd319d6
commit 61b91fe4b2
No known key found for this signature in database
GPG key ID: 100D5BFB5166E005
2 changed files with 75 additions and 14 deletions

View file

@ -16,6 +16,7 @@ has usage => sub { shift->extract_usage };
sub pull_sync { sub pull_sync {
my ($self) = @_; my ($self) = @_;
my %pull_result;
my $request_count = 0; my $request_count = 0;
for my $account_data ( $self->app->traewelling->get_pull_accounts ) { for my $account_data ( $self->app->traewelling->get_pull_accounts ) {
@ -48,6 +49,7 @@ sub pull_sync {
)->then( )->then(
sub { sub {
my ($traewelling) = @_; my ($traewelling) = @_;
$pull_result{ $traewelling->{http} } += 1;
$self->app->traewelling_to_travelynx( $self->app->traewelling_to_travelynx(
traewelling => $traewelling, traewelling => $traewelling,
user_data => $account_data user_data => $account_data
@ -56,19 +58,23 @@ sub pull_sync {
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$pull_result{ $err->{http} // 0 } += 1;
$self->app->traewelling->log( $self->app->traewelling->log(
uid => $account_data->{user_id}, uid => $account_data->{user_id},
message => "Fehler bei der Status-Abfrage: $err", message => "Fehler bei der Status-Abfrage: $err->{text}",
is_error => 1 is_error => 1
); );
$self->app->log->debug("Error $err"); $self->app->log->debug("Error $err->{text}");
} }
)->wait; )->wait;
} }
return \%pull_result;
} }
sub push_sync { sub push_sync {
my ($self) = @_; my ($self) = @_;
my %push_result;
for my $candidate ( $self->app->traewelling->get_pushable_accounts ) { for my $candidate ( $self->app->traewelling->get_pushable_accounts ) {
$self->app->log->debug( $self->app->log->debug(
@ -90,9 +96,21 @@ sub push_sync {
$self->app->log->debug("... already handled"); $self->app->log->debug("... already handled");
next; next;
} }
$self->app->traewelling_api->checkin( %{$candidate}, $self->app->traewelling_api->checkin_p( %{$candidate},
trip_id => $trip_id ); trip_id => $trip_id )->then(
sub {
my ($status) = @_;
$push_result{ $status->{http} } += 1;
}
)->catch(
sub {
my ($status) = @_;
$push_result{ $status->{http} // 0 } += 1;
}
)->wait;
} }
return \%push_result;
} }
sub run { sub run {
@ -100,15 +118,17 @@ sub run {
my $now = DateTime->now( time_zone => 'Europe/Berlin' ); my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my $started_at = $now; my $started_at = $now;
my $push_result;
my $pull_result;
if ( not $direction or $direction eq 'push' ) { if ( not $direction or $direction eq 'push' ) {
$self->push_sync; $push_result = $self->push_sync;
} }
my $trwl_push_finished_at = DateTime->now( time_zone => 'Europe/Berlin' ); my $trwl_push_finished_at = DateTime->now( time_zone => 'Europe/Berlin' );
if ( not $direction or $direction eq 'pull' ) { if ( not $direction or $direction eq 'pull' ) {
$self->pull_sync; $pull_result = $self->pull_sync;
} }
my $trwl_pull_finished_at = DateTime->now( time_zone => 'Europe/Berlin' ); my $trwl_pull_finished_at = DateTime->now( time_zone => 'Europe/Berlin' );
@ -135,6 +155,40 @@ sub run {
$self->app->ua->post_p( $self->app->config->{influxdb}->{url}, $self->app->ua->post_p( $self->app->config->{influxdb}->{url},
"traewelling ${report}" )->wait; "traewelling ${report}" )->wait;
} }
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;
}
}
}
} }
} }

View file

@ -83,7 +83,7 @@ sub get_status_p {
if ( my $err = $tx->error ) { if ( my $err = $tx->error ) {
my $err_msg my $err_msg
= "v1/user/${username}/statuses: HTTP $err->{code} $err->{message}"; = "v1/user/${username}/statuses: HTTP $err->{code} $err->{message}";
$promise->reject($err_msg); $promise->reject( { http => $err->{code}, text => $err_msg } );
return; return;
} }
else { else {
@ -118,6 +118,7 @@ sub get_status_p {
my ( $train_type, $train_line ) = split( qr{ }, $linename ); my ( $train_type, $train_line ) = split( qr{ }, $linename );
$promise->resolve( $promise->resolve(
{ {
http => $tx->res->code,
status_id => $status_id, status_id => $status_id,
message => $message, message => $message,
checkin => $checkin_at, checkin => $checkin_at,
@ -138,7 +139,8 @@ sub get_status_p {
return; return;
} }
else { else {
$promise->reject("v1/${username}/statuses: unknown error"); $promise->reject(
{ text => "v1/${username}/statuses: unknown error" } );
return; return;
} }
} }
@ -146,7 +148,7 @@ sub get_status_p {
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$promise->reject("v1/${username}/statuses: $err"); $promise->reject( { text => "v1/${username}/statuses: $err" } );
return; return;
} }
)->wait; )->wait;
@ -320,7 +322,7 @@ sub logout_p {
return $promise; return $promise;
} }
sub checkin { sub checkin_p {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
my $header = { my $header = {
@ -356,10 +358,11 @@ sub checkin {
$request->{body} = $opt{user_data}{comment}; $request->{body} = $opt{user_data}{comment};
} }
# https://github.com/Traewelling/traewelling/blob/develop/app/Http/Controllers/API/v1/TransportController.php -> create. trains/checkin ist richtig.
my $debug_prefix my $debug_prefix
= "v1/trains/checkin('$request->{lineName}' $request->{tripId} $request->{start} -> $request->{destination})"; = "v1/trains/checkin('$request->{lineName}' $request->{tripId} $request->{start} -> $request->{destination})";
my $promise = Mojo::Promise->new;
$self->{user_agent}->request_timeout(20) $self->{user_agent}->request_timeout(20)
->post_p( ->post_p(
"https://traewelling.de/api/v1/trains/checkin" => $header => json => "https://traewelling.de/api/v1/trains/checkin" => $header => json =>
@ -389,13 +392,11 @@ sub checkin {
"Konnte $opt{train_type} $opt{train_no} nicht übertragen: $debug_prefix returned $err_msg", "Konnte $opt{train_type} $opt{train_no} nicht übertragen: $debug_prefix returned $err_msg",
is_error => 1 is_error => 1
); );
$promise->reject( { http => $err->{code} } );
return; return;
} }
$self->{log}->debug( "... success! " . $tx->res->body ); $self->{log}->debug( "... success! " . $tx->res->body );
# As of 2020-10-04, traewelling.de checkins do not yet return
# "statusId". The patch is present on the develop branch and waiting
# for a merge into master.
$self->{model}->log( $self->{model}->log(
uid => $opt{uid}, uid => $opt{uid},
message => "Eingecheckt in $opt{train_type} $opt{train_no}", message => "Eingecheckt in $opt{train_type} $opt{train_no}",
@ -405,9 +406,11 @@ sub checkin {
uid => $opt{uid}, uid => $opt{uid},
ts => $opt{checkin_ts} ts => $opt{checkin_ts}
); );
$promise->resolve( { http => $tx->res->code } );
# TODO store status_id in in_transit object so that it can be shown # TODO store status_id in in_transit object so that it can be shown
# on the user status page # on the user status page
return;
} }
)->catch( )->catch(
sub { sub {
@ -419,8 +422,12 @@ sub checkin {
"Konnte $opt{train_type} $opt{train_no} nicht übertragen: $debug_prefix returned $err", "Konnte $opt{train_type} $opt{train_no} nicht übertragen: $debug_prefix returned $err",
is_error => 1 is_error => 1
); );
$promise->reject( { connection => $err } );
return;
} }
)->wait; )->wait;
return $promise;
} }
1; 1;