Implement first version of the new rule engine.

This commit is contained in:
James Cole 2020-08-22 16:55:54 +02:00
parent 14df37712c
commit 216a0a186c
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
12 changed files with 825 additions and 586 deletions

View File

@ -33,12 +33,14 @@ use FireflyIII\Models\Rule;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngine;
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
use FireflyIII\TransactionRules\TransactionMatcher;
use FireflyIII\Transformers\RuleTransformer;
use FireflyIII\Transformers\TransactionGroupTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
@ -215,8 +217,8 @@ class RuleController extends Controller
* @param RuleTestRequest $request
* @param Rule $rule
*
* @throws FireflyException
* @return JsonResponse
* @throws FireflyException
*/
public function testRule(RuleTestRequest $request, Rule $rule): JsonResponse
{
@ -265,28 +267,27 @@ class RuleController extends Controller
// Get parameters specified by the user
$parameters = $request->getTriggerParameters();
/** @var RuleEngine $ruleEngine */
$ruleEngine = app(RuleEngine::class);
$ruleEngine->setUser(auth()->user());
/** @var RuleEngineInterface $ruleEngine */
$ruleEngine = app(RuleEngineInterface::class);
$ruleEngine->setRules(new Collection([$rule]));
$rules = [$rule->id];
$ruleEngine->setRulesToApply($rules);
$ruleEngine->setTriggerMode(RuleEngine::TRIGGER_STORE);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($parameters['accounts']);
$collector->setRange($parameters['start_date'], $parameters['end_date']);
$journals = $collector->getExtractedJournals();
/** @var array $journal */
foreach ($journals as $journal) {
Log::debug('Start of new journal.');
$ruleEngine->processJournalArray($journal);
Log::debug('Done with all rules for this group + done with journal.');
// overrule the rule(s) if necessary.
if (array_key_exists('start', $parameters) && null !== $parameters['start'] ) {
// add a range:
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
}
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
// add a range:
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
}
if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) {
$ruleEngine->addOperator(['type' => 'account_id', 'value' => $parameters['accounts']]);
}
// file the rule(s)
$ruleEngine->fire();
return response()->json([], 204);
}

View File

@ -28,11 +28,8 @@ namespace FireflyIII\Api\V1\Requests;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
* Class RuleTriggerRequest
@ -40,6 +37,7 @@ use Log;
class RuleTriggerRequest extends FormRequest
{
use ConvertsDataTypes;
/**
* Authorize logged in users.
*
@ -57,9 +55,9 @@ class RuleTriggerRequest extends FormRequest
public function getTriggerParameters(): array
{
return [
'start_date' => $this->getDate('start_date'),
'end_date' => $this->getDate('end_date'),
'accounts' => $this->getAccounts(),
'start' => $this->getDate('start'),
'end' => $this->getDate('end'),
'accounts' => $this->getAccounts(),
];
}
@ -69,33 +67,17 @@ class RuleTriggerRequest extends FormRequest
public function rules(): array
{
return [
'start_date' => 'required|date',
'end_date' => 'required|date|after:start_date',
'start' => 'date',
'end' => 'date|after:start',
];
}
/**
* @return Collection
* @return string
*/
private function getAccounts(): Collection
private function getAccounts(): string
{
$accountList = '' === (string) $this->query('accounts') ? [] : explode(',', $this->query('accounts'));
$accounts = new Collection;
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
foreach ($accountList as $accountId) {
Log::debug(sprintf('Searching for asset account with id "%s"', $accountId));
$account = $accountRepository->findNull((int) $accountId);
if ($this->validAccount($account)) {
/** @noinspection NullPointerExceptionInspection */
Log::debug(sprintf('Found account #%d ("%s") and its an asset account', $account->id, $account->name));
$accounts->push($account);
}
}
return $accounts;
return (string) $this->query('accounts');
}
/**
@ -111,14 +93,4 @@ class RuleTriggerRequest extends FormRequest
return $result;
}
/**
* @param Account|null $account
*
* @return bool
*/
private function validAccount(?Account $account): bool
{
return null !== $account && AccountType::ASSET === $account->accountType->type;
}
}

View File

@ -35,23 +35,7 @@ use Log;
*/
class TagFactory
{
/** @var Collection */
private $tags;
/** @var User */
private $user;
/**
* Constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
private User $user;
/**
* @param array $data

View File

@ -63,6 +63,8 @@ use FireflyIII\Support\Navigation;
use FireflyIII\Support\Preferences;
use FireflyIII\Support\Steam;
use FireflyIII\Support\Telemetry;
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
use FireflyIII\TransactionRules\Engine\SearchRuleEngine;
use FireflyIII\Validation\FireflyValidator;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
@ -192,6 +194,19 @@ class FireflyServiceProvider extends ServiceProvider
}
);
$this->app->bind(
RuleEngineInterface::class,
static function (Application $app) {
/** @var SearchRuleEngine $engine */
$engine = app(SearchRuleEngine::class);
if ($app->auth->check()) {
$engine->setUser(auth()->user());
}
return $engine;
}
);
// more generators:
$this->app->bind(PopupReportInterface::class, PopupReport::class);
$this->app->bind(HelpInterface::class, Help::class);

