Skip to content

Commit

Permalink
Improved logging for bunq routine. #1607
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Oct 31, 2018
1 parent 5626fee commit 53ed5b2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/Factory/TransactionCurrencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurre
$currencyId = (int)$currencyId;

if ('' === $currencyCode && 0 === $currencyId) {
Log::warning('Cannot find anything on empty currency code and empty currency ID!');
Log::debug('Cannot find anything on empty currency code and empty currency ID!');

return null;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Import/JobConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function index(ImportJob $importJob)
Log::debug('Now in JobConfigurationController::index()');
$allowed = ['has_prereq', 'need_job_config'];
if (null !== $importJob && !\in_array($importJob->status, $allowed, true)) {
Log::debug(sprintf('Job has state "%s", but we only accept %s', $importJob->status, json_encode($allowed)));
Log::error(sprintf('Job has state "%s", but we only accept %s', $importJob->status, json_encode($allowed)));
session()->flash('error', (string)trans('import.bad_job_status', ['status' => $importJob->status]));

return redirect(route('import.index'));
Expand Down
10 changes: 7 additions & 3 deletions app/Http/Controllers/Import/JobStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function json(ImportJob $importJob): JsonResponse
*/
public function start(ImportJob $importJob): JsonResponse
{
Log::debug('Now in JobStatusController::start');
Log::info('Now in JobStatusController::start');
// catch impossible status:
$allowed = ['ready_to_run', 'need_job_config'];

Expand All @@ -152,8 +152,10 @@ public function start(ImportJob $importJob): JsonResponse
$className = config($key);
if (null === $className || !class_exists($className)) {
// @codeCoverageIgnoreStart
$message = sprintf('Cannot find import routine class for job of type "%s".', $importProvider);
Log::error($message);
return response()->json(
['status' => 'NOK', 'message' => sprintf('Cannot find import routine class for job of type "%s".', $importProvider)]
['status' => 'NOK', 'message' => $message]
);
// @codeCoverageIgnoreEnd
}
Expand All @@ -179,6 +181,7 @@ public function start(ImportJob $importJob): JsonResponse
}

// expect nothing from routine, just return OK to user.
Log::info('Now finished with JobStatusController::start');
return response()->json(['status' => 'OK', 'message' => 'stage_finished']);
}

Expand All @@ -194,6 +197,7 @@ public function start(ImportJob $importJob): JsonResponse
*/
public function store(ImportJob $importJob): JsonResponse
{
Log::info('Now in JobStatusController::store');
// catch impossible status:
$allowed = ['provider_finished', 'storing_data'];
if (null !== $importJob && !\in_array($importJob->status, $allowed, true)) {
Expand Down Expand Up @@ -222,7 +226,7 @@ public function store(ImportJob $importJob): JsonResponse
// set storage to be finished:
$this->repository->setStatus($importJob, 'storage_finished');


Log::info('Now finished with JobStatusController::start');
// expect nothing from routine, just return OK to user.
return response()->json(['status' => 'OK', 'message' => 'storage_finished']);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Import/Routine/BunqRoutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BunqRoutine implements RoutineInterface
*/
public function run(): void
{
Log::debug(sprintf('Now in BunqRoutine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage));
Log::info(sprintf('Now in BunqRoutine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage));
$valid = ['ready_to_run']; // should be only ready_to_run
if (\in_array($this->importJob->status, $valid, true)) {
switch ($this->importJob->stage) {
Expand Down
12 changes: 8 additions & 4 deletions app/Import/Storage/ImportArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,15 @@ private function storeArray(): Collection
$count = \count($array);
$toStore = [];

Log::debug(sprintf('Now in store(). Count of items is %d.', $count));
Log::notice(sprintf('Will now store the transactions. Count of items is %d.', $count));

/*
* Detect duplicates in initial array:
*/
foreach ($array as $index => $transaction) {
Log::debug(sprintf('Now at item %d out of %d', $index + 1, $count));
if ($this->duplicateDetected($index, $transaction)) {
Log::warning(sprintf('Row #%d seems to be a duplicate entry and will be ignored.', $index));
continue;
}
$transaction['importHashV2'] = $this->getHash($transaction);
Expand All @@ -443,13 +444,14 @@ private function storeArray(): Collection

return new Collection;
}

Log::debug('Going to store...');
Log::notice(sprintf('After a first check for duplicates, the count of items is %d.', $count));
Log::notice('Going to store...');
// now actually store them:
$collection = new Collection;
foreach ($toStore as $index => $store) {
// do duplicate detection again!
if ($this->duplicateDetected($index, $store)) {
Log::warning(sprintf('Row #%d seems to be a imported already and will be ignored.', $index), $store);
continue;
}

Expand All @@ -466,6 +468,8 @@ private function storeArray(): Collection
$this->repository->addErrorMessage($this->importJob, sprintf('Row #%d could not be imported. %s', $index, $e->getMessage()));
continue;
}

Log::info(sprintf('Stored #%d: "%s" (ID #%d)', $index, $journal->description, $journal->id));
Log::debug(sprintf('Stored as journal #%d', $journal->id));
$collection->push($journal);

Expand All @@ -477,7 +481,7 @@ private function storeArray(): Collection
Log::debug(sprintf('List length is now %d', $this->transfers->count()));
}
}
Log::debug('DONE storing!');
Log::notice(sprintf('Done storing. Firefly III has stored %d transactions.', $collection->count()));

return $collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;

/**
* Class ChooseAccountsHandler
Expand Down Expand Up @@ -119,6 +120,9 @@ public function configureJob(array $data): MessageBag
$config['apply-rules'] = $applyRules;
$this->repository->setConfiguration($this->importJob, $config);

Log::info('Account mapping: ', $final);
Log::info('Bunq IBAN array: ', $ibanToAsset);

return new MessageBag;
}

Expand Down
18 changes: 11 additions & 7 deletions app/Support/Import/Routine/Bunq/StageImportDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public function run(): void
foreach ($accounts as $bunqAccount) {
$bunqAccountId = $bunqAccount['id'] ?? 0;
$localId = $mapping[$bunqAccountId] ?? 0;
Log::debug(sprintf('Looping accounts, now at bunq account #%d and local account #%d', $bunqAccountId, $localId));
if (0 !== $localId && 0 !== $bunqAccountId) {
Log::info(sprintf('Now at bunq account #%d and local account #%d', $bunqAccountId, $localId));
$localAccount = $this->getLocalAccount((int)$localId);
$collection[] = $this->getTransactionsFromBunq($bunqAccountId, $localAccount);
}
Expand Down Expand Up @@ -205,6 +205,7 @@ private function convertPayment(BunqPayment $payment, int $bunqAccountId, LocalA
],
],
];
Log::info(sprintf('Parsed %s: "%s" (%s).', $created->format('Y-m-d'), $storeData['description'], $storeData['transactions'][0]['amount']));

return $storeData;
}
Expand Down Expand Up @@ -267,7 +268,7 @@ private function convertToAccount(LabelMonetaryAccount $party, string $expectedT
$account = $this->accountFactory->create($data);
Log::debug(
sprintf(
'Converted label monetary account %s to %s account %s (#%d)',
'Converted label monetary account "%s" to "%s" account "%s" (#%d)',
$party->getLabelUser()->getDisplayName(),
$expectedType,
$account->name, $account->id
Expand Down Expand Up @@ -362,13 +363,15 @@ private function getTransactionsFromBunq(int $bunqAccountId, LocalAccount $local
$direction = $this->getDirection($bunqAccountId);
$return = [];
if (self::DOWNLOAD_BACKWARDS === $direction) {
Log::info('For this account we go backwards in time.');
// go back either from NULL or from ID.
// ID is the very last transaction downloaded from bunq.
$preference = \Preferences::getForUser($this->importJob->user, sprintf('bunq-oldest-transaction-%d', $bunqAccountId), 0);
$transactionId = 0 === $preference->data ? null : $preference->data;
$return = $this->goBackInTime($bunqAccountId, $localAccount, $transactionId);
}
if (self::DOWNLOAD_FORWARDS === $direction) {
Log::info('For this account we go forwards in time.');
// go forward from ID. There is no NULL, young padawan
$return = $this->goForwardInTime($bunqAccountId, $localAccount);
}
Expand Down Expand Up @@ -415,7 +418,7 @@ private function goBackInTime(int $bunqAccountId, LocalAccount $localAccount, in
*/
/** @var Payment $paymentRequest */
$paymentRequest = app(Payment::class);
$params = ['count' => 53, 'older_id' => $olderId];
$params = ['count' => 79, 'older_id' => $olderId];
$response = $paymentRequest->listing($bunqAccountId, $params);
$pagination = $response->getPagination();
Log::debug('Params for the request to bunq are: ', $params);
Expand Down Expand Up @@ -472,13 +475,13 @@ private function goBackInTime(int $bunqAccountId, LocalAccount $localAccount, in
*/
$oldestTransaction = 0;
}
sleep(1);


// half a second.
usleep(500000);
}
// store newest and oldest tranasction ID to be used later:
\Preferences::setForUser($this->importJob->user, sprintf('bunq-oldest-transaction-%d', $bunqAccountId), $oldestTransaction);
\Preferences::setForUser($this->importJob->user, sprintf('bunq-newest-transaction-%d', $bunqAccountId), $newestTransaction);
Log::info(sprintf('Downloaded and parsed %d transactions from bunq.', \count($return)));

return $return;
}
Expand Down Expand Up @@ -567,11 +570,12 @@ private function goForwardInTime(int $bunqAccountId, LocalAccount $localAccount)
$hasMoreTransactions = false;
$this->stillRunning = false;
}
sleep(1);
usleep(500000);
}

// store newest tranasction ID to be used later:
\Preferences::setForUser($this->importJob->user, sprintf('bunq-newest-transaction-%d', $bunqAccountId), $newestTransaction);
Log::info(sprintf('Downloaded and parsed %d transactions from bunq.', \count($return)));

return $return;
}
Expand Down
27 changes: 27 additions & 0 deletions app/Support/Import/Routine/Bunq/StageNewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class StageNewHandler
*/
public function run(): void
{
Log::info('Now in StageNewHandler::run()');
/** @var Preference $preference */
$preference = app('preferences')->getForUser($this->importJob->user, 'bunq_api_context', null);
if (null !== $preference && '' !== (string)$preference->data) {
Expand All @@ -68,6 +69,7 @@ public function run(): void
$config = $this->repository->getConfiguration($this->importJob);
$config['accounts'] = $accounts;
$this->repository->setConfiguration($this->importJob, $config);

return;
}
throw new FireflyException('The bunq API context is unexpectedly empty.'); // @codeCoverageIgnore
Expand Down Expand Up @@ -127,9 +129,11 @@ private function listAccounts(): array
}
if (null !== $array) {
$accounts[] = $array;
$this->reportFinding($array);
}
}
}
Log::info(sprintf('Found %d account(s) at bunq', \count($accounts)));

return $accounts;
}
Expand Down Expand Up @@ -274,4 +278,27 @@ private function processMal(MonetaryAccountLight $mal): array

return $return;
}

/**
* Basic report method.
*
* @param array $array
*/
private function reportFinding(array $array): void
{
$bunqId = $array['id'] ?? '';
$bunqDescription = $array['description'] ?? '';
$bunqIBAN = '';

// find IBAN:
$aliases = $array['aliases'] ?? [];
foreach ($aliases as $alias) {
$type = $alias['type'] ?? 'none';
if ('IBAN' === $type) {
$bunqIBAN = $alias['value'] ?? '';
}
}

Log::info(sprintf('Found account at bunq. ID #%d, title "%s" and IBAN "%s" ', $bunqId, $bunqDescription, $bunqIBAN));
}
}

0 comments on commit 53ed5b2

Please sign in to comment.