mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-26 02:40:43 -06:00
Optimise chart.
This commit is contained in:
parent
d75614e9a7
commit
1b3592d959
@ -63,12 +63,18 @@ class CategoryController extends Controller
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
$spentArray = $repository->spentPerDay($category, $start, $end);
|
||||
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
||||
|
||||
|
||||
while ($start <= $end) {
|
||||
$currentEnd = Navigation::endOfPeriod($start, $range);
|
||||
$spent = $repository->spentInPeriod($category, $start, $currentEnd);
|
||||
$earned = $repository->earnedInPeriod($category, $start, $currentEnd);
|
||||
$date = Navigation::periodShow($start, $range);
|
||||
|
||||
// get the sum from $spentArray and $earnedArray:
|
||||
$spent = $this->getSumOfRange($start, $currentEnd, $spentArray);
|
||||
$earned = $this->getSumOfRange($start, $currentEnd, $earnedArray);
|
||||
|
||||
$date = Navigation::periodShow($start, $range);
|
||||
$entries->push([clone $start, $date, $spent, $earned]);
|
||||
$start = Navigation::addPeriod($start, $range, 0);
|
||||
}
|
||||
@ -85,6 +91,7 @@ class CategoryController extends Controller
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show this month's category overview.
|
||||
*
|
||||
@ -251,9 +258,9 @@ class CategoryController extends Controller
|
||||
$entries = new Collection;
|
||||
|
||||
// get amount earned in period, grouped by day.
|
||||
// get amount spent in period, grouped by day.
|
||||
$spentArray = $repository->spentPerDay($category, $start, $end);
|
||||
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
||||
// get amount spent in period, grouped by day.
|
||||
|
||||
while ($start <= $end) {
|
||||
$str = $start->format('Y-m-d');
|
||||
@ -453,4 +460,32 @@ class CategoryController extends Controller
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the array as returned by SingleCategoryRepositoryInterface::spentPerDay and SingleCategoryRepositoryInterface::earnedByDay
|
||||
* and sum up everything in the array in the given range.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
|
||||
{
|
||||
bcscale(2);
|
||||
$sum = '0';
|
||||
$currentStart = clone $start; // to not mess with the original one
|
||||
$currentEnd = clone $end; // to not mess with the original one
|
||||
|
||||
while ($currentStart <= $currentEnd) {
|
||||
$date = $currentStart->format('Y-m-d');
|
||||
if (isset($array[$date])) {
|
||||
$sum = bcadd($sum, $array[$date]);
|
||||
}
|
||||
$currentStart->addDay();
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -107,14 +107,12 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
||||
public function earnedPerDay(Category $category, Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $query */
|
||||
$query = Auth::user()->transactionJournals()
|
||||
$query = $category->transactionJournals()
|
||||
->transactionTypes([TransactionType::DEPOSIT])
|
||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.amount', '>', 0)
|
||||
->before($end)
|
||||
->after($start)
|
||||
->where('category_transaction_journal.category_id', $category->id)
|
||||
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
|
||||
$return = [];
|
||||
@ -245,14 +243,12 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
||||
public function spentPerDay(Category $category, Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $query */
|
||||
$query = Auth::user()->transactionJournals()
|
||||
$query = $category->transactionJournals()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.amount', '<', 0)
|
||||
->before($end)
|
||||
->after($start)
|
||||
->where('category_transaction_journal.category_id', $category->id)
|
||||
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
|
||||
$return = [];
|
||||
|
Loading…
Reference in New Issue
Block a user