Improve step count for spectre imports.

This commit is contained in:
James Cole
2018-01-13 07:52:35 +01:00
parent 50882f309b
commit fb863b0bf2
3 changed files with 37 additions and 28 deletions

View File

@@ -113,6 +113,7 @@ class StatusController extends Controller
$result['started'] = true; $result['started'] = true;
$result['running'] = true; $result['running'] = true;
} }
$result['percentage'] = $result['percentage'] > 100 ? 100 : $result['percentage'];
return Response::json($result); return Response::json($result);
} }

View File

@@ -216,7 +216,7 @@ class SpectreConfigurator implements ConfiguratorInterface
// set default extended status: // set default extended status:
$extendedStatus = $this->repository->getExtendedStatus($job); $extendedStatus = $this->repository->getExtendedStatus($job);
$extendedStatus['steps'] = 100; $extendedStatus['steps'] = 6;
// save to job: // save to job:
$job = $this->repository->setConfiguration($job, $finalConfig); $job = $this->repository->setConfiguration($job, $finalConfig);

View File

@@ -191,6 +191,7 @@ class SpectreRoutine implements RoutineInterface
} }
} }
Preferences::setForUser($this->job->user, 'spectre_customer', $customer->toArray()); Preferences::setForUser($this->job->user, 'spectre_customer', $customer->toArray());
return $customer; return $customer;
@@ -254,17 +255,11 @@ class SpectreRoutine implements RoutineInterface
$customer = $this->getCustomer(); $customer = $this->getCustomer();
Log::debug(sprintf('Customer ID is %s', $customer->getId())); Log::debug(sprintf('Customer ID is %s', $customer->getId()));
// add some steps done
$this->repository->addStepsDone($this->job, 2);
// use customer to request a token: // use customer to request a token:
$uri = route('import.status', [$this->job->key]); $uri = route('import.status', [$this->job->key]);
$token = $this->getToken($customer, $uri); $token = $this->getToken($customer, $uri);
Log::debug(sprintf('Token is %s', $token->getToken())); Log::debug(sprintf('Token is %s', $token->getToken()));
// add some steps done
$this->repository->addStepsDone($this->job, 2);
// update job, give it the token: // update job, give it the token:
$config = $this->getConfig(); $config = $this->getConfig();
$config['has-token'] = true; $config['has-token'] = true;
@@ -279,6 +274,7 @@ class SpectreRoutine implements RoutineInterface
// update job, set status to "configuring". // update job, set status to "configuring".
$this->setStatus('configuring'); $this->setStatus('configuring');
Log::debug(sprintf('Job status is now %s', $this->job->status)); Log::debug(sprintf('Job status is now %s', $this->job->status));
$this->addStep();
} }
/** /**
@@ -294,9 +290,6 @@ class SpectreRoutine implements RoutineInterface
$request->setCustomer($customer); $request->setCustomer($customer);
$request->call(); $request->call();
// add some steps done
$this->repository->addStepsDone($this->job, 2);
$logins = $request->getLogins(); $logins = $request->getLogins();
/** @var Login $final */ /** @var Login $final */
$final = null; $final = null;
@@ -314,15 +307,11 @@ class SpectreRoutine implements RoutineInterface
if (is_null($final)) { if (is_null($final)) {
Log::error('Could not find a valid login for this user.'); Log::error('Could not find a valid login for this user.');
$this->repository->addError($this->job, 0, 'Spectre connection failed. Did you use invalid credentials, press Cancel or failed the 2FA challenge?'); $this->repository->addError($this->job, 0, 'Spectre connection failed. Did you use invalid credentials, press Cancel or failed the 2FA challenge?');
$this->repository->setTotalSteps($this->job, 1);
$this->repository->setStepsDone($this->job, 1);
$this->repository->setStatus($this->job, 'error'); $this->repository->setStatus($this->job, 'error');
return; return;
} }
$this->addStep();
// add some steps done
$this->repository->addStepsDone($this->job, 2);
// list the users accounts using this login. // list the users accounts using this login.
$accountRequest = new ListAccountsRequest($this->job->user); $accountRequest = new ListAccountsRequest($this->job->user);
@@ -330,9 +319,6 @@ class SpectreRoutine implements RoutineInterface
$accountRequest->call(); $accountRequest->call();
$accounts = $accountRequest->getAccounts(); $accounts = $accountRequest->getAccounts();
// add some steps done
$this->repository->addStepsDone($this->job, 2);
// store accounts in job: // store accounts in job:
$all = []; $all = [];
/** @var Account $account */ /** @var Account $account */
@@ -348,13 +334,29 @@ class SpectreRoutine implements RoutineInterface
$this->setConfig($config); $this->setConfig($config);
$this->setStatus('configuring'); $this->setStatus('configuring');
$this->addStep();
// add some steps done
$this->repository->addStepsDone($this->job, 2);
return; return;
} }
/**
* Shorthand method.
*/
private function addStep()
{
$this->repository->addStepsDone($this->job, 1);
}
/**
* Shorthand
*
* @param int $steps
*/
private function addTotalSteps(int $steps)
{
$this->repository->addTotalSteps($this->job, $steps);
}
/** /**
* @return array * @return array
*/ */
@@ -364,6 +366,8 @@ class SpectreRoutine implements RoutineInterface
} }
/** /**
* Shorthand method.
*
* @return array * @return array
*/ */
private function getExtendedStatus(): array private function getExtendedStatus(): array
@@ -462,8 +466,11 @@ class SpectreRoutine implements RoutineInterface
$collection->push($importJournal); $collection->push($importJournal);
} }
} }
$this->addStep();
Log::debug(sprintf('Going to try and store all %d them.', $collection->count())); Log::debug(sprintf('Going to try and store all %d them.', $collection->count()));
// try to store them:
$this->addTotalSteps(7 * $collection->count());
// try to store them (seven steps per transaction)
$storage = new ImportStorage; $storage = new ImportStorage;
$storage->setJob($this->job); $storage->setJob($this->job);
@@ -494,15 +501,19 @@ class SpectreRoutine implements RoutineInterface
Log::debug('Looping journals...'); Log::debug('Looping journals...');
$journalIds = $storage->journals->pluck('id')->toArray(); $journalIds = $storage->journals->pluck('id')->toArray();
$tagId = $tag->id; $tagId = $tag->id;
$this->addTotalSteps(count($journalIds));
foreach ($journalIds as $journalId) { foreach ($journalIds as $journalId) {
Log::debug(sprintf('Linking journal #%d to tag #%d...', $journalId, $tagId)); Log::debug(sprintf('Linking journal #%d to tag #%d...', $journalId, $tagId));
DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journalId, 'tag_id' => $tagId]); DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journalId, 'tag_id' => $tagId]);
$this->addStep();
} }
Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $storage->journals->count(), $tag->id, $tag->tag)); Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $storage->journals->count(), $tag->id, $tag->tag));
// set status to "finished"? // set status to "finished"?
// update job: // update job:
$this->setStatus('finished'); $this->setStatus('finished');
$this->addStep();
return; return;
} }
@@ -537,14 +548,9 @@ class SpectreRoutine implements RoutineInterface
'transactions' => $transactions, 'transactions' => $transactions,
]; ];
$count += count($transactions); $count += count($transactions);
// add some steps done
$this->repository->addStepsDone($this->job, 2);
} }
// update number of steps:
$this->repository->setTotalSteps($this->job, $count * 5);
$this->repository->setStepsDone($this->job, 1);
Log::debug(sprintf('Total number of transactions: %d', $count)); Log::debug(sprintf('Total number of transactions: %d', $count));
$this->addStep();
$this->importTransactions($all); $this->importTransactions($all);
@@ -563,6 +569,8 @@ class SpectreRoutine implements RoutineInterface
} }
/** /**
* Shorthand method.
*
* @param array $extended * @param array $extended
*/ */
private function setExtendedStatus(array $extended): void private function setExtendedStatus(array $extended): void