mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-11 15:16:27 +00:00
Optimize query for period statistics.
This commit is contained in:
@@ -27,32 +27,31 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Models\PeriodStatistic;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface
|
||||
{
|
||||
public function findPeriodStatistics(Model $model, Carbon $start, Carbon $end, array $types): Collection
|
||||
{
|
||||
return $model->primaryPeriodStatistics()
|
||||
->where('start', $start)
|
||||
->where('end', $end)
|
||||
->whereIn('type', $types)
|
||||
->get()
|
||||
;
|
||||
->where('start', $start)
|
||||
->where('end', $end)
|
||||
->whereIn('type', $types)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function findPeriodStatistic(Model $model, Carbon $start, Carbon $end, string $type): Collection
|
||||
{
|
||||
return $model->primaryPeriodStatistics()
|
||||
->where('start', $start)
|
||||
->where('end', $end)
|
||||
->where('type', $type)
|
||||
->get()
|
||||
;
|
||||
->where('start', $start)
|
||||
->where('end', $end)
|
||||
->where('type', $type)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function saveStatistic(Model $model, int $currencyId, Carbon $start, Carbon $end, string $type, int $count, string $amount): PeriodStatistic
|
||||
{
|
||||
$stat = new PeriodStatistic();
|
||||
$stat = new PeriodStatistic();
|
||||
$stat->primaryStatable()->associate($model);
|
||||
$stat->transaction_currency_id = $currencyId;
|
||||
$stat->start = $start;
|
||||
@@ -64,11 +63,15 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface
|
||||
$stat->type = $type;
|
||||
$stat->save();
|
||||
|
||||
Log::debug(sprintf('Saved #%d [currency #%d, Model %s #%d, %s to %s, %d, %s] as new statistic.',
|
||||
$stat->id, get_class($model), $model->id, $stat->transaction_currency_id, $stat->start->toW3cString(), $stat->end->toW3cString(), $count, $amount
|
||||
));
|
||||
|
||||
return $stat;
|
||||
}
|
||||
|
||||
public function allInRangeForModel(Model $model, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
return $model->primaryPeriodStatistics()->where('start','>=', $start)->where('end','<=', $end)->get();
|
||||
return $model->primaryPeriodStatistics()->where('start', '>=', $start)->where('end', '<=', $end)->get();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user