Cleared lots of queries. In some cases, from 1400 back to 300. And those 300 have a different cause which is next.

This commit is contained in:
James Cole
2015-12-31 17:46:34 +01:00
parent a6594358d8
commit 3dcdacc3b8
4 changed files with 159 additions and 44 deletions

View File

@@ -8,6 +8,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
use Input; use Input;
use Navigation; use Navigation;
use Preferences; use Preferences;
@@ -132,9 +133,9 @@ class BudgetController extends Controller
} }
/** /**
* @param BudgetRepositoryInterface $repository * @param BudgetRepositoryInterface $repository
* *
* @param ARI $accountRepository * @param ARI $accountRepository
* *
* @return \Illuminate\View\View * @return \Illuminate\View\View
*/ */
@@ -232,13 +233,38 @@ class BudgetController extends Controller
$journals = $repository->getJournals($budget, $repetition); $journals = $repository->getJournals($budget, $repetition);
if (is_null($repetition->id)) { if (is_null($repetition->id)) {
$limits = $repository->getBudgetLimits($budget); $start = $repository->firstActivity($budget);
$subTitle = e($budget->name); $end = new Carbon;
$set = $budget->limitrepetitions()->orderBy('startdate','DESC')->get();
//$set = $repository->getBudgetLimits($budget);
//$subTitle = e($budget->name);
} else { } else {
$limits = [$repetition->budgetLimit]; $start = $repetition->startdate;
$subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]); $end = $repetition->enddate;
$set = new Collection([$repetition]);
// $spentArray = $repository->spentPerDay($budget, $start, $end);
// $budgetLimit = $repetition->budgetLimit;
// $budgetLimit->spent = $this->getSumOfRange($start, $end, $spentArray); // bit weird but it works.
// $limits = new Collection([$budgetLimit]);
//$subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]);
} }
$spentArray = $repository->spentPerDay($budget, $start, $end);
$limits = new Collection();
/** @var LimitRepetition $entry */
foreach ($set as $entry) {
$entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray);
$limits->push($entry);
}
// loop limits and set the amount spent for each limit.
// /** @var BudgetLimit $budgetLimit */
// foreach ($set as $budgetLimit) {
// $start = $budgetLimit->startdate;
// $end = $budgetLimit->lim
// }
$journals->setPath('/budgets/show/' . $budget->id); $journals->setPath('/budgets/show/' . $budget->id);
return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));

View File

