A lot less queries thanks to efficient query.

This commit is contained in:
James Cole
2015-12-27 08:39:29 +01:00
parent dd42d8437c
commit 5f8b6640a9
2 changed files with 62 additions and 9 deletions

View File

@@ -3,10 +3,8 @@
namespace FireflyIII\Generator\Chart\Account; namespace FireflyIII\Generator\Chart\Account;
use Carbon\Carbon; use Carbon\Carbon;
use Config;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Preferences;
use Steam; use Steam;
/** /**
@@ -90,20 +88,21 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
public function frontpage(Collection $accounts, Carbon $start, Carbon $end) public function frontpage(Collection $accounts, Carbon $start, Carbon $end)
{ {
// language: // language:
$format = trans('config.month_and_day'); $format = trans('config.month_and_day');
$data = [ $data = [
'count' => 0, 'count' => 0,
'labels' => [], 'labels' => [],
'datasets' => [], 'datasets' => [],
]; ];
$current = clone $start; $current = clone $start;
while ($current <= $end) { while ($current <= $end) {
$data['labels'][] = $current->formatLocalized($format); $data['labels'][] = $current->formatLocalized($format);
$current->addDay(); $current->addDay();
} }
foreach ($accounts as $account) { foreach ($accounts as $account) {
$set = [ $set = [
'label' => $account->name, 'label' => $account->name,
'fillColor' => 'rgba(220,220,220,0.2)', 'fillColor' => 'rgba(220,220,220,0.2)',
'strokeColor' => 'rgba(220,220,220,1)', 'strokeColor' => 'rgba(220,220,220,1)',
@@ -113,9 +112,15 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
'pointHighlightStroke' => 'rgba(220,220,220,1)', 'pointHighlightStroke' => 'rgba(220,220,220,1)',
'data' => [], 'data' => [],
]; ];
$current = clone $start; $current = clone $start;
$range = Steam::balanceInRange($account, $start, $end);
$previous = array_values($range)[0];
while ($current <= $end) { while ($current <= $end) {
$set['data'][] = Steam::balance($account, $current); $format = $current->format('Y-m-d');
$balance = isset($range[$format]) ? $range[$format] : $previous;
$set['data'][] = $balance;
$previous = $balance;
$current->addDay(); $current->addDay();
} }
$data['datasets'][] = $set; $data['datasets'][] = $set;
@@ -135,7 +140,7 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator
public function single(Account $account, Carbon $start, Carbon $end) public function single(Account $account, Carbon $start, Carbon $end)
{ {
// language: // language:
$format = trans('config.month_and_day'); $format = trans('config.month_and_day');
$data = [ $data = [
'count' => 1, 'count' => 1,

View File

@@ -72,6 +72,54 @@ class Steam
return round($balance, 2); return round($balance, 2);
} }
/**
* Gets the balance for the given account during the whole range, using this format:
*
* [yyyy-mm-dd] => 123,2
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*/
public function balanceInRange(Account $account, Carbon $start, Carbon $end)
{
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty('balance-in-range');
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$balances = [];
$start->subDay();
$end->addDay();
$startBalance = $this->balance($account, $start);
$balances[$start->format('Y-m-d')] = $startBalance;
$start->addDay();
// query!
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->groupBy('transaction_journals.date')
->get(['transaction_journals.date', DB::Raw('SUM(`transactions`.`amount`) as `modified`')]);
$currentBalance = $startBalance;
foreach ($set as $entry) {
$currentBalance = bcadd($currentBalance, $entry->modified);
$balances[$entry->date] = $currentBalance;
}
$cache->store($balances);
return $balances;
}
/** /**
* *
* @param array $ids * @param array $ids