mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Use a lot less queries
This commit is contained in:
parent
f98921da46
commit
a6594358d8
@ -202,6 +202,12 @@ class CategoryController extends Controller
|
|||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
$cache->addProperty('category-show');
|
$cache->addProperty('category-show');
|
||||||
$cache->addProperty($category->id);
|
$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()) {
|
if ($cache->has()) {
|
||||||
$entries = $cache->get();
|
$entries = $cache->get();
|
||||||
} else {
|
} else {
|
||||||
@ -210,9 +216,9 @@ class CategoryController extends Controller
|
|||||||
$end = Navigation::startOfPeriod($end, $range);
|
$end = Navigation::startOfPeriod($end, $range);
|
||||||
$currentEnd = Navigation::endOfPeriod($end, $range);
|
$currentEnd = Navigation::endOfPeriod($end, $range);
|
||||||
|
|
||||||
// here do something.
|
// get data from spentArray:
|
||||||
$spent = $repository->spentInPeriod($category, $end, $currentEnd);
|
$spent = $this->getSumOfRange($end, $currentEnd, $spentArray);
|
||||||
$earned = $repository->earnedInPeriod($category, $end, $currentEnd);
|
$earned = $this->getSumOfRange($end, $currentEnd, $earnedArray);
|
||||||
$dateStr = $end->format('Y-m-d');
|
$dateStr = $end->format('Y-m-d');
|
||||||
$dateName = Navigation::periodShow($end, $range);
|
$dateName = Navigation::periodShow($end, $range);
|
||||||
$entries->push([$dateStr, $dateName, $spent, $earned]);
|
$entries->push([$dateStr, $dateName, $spent, $earned]);
|
||||||
|
@ -460,32 +460,6 @@ 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php namespace FireflyIII\Http\Controllers;
|
<?php namespace FireflyIII\Http\Controllers;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||||
use Illuminate\Routing\Controller as BaseController;
|
use Illuminate\Routing\Controller as BaseController;
|
||||||
@ -44,4 +45,32 @@ abstract class Controller extends BaseController
|
|||||||
View::share('localeconv', localeconv());
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,11 +100,11 @@ class Navigation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param Carbon $theCurrentEnd
|
* @param \Carbon\Carbon $theCurrentEnd
|
||||||
* @param $repeatFreq
|
* @param $repeatFreq
|
||||||
* @param Carbon $maxDate
|
* @param \Carbon\Carbon $maxDate
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return \Carbon\Carbon
|
||||||
*/
|
*/
|
||||||
public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate = null)
|
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 $repeatFreq
|
||||||
* @param int $subtract
|
* @param int $subtract
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return \Carbon\Carbon
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1)
|
public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user