From ad1d15ddf5dc8247ba182bb1aef1b5efe8857b95 Mon Sep 17 00:00:00 2001 From: Alfonso Bribiesca Date: Thu, 23 Sep 2021 14:06:31 -0500 Subject: [PATCH] refactor: optimize cache-transactions command (#932) --- .../Aggregates/Historical/AllAggregate.php | 15 ++++++++++----- .../Aggregates/Historical/DayAggregate.php | 2 +- .../Aggregates/Historical/MonthAggregate.php | 2 +- .../Aggregates/Historical/QuarterAggregate.php | 2 +- .../Aggregates/Historical/RangeAggregate.php | 17 ++++++++++++----- .../Aggregates/Historical/WeekAggregate.php | 2 +- .../Aggregates/Historical/YearAggregate.php | 2 +- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/Services/Transactions/Aggregates/Historical/AllAggregate.php b/app/Services/Transactions/Aggregates/Historical/AllAggregate.php index e293809764..a3ada6ae7a 100644 --- a/app/Services/Transactions/Aggregates/Historical/AllAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/AllAggregate.php @@ -13,11 +13,16 @@ final class AllAggregate { public function aggregate(): Collection { + $select = [ + 'MAX(timestamp) as timestamp', + 'COUNT(*) as total', + sprintf("to_char(to_timestamp(%d+timestamp) AT TIME ZONE 'UTC', '%s') as formatted_date", Network::epoch()->timestamp, 'YYYY-MM'), + ]; + return Transaction::query() - ->select(DB::raw('COUNT(*) as transactions, to_char(to_timestamp(timestamp+'.Network::epoch()->timestamp."), 'YYYY-MM') as month")) - ->groupBy('month') - ->orderBy('month') - ->pluck('transactions', 'month') - ->mapWithKeys(fn ($transactions, $month) => [$month => $transactions]); + ->select(DB::raw(implode(', ', $select))) + ->orderBy('formatted_date') + ->groupBy('formatted_date') + ->pluck('total', 'formatted_date'); } } diff --git a/app/Services/Transactions/Aggregates/Historical/DayAggregate.php b/app/Services/Transactions/Aggregates/Historical/DayAggregate.php index 90748c2591..4936ef0cbf 100644 --- a/app/Services/Transactions/Aggregates/Historical/DayAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/DayAggregate.php @@ -17,7 +17,7 @@ final class DayAggregate public function aggregate(): Collection { return $this->mergeWithPlaceholders( - (new RangeAggregate())->aggregate(Carbon::now()->subDay()->addHour(), Carbon::now(), 'H'), + (new RangeAggregate())->aggregate(Carbon::now()->subDay()->addHour(), Carbon::now(), 'HH24'), $this->placeholders((int) Carbon::now()->subDay()->addHour()->timestamp, (int) Carbon::now()->timestamp, 3600, 'H')->take(24) ); } diff --git a/app/Services/Transactions/Aggregates/Historical/MonthAggregate.php b/app/Services/Transactions/Aggregates/Historical/MonthAggregate.php index ff578aa779..40be0d84ad 100644 --- a/app/Services/Transactions/Aggregates/Historical/MonthAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/MonthAggregate.php @@ -17,7 +17,7 @@ final class MonthAggregate public function aggregate(): Collection { return $this->mergeWithPlaceholders( - (new RangeAggregate())->aggregate(Carbon::now()->subDays(29), Carbon::now()->addDay(), 'd.m'), + (new RangeAggregate())->aggregate(Carbon::now()->subDays(29), Carbon::now()->addDay(), 'DD.MM'), $this->placeholders((int) Carbon::now()->subDays(29)->timestamp, (int) Carbon::now()->addDay()->timestamp, 86400, 'd.m')->take(30) ); } diff --git a/app/Services/Transactions/Aggregates/Historical/QuarterAggregate.php b/app/Services/Transactions/Aggregates/Historical/QuarterAggregate.php index c9d0e3602c..0e2dc99f77 100644 --- a/app/Services/Transactions/Aggregates/Historical/QuarterAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/QuarterAggregate.php @@ -17,7 +17,7 @@ final class QuarterAggregate public function aggregate(): Collection { return $this->mergeWithPlaceholders( - (new RangeAggregate())->aggregate(Carbon::now()->subDays(89), Carbon::now()->addDay(), 'M'), + (new RangeAggregate())->aggregate(Carbon::now()->subDays(89), Carbon::now()->addDay(), 'Mon'), $this->placeholders((int) Carbon::now()->subDays(89)->timestamp, (int) Carbon::now()->addDay()->timestamp, 86400, 'M')->reverse()->take(3)->reverse() ); } diff --git a/app/Services/Transactions/Aggregates/Historical/RangeAggregate.php b/app/Services/Transactions/Aggregates/Historical/RangeAggregate.php index 04eedd51f1..f4604da2f0 100644 --- a/app/Services/Transactions/Aggregates/Historical/RangeAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/RangeAggregate.php @@ -4,10 +4,11 @@ namespace App\Services\Transactions\Aggregates\Historical; -use App\Services\Timestamp; +use App\Facades\Network; use App\Services\Transactions\Aggregates\Concerns\HasQueries; use Carbon\Carbon; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\DB; final class RangeAggregate { @@ -15,11 +16,17 @@ final class RangeAggregate public function aggregate(Carbon $start, Carbon $end, string $format): Collection { + $select = [ + 'MAX(timestamp) as timestamp', + 'COUNT(*) as total', + sprintf("to_char(to_timestamp(%d+timestamp) AT TIME ZONE 'UTC', '%s') as formatted_date", Network::epoch()->timestamp, $format), + ]; + return $this ->dateRangeQuery($start, $end) - ->orderBy('timestamp') - ->get() - ->groupBy(fn ($date) => Timestamp::fromGenesis($date->timestamp)->format($format)) - ->mapWithKeys(fn ($transactions, $day) => [$day => $transactions->count()]); + ->select(DB::raw(implode(', ', $select))) + ->orderBy('formatted_date') + ->groupBy('formatted_date') + ->pluck('total', 'formatted_date'); } } diff --git a/app/Services/Transactions/Aggregates/Historical/WeekAggregate.php b/app/Services/Transactions/Aggregates/Historical/WeekAggregate.php index a59c20cd1e..a40f184f9b 100644 --- a/app/Services/Transactions/Aggregates/Historical/WeekAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/WeekAggregate.php @@ -17,7 +17,7 @@ final class WeekAggregate public function aggregate(): Collection { return $this->mergeWithPlaceholders( - (new RangeAggregate())->aggregate(Carbon::now()->subDays(6), Carbon::now()->addDay(), 'd.m'), + (new RangeAggregate())->aggregate(Carbon::now()->subDays(6), Carbon::now()->addDay(), 'DD.MM'), $this->placeholders((int) Carbon::now()->subDays(6)->timestamp, (int) Carbon::now()->addDay()->timestamp, 86400, 'd.m')->take(7) ); } diff --git a/app/Services/Transactions/Aggregates/Historical/YearAggregate.php b/app/Services/Transactions/Aggregates/Historical/YearAggregate.php index 99195b92a7..a6f72356c8 100644 --- a/app/Services/Transactions/Aggregates/Historical/YearAggregate.php +++ b/app/Services/Transactions/Aggregates/Historical/YearAggregate.php @@ -17,7 +17,7 @@ final class YearAggregate public function aggregate(): Collection { return $this->mergeWithPlaceholders( - (new RangeAggregate())->aggregate(Carbon::now()->subDays(365)->addMonth(), Carbon::now()->addMonth(), 'M'), + (new RangeAggregate())->aggregate(Carbon::now()->subDays(365)->addMonth(), Carbon::now()->addMonth(), 'Mon'), $this->placeholders((int) Carbon::now()->subDays(365)->addMonth()->timestamp, (int) Carbon::now()->addMonth()->timestamp, 86400, 'M')->take(365) ); }