Unverified Commit 61b91fe4 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

log traewelling sync HTTP status to influxdb

parent 628bd319
Loading
Loading
Loading
Loading
+60 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ has usage => sub { shift->extract_usage };

sub pull_sync {
	my ($self) = @_;
	my %pull_result;
	my $request_count = 0;
	for my $account_data ( $self->app->traewelling->get_pull_accounts ) {

@@ -48,6 +49,7 @@ sub pull_sync {
		)->then(
			sub {
				my ($traewelling) = @_;
				$pull_result{ $traewelling->{http} } += 1;
				$self->app->traewelling_to_travelynx(
					traewelling => $traewelling,
					user_data   => $account_data
@@ -56,19 +58,23 @@ sub pull_sync {
		)->catch(
			sub {
				my ($err) = @_;
				$pull_result{ $err->{http} // 0 } += 1;
				$self->app->traewelling->log(
					uid      => $account_data->{user_id},
					message  => "Fehler bei der Status-Abfrage: $err",
					message  => "Fehler bei der Status-Abfrage: $err->{text}",
					is_error => 1
				);
				$self->app->log->debug("Error $err");
				$self->app->log->debug("Error $err->{text}");
			}
		)->wait;
	}

	return \%pull_result;
}

sub push_sync {
	my ($self) = @_;
	my %push_result;

	for my $candidate ( $self->app->traewelling->get_pushable_accounts ) {
		$self->app->log->debug(
@@ -90,9 +96,21 @@ sub push_sync {
			$self->app->log->debug("... already handled");
			next;
		}
		$self->app->traewelling_api->checkin( %{$candidate},
			trip_id => $trip_id );
		$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;
	}

	return \%push_result;
}

sub run {
@@ -100,15 +118,17 @@ sub run {

	my $now        = DateTime->now( time_zone => 'Europe/Berlin' );
	my $started_at = $now;
	my $push_result;
	my $pull_result;

	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' );

	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' );
@@ -135,6 +155,40 @@ sub run {
			$self->app->ua->post_p( $self->app->config->{influxdb}->{url},
				"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;
				}
			}
		}
	}
}

+15 −8
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ sub get_status_p {
			if ( my $err = $tx->error ) {
				my $err_msg
				  = "v1/user/${username}/statuses: HTTP $err->{code} $err->{message}";
				$promise->reject($err_msg);
				$promise->reject( { http => $err->{code}, text => $err_msg } );
				return;
			}
			else {
@@ -118,6 +118,7 @@ sub get_status_p {
					my ( $train_type, $train_line ) = split( qr{ }, $linename );
					$promise->resolve(
						{
							http       => $tx->res->code,
							status_id  => $status_id,
							message    => $message,
							checkin    => $checkin_at,
@@ -138,7 +139,8 @@ sub get_status_p {
					return;
				}
				else {
					$promise->reject("v1/${username}/statuses: unknown error");
					$promise->reject(
						{ text => "v1/${username}/statuses: unknown error" } );
					return;
				}
			}
@@ -146,7 +148,7 @@ sub get_status_p {
	)->catch(
		sub {
			my ($err) = @_;
			$promise->reject("v1/${username}/statuses: $err");
			$promise->reject( { text => "v1/${username}/statuses: $err" } );
			return;
		}
	)->wait;
@@ -320,7 +322,7 @@ sub logout_p {
	return $promise;
}

sub checkin {
sub checkin_p {
	my ( $self, %opt ) = @_;

	my $header = {
@@ -356,10 +358,11 @@ sub checkin {
		$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
	  = "v1/trains/checkin('$request->{lineName}' $request->{tripId} $request->{start} -> $request->{destination})";

	my $promise = Mojo::Promise->new;

	$self->{user_agent}->request_timeout(20)
	  ->post_p(
		"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",
					is_error => 1
				);
				$promise->reject( { http => $err->{code} } );
				return;
			}
			$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(
				uid       => $opt{uid},
				message   => "Eingecheckt in $opt{train_type} $opt{train_no}",
@@ -405,9 +406,11 @@ sub checkin {
				uid => $opt{uid},
				ts  => $opt{checkin_ts}
			);
			$promise->resolve( { http => $tx->res->code } );

			# TODO store status_id in in_transit object so that it can be shown
			# on the user status page
			return;
		}
	)->catch(
		sub {
@@ -419,8 +422,12 @@ sub checkin {
"Konnte $opt{train_type} $opt{train_no} nicht übertragen: $debug_prefix returned $err",
				is_error => 1
			);
			$promise->reject( { connection => $err } );
			return;
		}
	)->wait;

	return $promise;
}

1;