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 862a7870..aca06198 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -106,8 +106,11 @@ public function up(): void $table->text('uri'); $table->char('uri_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(`uri`))'); $table->unsignedInteger('duration'); + $table->boolean('slow'); - $table->index(['uri_hash', 'date', 'duration']); + $table->index(['date']); // trim + $table->index(['uri_hash']); // slow_outgoing_requests + $table->index(['slow', 'date', 'uri_hash', 'duration']); // slow_outgoing_requests }); } diff --git a/src/Queries/SlowOutgoingRequests.php b/src/Queries/SlowOutgoingRequests.php index e3a89750..1e4c3d42 100644 --- a/src/Queries/SlowOutgoingRequests.php +++ b/src/Queries/SlowOutgoingRequests.php @@ -6,8 +6,8 @@ use Carbon\CarbonInterval as Interval; use Illuminate\Config\Repository; use Illuminate\Database\DatabaseManager; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; -use Laravel\Pulse\Recorders\OutgoingRequests; /** * @internal @@ -35,13 +35,21 @@ public function __invoke(Interval $interval): Collection { $now = new CarbonImmutable; - return $this->connection()->table('pulse_outgoing_requests') - ->selectRaw('MAX(`uri`) AS `uri`, COUNT(*) AS `count`, MAX(`duration`) AS `slowest`') + return $this->connection()->query()->select([ + 'count', + 'slowest', + 'uri' => fn (Builder $query) => $query->select('uri') + ->from('pulse_outgoing_requests', as: 'child') + ->whereRaw('`child`.`uri_hash` = `parent`.`uri_hash`') + ->limit(1), + ])->fromSub(fn (Builder $query) => $query->selectRaw('`uri_hash`, MAX(`duration`) as `slowest`, COUNT(*) as `count`') + ->from('pulse_outgoing_requests') + ->where('slow', true) ->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString()) - ->where('duration', '>=', $this->config->get('pulse.recorders.'.OutgoingRequests::class.'.threshold')) ->groupBy('uri_hash') ->orderByDesc('slowest') - ->limit(101) + ->orderByDesc('count') + ->limit(101), as: 'parent') ->get(); } } diff --git a/src/Queries/SlowRoutes.php b/src/Queries/SlowRoutes.php index 64bd070d..5af025fe 100644 --- a/src/Queries/SlowRoutes.php +++ b/src/Queries/SlowRoutes.php @@ -57,7 +57,7 @@ public function __invoke(Interval $interval): Collection ->groupBy('route_hash') ->orderByDesc('slowest') ->orderByDesc('count') - ->limit(100), as: 'parent') + ->limit(101), as: 'parent') ->get() ->map(fn (stdClass $row) => (object) [ 'route' => (string) $row->route, diff --git a/src/Recorders/OutgoingRequests.php b/src/Recorders/OutgoingRequests.php index a1f2f68c..204fe32a 100644 --- a/src/Recorders/OutgoingRequests.php +++ b/src/Recorders/OutgoingRequests.php @@ -63,8 +63,9 @@ public function record(RequestInterface $request, CarbonImmutable $startedAt): ? return new Entry($this->table, [ 'uri' => $this->normalizeUri($request), 'date' => $startedAt->toDateTimeString(), - 'duration' => $startedAt->diffInMilliseconds($endedAt), + 'duration' => $duration = $startedAt->diffInMilliseconds($endedAt), 'user_id' => $this->pulse->authenticatedUserIdResolver(), + 'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'), ]); } diff --git a/src/Recorders/Requests.php b/src/Recorders/Requests.php index aba1df4f..70d810ce 100644 --- a/src/Recorders/Requests.php +++ b/src/Recorders/Requests.php @@ -66,7 +66,7 @@ public function record(Carbon $startedAt, Request $request, Response $response): 'route' => $request->method().' '.$path, 'duration' => $duration = $startedAt->diffInMilliseconds(), 'user_id' => $this->pulse->authenticatedUserIdResolver(), - 'slow' => $duration >= $this->config->get('pulse.recorders.'.self::class.'.threshold'), + 'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'), ]); } diff --git a/tests/Feature/Livewire/SlowOutgoingRequestsTest.php b/tests/Feature/Livewire/SlowOutgoingRequestsTest.php index 2b75eb97..f2b01a57 100644 --- a/tests/Feature/Livewire/SlowOutgoingRequestsTest.php +++ b/tests/Feature/Livewire/SlowOutgoingRequestsTest.php @@ -16,10 +16,10 @@ it('renders slow outgoing requests', function () { Pulse::ignore(fn () => DB::table('pulse_outgoing_requests')->insert([ - ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 1234], - ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 2468], - ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 123], - ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 1234], + ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 1234, 'slow' => true], + ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 2468, 'slow' => true], + ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 123, 'slow' => false], + ['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 1234, 'slow' => true], ])); Carbon::setTestNow('2000-01-02 03:04:15');