Skip to content

Commit

Permalink
refactor: optimize cache-transactions command (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Sep 23, 2021
1 parent a6f616f commit ad1d15d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
15 changes: 10 additions & 5 deletions app/Services/Transactions/Aggregates/Historical/AllAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
17 changes: 12 additions & 5 deletions app/Services/Transactions/Aggregates/Historical/RangeAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@

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
{
use HasQueries;

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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down

0 comments on commit ad1d15d

Please sign in to comment.