mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-20 19:35:16 +00:00
Optimise chart.
This commit is contained in:
@@ -63,12 +63,18 @@ class CategoryController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
$spentArray = $repository->spentPerDay($category, $start, $end);
|
||||||
|
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
||||||
|
|
||||||
|
|
||||||
while ($start <= $end) {
|
while ($start <= $end) {
|
||||||
$currentEnd = Navigation::endOfPeriod($start, $range);
|
$currentEnd = Navigation::endOfPeriod($start, $range);
|
||||||
$spent = $repository->spentInPeriod($category, $start, $currentEnd);
|
|
||||||
$earned = $repository->earnedInPeriod($category, $start, $currentEnd);
|
// get the sum from $spentArray and $earnedArray:
|
||||||
$date = Navigation::periodShow($start, $range);
|
$spent = $this->getSumOfRange($start, $currentEnd, $spentArray);
|
||||||
|
$earned = $this->getSumOfRange($start, $currentEnd, $earnedArray);
|
||||||
|
|
||||||
|
$date = Navigation::periodShow($start, $range);
|
||||||
$entries->push([clone $start, $date, $spent, $earned]);
|
$entries->push([clone $start, $date, $spent, $earned]);
|
||||||
$start = Navigation::addPeriod($start, $range, 0);
|
$start = Navigation::addPeriod($start, $range, 0);
|
||||||
}
|
}
|
||||||
@@ -85,6 +91,7 @@ class CategoryController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show this month's category overview.
|
* Show this month's category overview.
|
||||||
*
|
*
|
||||||
@@ -251,9 +258,9 @@ class CategoryController extends Controller
|
|||||||
$entries = new Collection;
|
$entries = new Collection;
|
||||||
|
|
||||||
// get amount earned in period, grouped by day.
|
// get amount earned in period, grouped by day.
|
||||||
|
// get amount spent in period, grouped by day.
|
||||||
$spentArray = $repository->spentPerDay($category, $start, $end);
|
$spentArray = $repository->spentPerDay($category, $start, $end);
|
||||||
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
||||||
// get amount spent in period, grouped by day.
|
|
||||||
|
|
||||||
while ($start <= $end) {
|
while ($start <= $end) {
|
||||||
$str = $start->format('Y-m-d');
|
$str = $start->format('Y-m-d');
|
||||||
@@ -453,4 +460,32 @@ class CategoryController extends Controller
|
|||||||
return $data;
|
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)
|
public function earnedPerDay(Category $category, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
/** @var Collection $query */
|
/** @var Collection $query */
|
||||||
$query = Auth::user()->transactionJournals()
|
$query = $category->transactionJournals()
|
||||||
->transactionTypes([TransactionType::DEPOSIT])
|
->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')
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
->where('transactions.amount', '>', 0)
|
->where('transactions.amount', '>', 0)
|
||||||
->before($end)
|
->before($end)
|
||||||
->after($start)
|
->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`')]);
|
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||||
|
|
||||||
$return = [];
|
$return = [];
|
||||||
@@ -245,14 +243,12 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
|||||||
public function spentPerDay(Category $category, Carbon $start, Carbon $end)
|
public function spentPerDay(Category $category, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
/** @var Collection $query */
|
/** @var Collection $query */
|
||||||
$query = Auth::user()->transactionJournals()
|
$query = $category->transactionJournals()
|
||||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
->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')
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
->where('transactions.amount', '<', 0)
|
->where('transactions.amount', '<', 0)
|
||||||
->before($end)
|
->before($end)
|
||||||
->after($start)
|
->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`')]);
|
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||||
|
|
||||||
$return = [];
|
$return = [];
|
||||||
|
Reference in New Issue
Block a user