@@ -5,7 +5,6 @@ namespace FireflyIII\Repositories\Budget;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use DB; use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
@@ -185,6 +184,33 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
} }
/**
* @param Budget $budget
*
* @return Carbon
*/
public function firstActivity(Budget $budget)
{
$first = $budget->transactionjournals()->orderBy('date', 'ASC')->first();
if ($first) {
return $first->date;
}
return new Carbon;
}
/**
* Get a collection of all the limit repetitions belonging to this $budget.
*
* @param Budget $budget
*
* @return Collection
*/
public function getBudgetReps(Budget $budget) {
$set = $budget->limitrepetitions()->count();
var_dump($set);
}
/** /**
* @param Budget $budget * @param Budget $budget
* @param Carbon $start * @param Carbon $start
@@ -213,6 +239,41 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
return $budget->budgetLimits()->orderBy('startdate', 'DESC')->get(); return $budget->budgetLimits()->orderBy('startdate', 'DESC')->get();
} }
/**
* Returns an array with the following key:value pairs:
*
* yyyy-mm-dd:<amount>
*
* Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $budget
* from all the users accounts.
*
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end)
{
/** @var Collection $query */
$query = $budget->transactionJournals()
->transactionTypes([TransactionType::WITHDRAWAL])
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.amount', '<', 0)
->before($end)
->after($start)
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
$return = [];
foreach ($query->toArray() as $entry) {
$return[$entry['dateFormatted']] = $entry['sum'];
}
return $return;
}
/** /**
* @return Collection * @return Collection
*/ */
@@ -617,7 +678,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
*/ */
public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end)
{ {
$ids = $accounts->pluck('id')->toArray(); $ids = $accounts->pluck('id')->toArray();
$budgetIds = $budgets->pluck('id')->toArray(); $budgetIds = $budgets->pluck('id')->toArray();
/** @var Collection $set */ /** @var Collection $set */

View File

@@ -32,6 +32,22 @@ interface BudgetRepositoryInterface
*/ */
public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end); public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end);
/**
* @param Budget $budget
*
* @return Carbon
*/
public function firstActivity(Budget $budget);
/**
* Get a collection of all the limit repetitions belonging to this $budget.
*
* @param Budget $budget
*
* @return Collection
*/
public function getBudgetReps(Budget $budget);
/** /**
* Returns the expenses for this budget grouped per month, with the date * Returns the expenses for this budget grouped per month, with the date
* in "date" (a string, not a Carbon) and the amount in "dailyAmount". * in "date" (a string, not a Carbon) and the amount in "dailyAmount".
@@ -44,6 +60,22 @@ interface BudgetRepositoryInterface
*/ */
public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end); public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end);
/**
* Returns an array with the following key:value pairs:
*
* yyyy-mm-dd:<amount>
*
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget
* from all the users accounts.
*
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end);
/** /**
* @param Budget $budget * @param Budget $budget
* *

View File

@@ -44,47 +44,43 @@
{% endif %} {% endif %}
{% for limit in limits %} {% for limit in limits %}
{% for rep in limit.limitRepetitions %} <div class="box">
{% set spentInRep = spentInRepetition(rep) %} <div class="box-header with-border">
<div class="box"> <h3 class="box-title"><a href="{{ route('budgets.show',[budget.id,limit.id]) }}">{{ limit.startdate.formatLocalized(monthFormat) }}</a>
<div class="box-header with-border"> </h3>
<h3 class="box-title"><a href="{{ route('budgets.show',[budget.id,rep.id]) }}">{{ rep.startdate.formatLocalized(monthFormat) }}</a> </div>
</h3> <div class="box-body">
</div> <div class="row">
<div class="box-body"> <div class="col-lg-6 col-md-6 col-sm-6">
<div class="row"> {{ 'amount'|_ }}: {{ limit.amount|formatAmount }}
<div class="col-lg-6 col-md-6 col-sm-6">
{{ 'amount'|_ }}: {{ rep.amount|formatAmount }}
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
{{ 'spent'|_ }}: {{ spentInRep|formatAmount }}
</div>
</div> </div>
<div class="row"> <div class="col-lg-6 col-md-6 col-sm-6">
<div class="col-lg-12 col-md-12 col-sm-12"> {{ 'spent'|_ }}: {{ limit.spent|formatAmount }}
{% set overspent = rep.amount + spentInRep < 0 %} </div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
{% set overspent = limit.amount + limit.spent < 0 %}
{% if overspent %} {% if overspent %}
{% set pct = (spentInRep != 0 ? (rep.amount / (spentInRep*-1))*100 : 0) %} <!-- must have -1 here --> {% set pct = (limit.spent != 0 ? (limit.amount / (limit.spent*-1))*100 : 0) %} <!-- must have -1 here -->
<div class="progress progress-striped"> <div class="progress progress-striped">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0" <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0"
aria-valuemax="100" style="width: {{ pct|round }}%;"></div> aria-valuemax="100" style="width: {{ pct|round }}%;"></div>
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="{{ (100-pct)|round }}" <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="{{ (100-pct)|round }}"
aria-valuemin="0" aria-valuemax="100" style="width: {{ (100-pct)|round }}%;"></div> aria-valuemin="0" aria-valuemax="100" style="width: {{ (100-pct)|round }}%;"></div>
</div> </div>
{% else %} {% else %}
{% set amount = rep.amount %} {% set pct = (limit.amount != 0 ? (((limit.spent*-1) / limit.amount)*100) : 0) %} <!-- must have -1 here -->
{% set pct = (amount != 0 ? (((spentInRep*-1) / amount)*100) : 0) %} <!-- must have -1 here --> <div class="progress progress-striped">
<div class="progress progress-striped"> <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0"
<div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="{{ pct|round }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ pct|round }}%;"></div>
aria-valuemax="100" style="width: {{ pct|round }}%;"></div> </div>
</div> {% endif %}
{% endif %}
</div>
</div> </div>
</div> </div>
</div> </div>
{% endfor %} </div>
{% endfor %} {% endfor %}
{% if limits|length == 1 %} {% if limits|length == 1 %}