Fewer loops.

This commit is contained in:
James Cole
2025-06-26 06:40:00 +02:00
parent 4607466fb6
commit 5706666bb6

View File

@@ -70,7 +70,7 @@ trait PeriodOverview
protected JournalRepositoryInterface $journalRepos;
/**
* This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range)
* This method returns "period entries", so nov-2015, dec-2015, etc. (this depends on the users session range)
* and for each period, the amount of money spent and earned. This is a complex operation which is cached for
* performance reasons.
*
@@ -97,18 +97,26 @@ trait PeriodOverview
/** @var array $dates */
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
$spent= [];
$earned = [];
$transferredAway = [];
$transferredIn = [];
// run a custom query because doing this with the collector is MEGA slow.
$transactions = $this->accountRepository->periodCollection($account, $start, $end);
// loop dates
Log::debug(sprintf('Count of loops: %d', count($dates)));
$loops = 0;
// stop after 10 loops for memory reasons.
foreach ($dates as $currentDate) {
$title = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
if($loops < 10) {
[$transactions, $spent] = $this->filterTransactionsByType(TransactionTypeEnum::WITHDRAWAL, $transactions, $currentDate['start'], $currentDate['end']);
[$transactions, $earned] = $this->filterTransactionsByType(TransactionTypeEnum::DEPOSIT, $transactions, $currentDate['start'], $currentDate['end']);
[$transactions, $transferredAway] = $this->filterTransfers('away', $transactions, $currentDate['start'], $currentDate['end']);
[$transactions, $transferredIn] = $this->filterTransfers('in', $transactions, $currentDate['start'], $currentDate['end']);
}
$entries[]
= [
'title' => $title,
@@ -119,6 +127,7 @@ trait PeriodOverview
'transferred_away' => $this->groupByCurrency($transferredAway),
'transferred_in' => $this->groupByCurrency($transferredIn),
];
$loops++;
}
$cache->store($entries);
Timer::stop('account-period-total');
@@ -548,18 +557,19 @@ trait PeriodOverview
/** @var array $dates */
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = [];
$spent = [];
$earned = [];
$transferred = [];
// collect all journals in this period (regardless of type)
$collector = app(GroupCollectorInterface::class);
$collector->setTypes($types)->setRange($start, $end);
$genericSet = $collector->getExtractedJournals();
$loops = 0;
foreach ($dates as $currentDate) {
$spent = [];
$earned = [];
$transferred = [];
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
if($loops < 10) {
// set to correct array
if ('expenses' === $transactionType || 'withdrawal' === $transactionType) {
$spent = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
@@ -570,6 +580,7 @@ trait PeriodOverview
if ('transfer' === $transactionType || 'transfers' === $transactionType) {
$transferred = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
}
}
$entries[]
= [
'title' => $title,
@@ -579,6 +590,7 @@ trait PeriodOverview
'earned' => $this->groupByCurrency($earned),
'transferred' => $this->groupByCurrency($transferred),
];
$loops++;
}
return $entries;