Memory optimised, connect journal to tag.

This commit is contained in:
James Cole 2017-07-15 10:43:22 +02:00
parent 0c6c4d5959
commit c03ab269f0
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 21 additions and 9 deletions

View File

@ -13,11 +13,11 @@ namespace FireflyIII\Import\Routine;
use Carbon\Carbon; use Carbon\Carbon;
use DB;
use FireflyIII\Import\FileProcessor\FileProcessorInterface; use FireflyIII\Import\FileProcessor\FileProcessorInterface;
use FireflyIII\Import\Storage\ImportStorage; use FireflyIII\Import\Storage\ImportStorage;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Tag; use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@ -64,14 +64,19 @@ class ImportRoutine
Log::info(sprintf('Returned %d valid objects from file processor', $this->lines)); Log::info(sprintf('Returned %d valid objects from file processor', $this->lines));
$storage = $this->storeObjects($importObjects); $storage = $this->storeObjects($importObjects);
Log::debug('Back in run()');
// update job: // update job:
$this->job->status = 'finished'; $this->job->status = 'finished';
$this->job->save(); $this->job->save();
Log::debug('Updated job...');
$this->journals = $storage->journals; $this->journals = $storage->journals;
$this->errors = $storage->errors; $this->errors = $storage->errors;
Log::debug('Going to call createImportTag()');
// create tag, link tag to all journals: // create tag, link tag to all journals:
$this->createImportTag(); $this->createImportTag();
@ -101,7 +106,7 @@ class ImportRoutine
$processor = app($class); $processor = app($class);
$processor->setJob($this->job); $processor->setJob($this->job);
if ($this->job->status == 'configured') { if ($this->job->status === 'configured') {
// set job as "running"... // set job as "running"...
$this->job->status = 'running'; $this->job->status = 'running';
@ -120,6 +125,7 @@ class ImportRoutine
*/ */
private function createImportTag(): Tag private function createImportTag(): Tag
{ {
Log::debug('Now in createImportTag()');
/** @var TagRepositoryInterface $repository */ /** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class); $repository = app(TagRepositoryInterface::class);
$repository->setUser($this->job->user); $repository->setUser($this->job->user);
@ -138,11 +144,16 @@ class ImportRoutine
$this->job->extended_status = $extended; $this->job->extended_status = $extended;
$this->job->save(); $this->job->save();
$this->journals->each( Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag));
function (TransactionJournal $journal) use ($tag) { Log::debug('Looping journals...');
$journal->tags()->save($tag); $journalIds = $this->journals->pluck('id')->toArray();
} $tagId = $tag->id;
); foreach ($journalIds as $journalId) {
Log::debug(sprintf('Linking journal #%d to tag #%d...', $journalId, $tagId));
DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journalId, 'tag_id' => $tagId]);
}
Log::debug('Done!');
Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $this->journals->count(), $tag->id, $tag->tag));
return $tag; return $tag;
@ -160,6 +171,7 @@ class ImportRoutine
$storage->setDateFormat($this->job->configuration['date-format']); $storage->setDateFormat($this->job->configuration['date-format']);
$storage->setObjects($objects); $storage->setObjects($objects);
$storage->store(); $storage->store();
Log::info('Back in storeObjects()');
return $storage; return $storage;
} }

View File

@ -116,8 +116,9 @@ class ImportStorage
Log::error(sprintf('Cannot import row #%d because: %s', $index, $e->getMessage())); Log::error(sprintf('Cannot import row #%d because: %s', $index, $e->getMessage()));
} }
} }
Log::info('ImportStorage has finished.');
return true;
} }
/** /**
@ -383,7 +384,6 @@ class ImportStorage
// run rules: // run rules:
$this->applyRules($journal); $this->applyRules($journal);
$this->job->addStepsDone(1); $this->job->addStepsDone(1);
$this->journals->push($journal); $this->journals->push($journal);
Log::info( Log::info(