Optimize memory usage.

This commit is contained in:
James Cole
2024-12-22 06:55:07 +01:00
parent 0e2e155cc6
commit 9240b9868b

View File

@@ -34,6 +34,7 @@ use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\RecurrenceTransaction; use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\RuleTrigger;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/** /**
* Class ReportSkeleton * Class ReportSkeleton
@@ -69,135 +70,89 @@ class CorrectAmounts extends Command
private function fixAutoBudgets(): void private function fixAutoBudgets(): void
{ {
$set = AutoBudget::where('amount', '<', 0)->get(); $count = AutoBudget::where('amount', '<', 0)->update(['amount' => DB::raw('amount * -1')]);
$count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All auto budget amounts are positive.'); $this->friendlyPositive('All auto budget amounts are positive.');
return; return;
} }
/** @var AutoBudget $item */
foreach ($set as $item) {
$item->amount = app('steam')->positive($item->amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d auto budget amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d auto budget amount(s).', $count));
} }
private function fixAvailableBudgets(): void private function fixAvailableBudgets(): void
{ {
$set = AvailableBudget::where('amount', '<', 0)->get(); $count = AvailableBudget::where('amount', '<', 0)->update(['amount' => DB::raw('amount * -1')]);
$count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All available budget amounts are positive.'); $this->friendlyPositive('All available budget amounts are positive.');
return; return;
} }
/** @var AvailableBudget $item */
foreach ($set as $item) {
$item->amount = app('steam')->positive($item->amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d available budget amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d available budget amount(s).', $count));
} }
private function fixBills(): void private function fixBills(): void
{ {
$set = Bill::where('amount_min', '<', 0)->orWhere('amount_max', '<', 0)->get(); $count = 0;
$count = $set->count(); $count += Bill::where('amount_max', '<', 0)->update(['amount_max' => DB::raw('amount_max * -1')]);
$count += Bill::where('amount_min', '<', 0)->update(['amount_min' => DB::raw('amount_min * -1')]);
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All bill amounts are positive.'); $this->friendlyPositive('All bill amounts are positive.');
return; return;
} }
/** @var Bill $item */
foreach ($set as $item) {
$item->amount_min = app('steam')->positive($item->amount_min);
$item->amount_max = app('steam')->positive($item->amount_max);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d bill amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d bill amount(s).', $count));
} }
private function fixBudgetLimits(): void private function fixBudgetLimits(): void
{ {
$set = BudgetLimit::where('amount', '<', 0)->get(); $count = BudgetLimit::where('amount', '<', 0)->update(['amount' => DB::raw('amount * -1')]);
$count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All budget limit amounts are positive.'); $this->friendlyPositive('All budget limit amounts are positive.');
return; return;
} }
/** @var BudgetLimit $item */
foreach ($set as $item) {
$item->amount = app('steam')->positive($item->amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d budget limit amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d budget limit amount(s).', $count));
} }
private function fixExchangeRates(): void private function fixExchangeRates(): void
{ {
$set = CurrencyExchangeRate::where('rate', '<', 0)->get(); $count = CurrencyExchangeRate::where('rate', '<', 0)->update(['rate' => DB::raw('rate * -1')]);
$count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All currency exchange rates are positive.'); $this->friendlyPositive('All currency exchange rates are positive.');
return; return;
} }
/** @var CurrencyExchangeRate $item */
foreach ($set as $item) {
$item->rate = app('steam')->positive($item->rate);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d currency exchange rate(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d currency exchange rate(s).', $count));
} }
private function fixPiggyBanks(): void private function fixPiggyBanks(): void
{ {
$set = PiggyBank::where('target_amount', '<', 0)->get(); $count = PiggyBank::where('target_amount', '<', 0)->update(['target_amount' => DB::raw('target_amount * -1')]);
$count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All piggy bank amounts are positive.'); $this->friendlyPositive('All piggy bank amounts are positive.');
return; return;
} }
/** @var PiggyBank $item */
foreach ($set as $item) {
$item->target_amount = app('steam')->positive($item->target_amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d piggy bank amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d piggy bank amount(s).', $count));
} }
private function fixRecurrences(): void private function fixRecurrences(): void
{ {
$set = RecurrenceTransaction::where('amount', '<', 0) $count = 0;
->orWhere('foreign_amount', '<', 0) $count += RecurrenceTransaction::where('amount', '<', 0)->update(['amount' => DB::raw('amount * -1')]);
->get() $count += RecurrenceTransaction::where('foreign_amount', '<', 0)->update(['foreign_amount' => DB::raw('foreign_amount * -1')]);
;
$count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->friendlyPositive('All recurring transaction amounts are positive.'); $this->friendlyPositive('All recurring transaction amounts are positive.');
return; return;
} }
/** @var RecurrenceTransaction $item */
foreach ($set as $item) {
$item->amount = app('steam')->positive($item->amount);
$item->foreign_amount = app('steam')->positive($item->foreign_amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d recurring transaction amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d recurring transaction amount(s).', $count));
} }
/**
* Foreach loop is unavoidable here.
* @return void
*/
private function fixRuleTriggers(): void private function fixRuleTriggers(): void
{ {
$set = RuleTrigger::whereIn('trigger_type', ['amount_less', 'amount_more', 'amount_is'])->get(); $set = RuleTrigger::whereIn('trigger_type', ['amount_less', 'amount_more', 'amount_is'])->get();
@@ -205,21 +160,9 @@ class CorrectAmounts extends Command
/** @var RuleTrigger $item */ /** @var RuleTrigger $item */
foreach ($set as $item) { foreach ($set as $item) {
// basic check: $result = $this->fixRuleTrigger($item);
$check = 0; if (true === $result) {
$fixed++;
try {
$check = bccomp((string)$item->trigger_value, '0');
} catch (\ValueError $e) {
$this->friendlyError(sprintf('Rule #%d contained invalid %s-trigger "%s". The trigger has been removed, and the rule is disabled.', $item->rule_id, $item->trigger_type, $item->trigger_value));
$item->rule->active = false;
$item->rule->save();
$item->forceDelete();
}
if (-1 === $check) {
++$fixed;
$item->trigger_value = app('steam')->positive($item->trigger_value);
$item->save();
} }
} }
if (0 === $fixed) { if (0 === $fixed) {
@@ -229,4 +172,23 @@ class CorrectAmounts extends Command
} }
$this->friendlyInfo(sprintf('Corrected %d rule trigger amount(s).', $fixed)); $this->friendlyInfo(sprintf('Corrected %d rule trigger amount(s).', $fixed));
} }
private function fixRuleTrigger(RuleTrigger $item): bool
{
try {
$check = bccomp((string) $item->trigger_value, '0');
} catch (\ValueError $e) {
$this->friendlyError(sprintf('Rule #%d contained invalid %s-trigger "%s". The trigger has been removed, and the rule is disabled.', $item->rule_id, $item->trigger_type, $item->trigger_value));
$item->rule->active = false;
$item->rule->save();
$item->forceDelete();
return false;
}
if (-1 === $check) {
$item->trigger_value = app('steam')->positive($item->trigger_value);
$item->save();
return true;
}
return false;
}
} }