View File

@ -62,6 +62,7 @@ class OperatorQuerySearch implements SearchInterface
private User $user;
private ParsedQuery $query;
private int $page;
private int $limit;
private array $words;
private array $validOperators;
private GroupCollectorInterface $collector;
@ -80,6 +81,7 @@ class OperatorQuerySearch implements SearchInterface
$this->operators = new Collection;
$this->page = 1;
$this->words = [];
$this->limit = 25;
$this->validOperators = array_keys(config('firefly.search.operators'));
$this->startTime = microtime(true);
$this->accountRepository = app(AccountRepositoryInterface::class);
@ -154,11 +156,9 @@ class OperatorQuerySearch implements SearchInterface
$parser = new QueryParser();
$this->query = $parser->parse($query);
// get limit from preferences.
$pageSize = (int) app('preferences')->getForUser($this->user, 'listPageSize', 50)->data;
$this->collector = app(GroupCollectorInterface::class);
$this->collector->setUser($this->user);
$this->collector->setLimit($pageSize)->setPage($this->page);
$this->collector->setLimit($this->limit)->setPage($this->page);
$this->collector->withAccountInformation()->withCategoryInformation()->withBudgetInformation();
Log::debug(sprintf('Found %d node(s)', count($this->query->getNodes())));
@ -203,6 +203,8 @@ class OperatorQuerySearch implements SearchInterface
$this->billRepository->setUser($user);
$this->categoryRepository->setUser($user);
$this->budgetRepository->setUser($user);
$this->setLimit((int) app('preferences')->getForUser($user, 'listPageSize', 50)->data);
}
/**
@ -328,9 +330,16 @@ class OperatorQuerySearch implements SearchInterface
}
break;
case 'account_id':
$account = $this->accountRepository->findNull((int) $value);
if (null !== $account) {
$this->collector->setAccounts(new Collection([$account]));
$parts = explode(',', $value);
$collection = new Collection;
foreach ($parts as $accountId) {
$account = $this->accountRepository->findNull((int) $value);
if (null !== $account) {
$collection->push($account);
}
}
if ($collection->count() > 0) {
$this->collector->setAccounts($collection);
}
break;
//
@ -695,4 +704,11 @@ class OperatorQuerySearch implements SearchInterface
];
}
/**
* @param int $limit
*/
public function setLimit(int $limit): void
{
$this->limit = $limit;
}
}

View File

@ -51,6 +51,11 @@ interface SearchInterface
*/
public function setPage(int $page): void;
/**
* @param int $limit
*/
public function setLimit(int $limit): void;
/**
* @return bool
*/

View File

@ -45,4 +45,12 @@ interface ActionInterface
* @return bool
*/
public function act(TransactionJournal $journal): bool;
/**
* Execute the action on an array.
*
* @param array $journal
* @return bool
*/
public function actOnArray(array $journal): bool;
}

View File

@ -22,9 +22,11 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use DB;
use FireflyIII\Factory\TagFactory;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
use Log;
/**
@ -32,8 +34,7 @@ use Log;
*/
class AddTag implements ActionInterface
{
/** @var RuleAction The rule action */
private $action;
private RuleAction $action;
/**
* TriggerInterface constructor.
@ -46,11 +47,8 @@ class AddTag implements ActionInterface
}
/**
* Add a tag
*
* @param TransactionJournal $journal
*
* @return bool
* @inheritDoc
* @deprecated
*/
public function act(TransactionJournal $journal): bool
{
@ -77,4 +75,36 @@ class AddTag implements ActionInterface
return false;
}
/**
* @inheritDoc
*/
public function actOnArray(array $journal): bool
{
// journal has this tag maybe?
/** @var TagFactory $factory */
$factory = app(TagFactory::class);
$factory->setUser(User::find($journal['user_id']));
$tag = $factory->findOrCreate($this->action->action_value);
if (null === $tag) {
// could not find, could not create tag.
Log::error(sprintf('RuleAction AddTag. Could not find or create tag "%s"', $this->action->action_value));
return false;
}
$count = DB::table('tag_transaction_journal')
->where('tag_id', $tag->id)
->where('transaction_journal_id', $journal['transaction_journal_id'])
->count();
if (0 === $count) {
// add to journal:
DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]);
Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id']));
return true;
}
Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id']));
return false;
}
}

View File

@ -21,6 +21,7 @@
namespace FireflyIII\TransactionRules\Engine;
use FireflyIII\User;
use Illuminate\Support\Collection;
/**
@ -28,6 +29,10 @@ use Illuminate\Support\Collection;
*/
interface RuleEngineInterface
{
/**
* @param User $user
*/
public function setUser(User $user): void;
/**
* Add rules for the engine to execute.
*
@ -49,4 +54,9 @@ interface RuleEngineInterface
*/
public function addOperator(array $operator): void;
/**
* Fire the rule engine.
*/
public function fire(): void;
}

