Import routine cleanup.

This commit is contained in:
James Cole
2017-06-24 06:57:24 +02:00
parent 445dbf8779
commit e525e673a8
33 changed files with 245 additions and 583 deletions

View File

@@ -18,7 +18,7 @@ namespace FireflyIII\Import\Converter;
*
* @package FireflyIII\Import\Converter
*/
class Amount extends BasicConverter implements ConverterInterface
class Amount implements ConverterInterface
{
/**
@@ -28,9 +28,9 @@ class Amount extends BasicConverter implements ConverterInterface
*
* @param $value
*
* @return float
* @return string
*/
public function convert($value): float
public function convert($value): string
{
$len = strlen($value);
$decimalPosition = $len - 3;
@@ -59,10 +59,7 @@ class Amount extends BasicConverter implements ConverterInterface
$value = str_replace($search, '', $value);
}
$this->setCertainty(90);
return round(floatval($value), 12);
return strval(round(floatval($value), 12));
}
}

View File

@@ -1,85 +0,0 @@
<?php
/**
* BasicConverter.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Converter;
use FireflyIII\User;
/**
* Class BasicConverter
*
* @package FireflyIII\Import\Converter
*/
class BasicConverter
{
/** @var int */
public $certainty = 50;
/** @var array */
public $config;
/** @var bool */
public $doMap;
/** @var array */
public $mapping = [];
/** @var User */
public $user;
/**
* @return int
*/
public function getCertainty(): int
{
return $this->certainty;
}
/**
* @param int $certainty
*/
protected function setCertainty(int $certainty)
{
$this->certainty = $certainty;
}
/**
* @param array $config
*/
public function setConfig(array $config)
{
$this->config = $config;
}
/**
* @param mixed $doMap
*/
public function setDoMap(bool $doMap)
{
$this->doMap = $doMap;
}
/**
* @param array $mapping
*
*/
public function setMapping(array $mapping)
{
$this->mapping = $mapping;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
}

View File

@@ -13,8 +13,6 @@ declare(strict_types=1);
namespace FireflyIII\Import\Converter;
use FireflyIII\User;
/**
* Interface ConverterInterface
*
@@ -27,30 +25,4 @@ interface ConverterInterface
*
*/
public function convert($value);
/**
* @return int
*/
public function getCertainty(): int;
/**
* @param array $config
*/
public function setConfig(array $config);
/**
* @param bool $doMap
*/
public function setDoMap(bool $doMap);
/**
* @param array $mapping
*
*/
public function setMapping(array $mapping);
/**
* @param User $user
*/
public function setUser(User $user);
}

View File

@@ -20,7 +20,7 @@ use Log;
*
* @package FireflyIII\Import\Converter
*/
class INGDebetCredit extends BasicConverter implements ConverterInterface
class INGDebetCredit implements ConverterInterface
{
/**
@@ -34,12 +34,10 @@ class INGDebetCredit extends BasicConverter implements ConverterInterface
if ($value === 'Af') {
Log::debug('Return -1');
$this->setCertainty(100);
return -1;
}
$this->setCertainty(100);
Log::debug('Return 1');
return 1;

View File

@@ -20,7 +20,7 @@ use Log;
*
* @package FireflyIII\Import\Converter
*/
class RabobankDebetCredit extends BasicConverter implements ConverterInterface
class RabobankDebetCredit implements ConverterInterface
{
/**
@@ -34,13 +34,11 @@ class RabobankDebetCredit extends BasicConverter implements ConverterInterface
if ($value === 'D') {
Log::debug('Return -1');
$this->setCertainty(100);
return -1;
}
Log::debug('Return 1');
$this->setCertainty(100);
return 1;
}

View File

@@ -1,86 +0,0 @@
<?php
/**
* TagSplit.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* Class TagSplit
*
* @package FireflyIII\Import\Converter
*/
class TagSplit
{
/**
* @param User $user
* @param array $mapping
* @param array $parts
*
* @return Collection
*/
public static function createSetFromSplits(User $user, array $mapping, array $parts): Collection
{
$set = new Collection;
Log::debug('Exploded parts.', $parts);
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$repository->setUser($user);
/** @var string $part */
foreach ($parts as $part) {
if (isset($mapping[$part])) {
Log::debug('Found tag in mapping. Should exist.', ['value' => $part, 'map' => $mapping[$part]]);
$tag = $repository->find(intval($mapping[$part]));
if (!is_null($tag->id)) {
Log::debug('Found tag by ID', ['id' => $tag->id]);
$set->push($tag);
continue;
}
}
// not mapped? Still try to find it first:
$tag = $repository->findByTag($part);
if (!is_null($tag->id)) {
Log::debug('Found tag by name ', ['id' => $tag->id]);
$set->push($tag);
}
if (is_null($tag->id)) {
// create new tag
$tag = $repository->store(
[
'tag' => $part,
'date' => null,
'description' => $part,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
]
);
Log::debug('Created new tag', ['name' => $part, 'id' => $tag->id]);
$set->push($tag);
}
}
return $set;
}
}

View File

@@ -1,48 +0,0 @@
<?php
/**
* TagsComma.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Converter;
use Illuminate\Support\Collection;
use Log;
/**
* Class TagsComma
*
* @package FireflyIII\Import\Converter
*/
class TagsComma extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Collection
*/
public function convert($value)
{
$value = trim($value);
Log::debug('Going to convert using TagsComma', ['value' => $value]);
if (strlen($value) === 0) {
$this->setCertainty(0);
return new Collection;
}
$parts = array_unique(explode(',', $value));
$set = TagSplit::createSetFromSplits($this->user, $this->mapping, $parts);
$this->setCertainty(100);
return $set;
}
}

View File

@@ -1,49 +0,0 @@
<?php
/**
* TagsSpace.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Converter;
use Illuminate\Support\Collection;
use Log;
/**
* Class TagsSpace
*
* @package FireflyIII\Import\Converter
*/
class TagsSpace extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Collection
*/
public function convert($value)
{
$value = trim($value);
Log::debug('Going to convert using TagsSpace', ['value' => $value]);
if (strlen($value) === 0) {
$this->setCertainty(0);
return new Collection;
}
$parts = array_unique(explode(' ', $value));
$set = TagSplit::createSetFromSplits($this->user, $this->mapping, $parts);
$this->setCertainty(100);
return $set;
}
}

View File

@@ -70,6 +70,7 @@ class CsvProcessor implements FileProcessorInterface
$index = 0;
Log::notice('Building importable objects from CSV file.');
foreach ($entries as $index => $row) {
sleep(1);
// verify if not exists already:
if ($this->rowAlreadyImported($row)) {
$message = sprintf('Row #%d has already been imported.', $index);

View File

@@ -50,7 +50,7 @@ class AssetAccountIbans implements MapperInterface
asort($list);
$list = $topList + $list;
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -47,7 +47,7 @@ class AssetAccounts implements MapperInterface
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -40,7 +40,7 @@ class Bills implements MapperInterface
}
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -41,7 +41,7 @@ class Budgets implements MapperInterface
}
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -41,7 +41,7 @@ class Categories implements MapperInterface
}
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -56,7 +56,7 @@ class OpposingAccountIbans implements MapperInterface
asort($list);
$list = $topList + $list;
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -53,7 +53,7 @@ class OpposingAccounts implements MapperInterface
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;
}

View File

@@ -40,7 +40,7 @@ class Tags implements MapperInterface
}
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -36,7 +36,7 @@ class TransactionCurrencies implements MapperInterface
asort($list);
$list = [0 => trans('csv.do_not_map')] + $list;
$list = [0 => trans('csv.map_do_not_map')] + $list;
return $list;

View File

@@ -104,7 +104,7 @@ class ImportJournal
/** @var ConverterInterface $amountConverter */
$amountConverter = app(Amount::class);
$this->amount = strval($amountConverter->convert($this->amount));
$this->amount = $amountConverter->convert($this->amount);
// modify
foreach ($this->modifiers as $modifier) {
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $modifier['role'])));

View File

@@ -12,9 +12,13 @@ declare(strict_types=1);
namespace FireflyIII\Import\Routine;
use Carbon\Carbon;
use FireflyIII\Import\FileProcessor\FileProcessorInterface;
use FireflyIII\Import\Storage\ImportStorage;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@@ -53,8 +57,37 @@ class ImportRoutine
return false;
}
set_time_limit(0);
Log::debug(sprintf('Start with import job %s', $this->job->key));
$importObjects = $this->getImportObjects();
$this->lines = $importObjects->count();
// once done, use storage thing to actually store them:
Log::debug(sprintf('Returned %d valid objects from file processor', $this->lines));
$storage = $this->storeObjects($importObjects);
// update job:
$this->job->status = 'finished';
$this->job->save();
$this->journals = $storage->journals;
$this->errors = $storage->errors;
// create tag, link tag to all journals:
$this->createImportTag();
Log::debug(sprintf('Done with import job %s', $this->job->key));
return true;
}
/**
* @return Collection
*/
protected function getImportObjects(): Collection
{
$objects = new Collection;
$type = $this->job->file_type;
$class = config(sprintf('firefly.import_processors.%s', $type));
@@ -62,7 +95,6 @@ class ImportRoutine
$processor = app($class);
$processor->setJob($this->job);
set_time_limit(0);
if ($this->job->status == 'configured') {
// set job as "running"...
@@ -73,28 +105,56 @@ class ImportRoutine
$processor->run();
$objects = $processor->getObjects();
}
$this->lines = $objects->count();
// once done, use storage thing to actually store them:
Log::debug(sprintf('Returned %d valid objects from file processor', $this->lines));
return $objects;
}
/**
*
*/
private function createImportTag(): Tag
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$repository->setUser($this->job->user);
$data = [
'tag' => trans('firefly.import_with_key', ['key' => $this->job->key]),
'date' => new Carbon,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
];
$tag = $repository->store($data);
$extended = $this->job->extended_status;
$extended['tag'] = $tag->id;
$this->job->extended_status = $extended;
$this->job->save();
$this->journals->each(
function (TransactionJournal $journal) use ($tag) {
$journal->tags()->save($tag);
}
);
return $tag;
}
/**
* @param Collection $objects
*
* @return ImportStorage
*/
private function storeObjects(Collection $objects): ImportStorage
{
$storage = new ImportStorage;
$storage->setJob($this->job);
$storage->setDateFormat($this->job->configuration['date-format']);
$storage->setObjects($objects);
$storage->store();
// update job:
$this->job->status = 'finished';
$this->job->save();
$this->journals = $storage->journals;
$this->errors = $storage->errors;
// run rules:
Log::debug(sprintf('Done with import job %s', $this->job->key));
return true;
return $storage;
}
}

View File

@@ -99,7 +99,7 @@ class ImportStorage
* @var ImportJournal $object
*/
foreach ($this->objects as $index => $object) {
sleep(4);
sleep(1);
Log::debug(sprintf('Going to store object #%d with description "%s"', $index, $object->description));
$errors = new MessageBag;