diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php index 79ae0f0b..46928f76 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -55,7 +55,11 @@ public function up(): void $table->text('location'); $table->char('class_location_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(CONCAT(`class`, `location`)))'); - $table->index(['date', 'class_location_hash']); + $table->index(['class_location_hash']); // exceptions + $table->index([ + 'date', // exceptions, trim + 'class_location_hash', // exceptions + ]); }); Schema::create('pulse_slow_queries', function (Blueprint $table) { diff --git a/resources/views/livewire/exceptions.blade.php b/resources/views/livewire/exceptions.blade.php index c2dd8a3b..86bb9373 100644 --- a/resources/views/livewire/exceptions.blade.php +++ b/resources/views/livewire/exceptions.blade.php @@ -57,7 +57,7 @@ class="min-h-full flex flex-col" - @foreach ($exceptions as $exception) + @foreach ($exceptions->take(100) as $exception) @@ -83,6 +83,10 @@ class="min-h-full flex flex-col" @endif + + @if ($exceptions->count() > 100) +
Limited to 100 entries
+ @endif diff --git a/src/Queries/Exceptions.php b/src/Queries/Exceptions.php index 9d996ff9..10fb09a9 100644 --- a/src/Queries/Exceptions.php +++ b/src/Queries/Exceptions.php @@ -6,6 +6,7 @@ use Carbon\CarbonInterval as Interval; use Illuminate\Config\Repository; use Illuminate\Database\DatabaseManager; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; /** @@ -35,11 +36,23 @@ public function __invoke(Interval $interval, string $orderBy): Collection { $now = new CarbonImmutable; - return $this->connection()->table('pulse_exceptions') - ->selectRaw('MAX(`class`) AS `class`, MAX(`location`) AS `location`, COUNT(*) AS `count`, MAX(`date`) AS `last_occurrence`') + return $this->connection()->query()->select([ + 'count', + 'last_occurrence', + 'class' => fn (Builder $query) => $query->select('class') + ->from('pulse_exceptions', as: 'child1') + ->whereRaw('`child1`.`class_location_hash` = `parent`.`class_location_hash`') + ->limit(1), + 'location' => fn (Builder $query) => $query->select('location') + ->from('pulse_exceptions', as: 'child2') + ->whereRaw('`child2`.`class_location_hash` = `parent`.`class_location_hash`') + ->limit(1), + ])->fromSub(fn (Builder $query) => $query->selectRaw('`class_location_hash`, MAX(`date`) as `last_occurrence`, COUNT(*) as `count`') + ->from('pulse_exceptions') ->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString()) ->groupBy('class_location_hash') ->orderByDesc($orderBy) + ->limit(101), as: 'parent') ->get(); } }