View File

@ -21,10 +21,179 @@
namespace FireflyIII\TransactionRules\Engine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Support\Search\SearchInterface;
use FireflyIII\TransactionRules\Factory\ActionFactory;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* Class SearchRuleEngine
*/
class SearchRuleEngine implements RuleEngineInterface
{
private User $user;
private Collection $rules;
private array $operators;
public function __construct()
{
$this->rules = new Collection;
}
/**
* @inheritDoc
*/
public function setUser(User $user): void
{
$this->user = $user;
$this->operators = [];
}
/**
* @inheritDoc
*/
public function setRules(Collection $rules): void
{
foreach ($rules as $rule) {
if ($rule instanceof Rule) {
Log::debug(sprintf('Adding a rule to the SearchRuleEngine: #%d ("%s")', $rule->id, $rule->title));
$this->rules->push($rule);
}
}
}
/**
* @inheritDoc
*/
public function setRuleGroups(Collection $ruleGroups): void
{
die(__METHOD__);
}
/**
* @inheritDoc
*/
public function addOperator(array $operator): void
{
Log::debug('Add operator: ', $operator);
$this->operators[] = $operator;
}
/**
* @inheritDoc
* @throws FireflyException
*/
public function fire(): void
{
Log::debug('SearchRuleEngine::fire()!');
foreach ($this->rules as $rule) {
$this->fireRule($rule);
}
}
/**
* @param Rule $rule
* @throws FireflyException
*/
private function fireRule(Rule $rule): void
{
$searchArray = [];
/** @var RuleTrigger $ruleTrigger */
foreach ($rule->ruleTriggers as $ruleTrigger) {
$searchArray[$ruleTrigger->trigger_type] = sprintf('"%s"', $ruleTrigger->trigger_value);
}
// add local operators:
foreach ($this->operators as $operator) {
$searchArray[$operator['type']] = sprintf('"%s"', $operator['value']);
}
$toJoin = [];
foreach ($searchArray as $type => $value) {
$toJoin[] = sprintf('%s:%s', $type, $value);
}
$searchQuery = join(' ', $toJoin);
Log::debug(sprintf('Search query for rule #%d ("%s") = %s', $rule->id, $rule->title, $searchQuery));
// build and run the search engine.
$searchEngine = app(SearchInterface::class);
$searchEngine->setUser($this->user);
$searchEngine->setPage(1);
$searchEngine->setLimit(1337);
$searchEngine->parseQuery($searchQuery);
$result = $searchEngine->searchTransactions();
$collection = $result->getCollection();
Log::debug(sprintf('Found %d transactions using search engine.', $collection->count()));
$this->processResults($rule, $collection);
}
/**
* @param Rule $rule
* @param Collection $collection
* @throws FireflyException
*/
private function processResults(Rule $rule, Collection $collection): void
{
Log::debug('Going to process results.');
/** @var array $group */
foreach ($collection as $group) {
$this->processTransactionGroup($rule, $group);
}
}
/**
* @param Rule $rule
* @param array $group
* @throws FireflyException
*/
private function processTransactionGroup(Rule $rule, array $group): void
{
Log::debug(sprintf('Will now execute actions on transaction group #%d', $group['id']));
/** @var array $transaction */
foreach ($group['transactions'] as $transaction) {
$this->processTransactionJournal($rule, $transaction);
}
}
/**
* @param Rule $rule
* @param array $transaction
* @throws FireflyException
*/
private function processTransactionJournal(Rule $rule, array $transaction): void
{
Log::debug(sprintf('Will now execute actions on transaction journal #%d', $transaction['transaction_journal_id']));
/** @var RuleAction $ruleAction */
foreach ($rule->ruleActions as $ruleAction) {
$break = $this->processRuleAction($ruleAction, $transaction);
if (true === $break) {
break;
}
}
}
/**
* @param RuleAction $ruleAction
* @param array $transaction
* @return bool
* @throws FireflyException
*/
private function processRuleAction(RuleAction $ruleAction, array $transaction): bool
{
Log::debug(sprintf('Executing rule action "%s" with value "%s"', $ruleAction->action_type, $ruleAction->action_value));
$actionClass = ActionFactory::getAction($ruleAction);
$actionClass->actOnArray($transaction);
if ($ruleAction->stop_processing) {
Log::debug(sprintf('Rule action "%s" asks to break, so break!', $ruleAction->action_value));
return true;
}
return false;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -483,7 +483,7 @@ class OperatorQuerySearchTest extends TestCase
$this->assertCount(0, $object->getWords());
// operator is assumed to be included.
$this->assertCount(1, $object->getOperators());
$this->assertCount(2, $object->getOperators());
$result = ['transactions' => []];
// execute search should work: