Use a lot less queries

This commit is contained in:
James Cole 2015-12-31 17:20:54 +01:00
parent f98921da46
commit a6594358d8
5 changed files with 58 additions and 49 deletions

View File

@ -56,8 +56,8 @@ class AccountController extends Controller
}
/**
* @param ARI $repository
* @param Account $account
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\View\View
*/
@ -77,8 +77,8 @@ class AccountController extends Controller
}
/**
* @param ARI $repository
* @param Account $account
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
@ -98,8 +98,8 @@ class AccountController extends Controller
}
/**
* @param ARI $repository
* @param Account $account
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\View\View
*/
@ -143,7 +143,7 @@ class AccountController extends Controller
}
/**
* @param ARI $repository
* @param ARI $repository
* @param $what
*
* @return \Illuminate\View\View
@ -184,8 +184,8 @@ class AccountController extends Controller
}
/**
* @param ARI $repository
* @param Account $account
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\View\View
*/
@ -195,7 +195,7 @@ class AccountController extends Controller
$subTitleIcon = Config::get('firefly.subTitlesByIdentifier.' . $account->accountType->type);
$what = Config::get('firefly.shortNamesByFullName.' . $account->accountType->type);
$journals = $repository->getJournals($account, $page);
$subTitle = trans('firefly.details_for_' . $what, ['name' => $account->name]);
$subTitle = trans('firefly.details_for_' . $what, ['name' => $account->name]);
$journals->setPath('accounts/show/' . $account->id);
@ -203,8 +203,8 @@ class AccountController extends Controller
}
/**
* @param AccountFormRequest $request
* @param ARI $repository
* @param AccountFormRequest $request
* @param ARI $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
@ -242,9 +242,9 @@ class AccountController extends Controller
}
/**
* @param AccountFormRequest $request
* @param ARI $repository
* @param Account $account
* @param AccountFormRequest $request
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/

View File

@ -202,6 +202,12 @@ class CategoryController extends Controller
$cache->addProperty($end);
$cache->addProperty('category-show');
$cache->addProperty($category->id);
// get all spent and earned data:
// get amount earned in period, grouped by day.
$spentArray = $repository->spentPerDay($category, $start, $end);
$earnedArray = $repository->earnedPerDay($category, $start, $end);
if ($cache->has()) {
$entries = $cache->get();
} else {
@ -210,9 +216,9 @@ class CategoryController extends Controller
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
// here do something.
$spent = $repository->spentInPeriod($category, $end, $currentEnd);
$earned = $repository->earnedInPeriod($category, $end, $currentEnd);
// get data from spentArray:
$spent = $this->getSumOfRange($end, $currentEnd, $spentArray);
$earned = $this->getSumOfRange($end, $currentEnd, $earnedArray);
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
$entries->push([$dateStr, $dateName, $spent, $earned]);

View File

@ -460,32 +460,6 @@ 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;
}
}

View File

@ -1,6 +1,7 @@
<?php namespace FireflyIII\Http\Controllers;
use Auth;
use Carbon\Carbon;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
@ -44,4 +45,32 @@ abstract class Controller extends BaseController
View::share('localeconv', localeconv());
}
}
/**
* 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;
}
}

View File

@ -100,11 +100,11 @@ class Navigation
/**
*
* @param Carbon $theCurrentEnd
* @param \Carbon\Carbon $theCurrentEnd
* @param $repeatFreq
* @param Carbon $maxDate
* @param \Carbon\Carbon $maxDate
*
* @return Carbon
* @return \Carbon\Carbon
*/
public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate = null)
{
@ -218,11 +218,11 @@ class Navigation
}
/**
* @param Carbon $theDate
* @param \Carbon\Carbon $theDate
* @param $repeatFreq
* @param int $subtract
*
* @return Carbon
* @return \Carbon\Carbon
* @throws FireflyException
*/
public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1)