From 40a6cb6962ca708c88339ab74eb19e59bfaa9e4e Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Fri, 25 Aug 2023 15:52:09 +1000 Subject: [PATCH] wip --- .../2023_06_07_000001_create_pulse_tables.php | 12 +++++----- phpstan-baseline.neon | 16 +++++++------- src/PulseServiceProvider.php | 4 ++-- src/Queries/CacheInteractions.php | 2 +- src/Queries/MonitoredCacheInteractions.php | 2 +- src/Queries/Servers.php | 10 ++++----- src/Queries/SlowQueries.php | 2 +- src/Recorders/CacheInteractions.php | 2 +- .../{HttpRequests.php => Requests.php} | 2 +- src/Recorders/SlowQueries.php | 2 +- src/Recorders/SystemStats.php | 2 +- tests/Feature/CacheInteractionsTest.php | 22 +++++++++---------- tests/Feature/SlowQueriesTest.php | 22 +++++++++---------- 13 files changed, 50 insertions(+), 50 deletions(-) rename src/Recorders/{HttpRequests.php => Requests.php} (98%) 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 ea3992e7..50c4faef 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -24,7 +24,7 @@ public function up(): void // - Review column types. Most of these likely need to be a text column, even "route". // - We may need to keep a hashed version of the text columns to index and group by. // - Do another pass at the indexes to ensure that they are optimized correctly. - Schema::create('pulse_servers', function (Blueprint $table) { + Schema::create('pulse_system_stats', function (Blueprint $table) { $table->timestamp('date'); $table->string('server'); $table->unsignedTinyInteger('cpu_percent'); @@ -54,7 +54,7 @@ public function up(): void $table->index(['date', 'class', 'location']); }); - Schema::create('pulse_queries', function (Blueprint $table) { + Schema::create('pulse_slow_queries', function (Blueprint $table) { $table->timestamp('date'); $table->string('user_id')->nullable(); $table->string('sql'); @@ -77,7 +77,7 @@ public function up(): void $table->index(['date', 'user_id']); // user_usage }); - Schema::create('pulse_cache_hits', function (Blueprint $table) { + Schema::create('pulse_cache_interactions', function (Blueprint $table) { $table->timestamp('date'); $table->string('key'); $table->boolean('hit'); @@ -107,12 +107,12 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('pulse_servers'); + Schema::dropIfExists('pulse_system_stats'); Schema::dropIfExists('pulse_requests'); Schema::dropIfExists('pulse_exceptions'); - Schema::dropIfExists('pulse_queries'); + Schema::dropIfExists('pulse_slow_queries'); Schema::dropIfExists('pulse_jobs'); - Schema::dropIfExists('pulse_cache_hits'); + Schema::dropIfExists('pulse_cache_interactions'); Schema::dropIfExists('pulse_outgoing_requests'); Schema::dropIfExists('pulse_queue_sizes'); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index a5ca18f3..8a5da667 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -141,24 +141,24 @@ parameters: path: src/Recorders/Exceptions.php - - message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\Http\\\\Kernel\\:\\:whenRequestLifecycleIsLongerThan\\(\\)\\.$#" + message: "#^Unable to resolve the template type TKey in call to function collect$#" count: 1 - path: src/Recorders/HttpRequests.php + path: src/Recorders/QueueSizes.php - - message: "#^Cannot call method uri\\(\\) on object\\|string\\.$#" + message: "#^Unable to resolve the template type TValue in call to function collect$#" count: 1 - path: src/Recorders/HttpRequests.php + path: src/Recorders/QueueSizes.php - - message: "#^Unable to resolve the template type TKey in call to function collect$#" + message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\Http\\\\Kernel\\:\\:whenRequestLifecycleIsLongerThan\\(\\)\\.$#" count: 1 - path: src/Recorders/QueueSizes.php + path: src/Recorders/Requests.php - - message: "#^Unable to resolve the template type TValue in call to function collect$#" + message: "#^Cannot call method uri\\(\\) on object\\|string\\.$#" count: 1 - path: src/Recorders/QueueSizes.php + path: src/Recorders/Requests.php - message: "#^Unable to resolve the template type TKey in call to function collect$#" diff --git a/src/PulseServiceProvider.php b/src/PulseServiceProvider.php index 3e2ba7a4..835b9f29 100644 --- a/src/PulseServiceProvider.php +++ b/src/PulseServiceProvider.php @@ -21,10 +21,10 @@ use Laravel\Pulse\Ingests\Storage as StorageIngest; use Laravel\Pulse\Recorders\CacheInteractions; use Laravel\Pulse\Recorders\Exceptions; -use Laravel\Pulse\Recorders\HttpRequests; use Laravel\Pulse\Recorders\Jobs; use Laravel\Pulse\Recorders\OutgoingRequests; use Laravel\Pulse\Recorders\QueueSizes; +use Laravel\Pulse\Recorders\Requests; use Laravel\Pulse\Recorders\SlowQueries; use Laravel\Pulse\Recorders\SystemStats; use Laravel\Pulse\Storage\Database as DatabaseStorage; @@ -106,10 +106,10 @@ public function boot(): void $pulse->register([ CacheInteractions::class, Exceptions::class, - HttpRequests::class, Jobs::class, OutgoingRequests::class, QueueSizes::class, + Requests::class, SlowQueries::class, SystemStats::class, ]); diff --git a/src/Queries/CacheInteractions.php b/src/Queries/CacheInteractions.php index b636b854..3e732d8c 100644 --- a/src/Queries/CacheInteractions.php +++ b/src/Queries/CacheInteractions.php @@ -26,7 +26,7 @@ public function __invoke(Interval $interval): object { $now = new CarbonImmutable(); - $cacheInteractions = $this->connection->table('pulse_cache_hits') + $cacheInteractions = $this->connection->table('pulse_cache_interactions') ->selectRaw('COUNT(*) AS count, SUM(CASE WHEN `hit` = TRUE THEN 1 ELSE 0 END) as hits') ->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString()) ->first() ?? (object) ['hits' => 0]; diff --git a/src/Queries/MonitoredCacheInteractions.php b/src/Queries/MonitoredCacheInteractions.php index 98db1146..49843b3d 100644 --- a/src/Queries/MonitoredCacheInteractions.php +++ b/src/Queries/MonitoredCacheInteractions.php @@ -46,7 +46,7 @@ public function __invoke(Interval $interval, Collection $keys): Collection ], ]); - $this->connection->table('pulse_cache_hits') + $this->connection->table('pulse_cache_interactions') ->selectRaw('`key`, COUNT(*) AS count, SUM(CASE WHEN `hit` = TRUE THEN 1 ELSE 0 END) as hits') ->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString()) // TODO: ensure PHP and MySQL regex is compatible diff --git a/src/Queries/Servers.php b/src/Queries/Servers.php index 3f73ea02..4f947f60 100644 --- a/src/Queries/Servers.php +++ b/src/Queries/Servers.php @@ -64,7 +64,7 @@ public function __invoke(Interval $interval): Collection }) ->fromSub( fn (Builder $query) => $query - ->from('pulse_servers') + ->from('pulse_system_stats') ->select(['server', 'cpu_percent', 'memory_used', 'date']) // Divide the data into buckets. ->selectRaw('FLOOR(UNIX_TIMESTAMP(CONVERT_TZ(`date`, ?, @@session.time_zone)) / ?) AS `bucket`', [$now->format('P'), $secondsPerPeriod]) @@ -83,16 +83,16 @@ public function __invoke(Interval $interval): Collection return $padding->merge($readings)->values(); }); - return $this->connection->table('pulse_servers') + return $this->connection->table('pulse_system_stats') // Get the latest row for every server, even if it hasn't reported in the selected period. ->joinSub( - $this->connection->table('pulse_servers') + $this->connection->table('pulse_system_stats') ->selectRaw('server, MAX(date) AS date') ->groupBy('server'), 'grouped', fn (JoinClause $join) => $join - ->on('pulse_servers'.'.server', '=', 'grouped.server') - ->on('pulse_servers'.'.date', '=', 'grouped.date') + ->on('pulse_system_stats'.'.server', '=', 'grouped.server') + ->on('pulse_system_stats'.'.date', '=', 'grouped.date') ) ->get() ->map(fn (stdClass $server) => (object) [ diff --git a/src/Queries/SlowQueries.php b/src/Queries/SlowQueries.php index 536b8736..f8da9d4c 100644 --- a/src/Queries/SlowQueries.php +++ b/src/Queries/SlowQueries.php @@ -32,7 +32,7 @@ public function __invoke(Interval $interval): Collection { $now = new CarbonImmutable; - return $this->connection->table('pulse_queries') + return $this->connection->table('pulse_slow_queries') ->selectRaw('`sql`, COUNT(*) as count, MAX(duration) AS slowest') ->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString()) ->where('duration', '>=', $this->config->get('pulse.slow_query_threshold')) diff --git a/src/Recorders/CacheInteractions.php b/src/Recorders/CacheInteractions.php index 0cba1759..c2303e34 100644 --- a/src/Recorders/CacheInteractions.php +++ b/src/Recorders/CacheInteractions.php @@ -17,7 +17,7 @@ class CacheInteractions /** * The table to record to. */ - public string $table = 'pulse_cache_hits'; + public string $table = 'pulse_cache_interactions'; /** * The events to listen for. diff --git a/src/Recorders/HttpRequests.php b/src/Recorders/Requests.php similarity index 98% rename from src/Recorders/HttpRequests.php rename to src/Recorders/Requests.php index 7a06aee2..c0934706 100644 --- a/src/Recorders/HttpRequests.php +++ b/src/Recorders/Requests.php @@ -13,7 +13,7 @@ /** * @internal */ -class HttpRequests +class Requests { /** * The table to record to. diff --git a/src/Recorders/SlowQueries.php b/src/Recorders/SlowQueries.php index 85ca224e..238a0d12 100644 --- a/src/Recorders/SlowQueries.php +++ b/src/Recorders/SlowQueries.php @@ -16,7 +16,7 @@ class SlowQueries /** * The table to record to. */ - public string $table = 'pulse_queries'; + public string $table = 'pulse_slow_queries'; /** * The events to listen for. diff --git a/src/Recorders/SystemStats.php b/src/Recorders/SystemStats.php index 905f1f2e..46918af9 100644 --- a/src/Recorders/SystemStats.php +++ b/src/Recorders/SystemStats.php @@ -15,7 +15,7 @@ class SystemStats /** * The table to record to. */ - public string $table = 'pulse_servers'; + public string $table = 'pulse_system_stats'; /** * The events to listen for. diff --git a/tests/Feature/CacheInteractionsTest.php b/tests/Feature/CacheInteractionsTest.php index 12560525..bfd0399d 100644 --- a/tests/Feature/CacheInteractionsTest.php +++ b/tests/Feature/CacheInteractionsTest.php @@ -22,12 +22,12 @@ Cache::get('cache-key'); expect(Pulse::entries())->toHaveCount(1); - Pulse::ignore(fn () => expect(DB::table('pulse_cache_hits')->count())->toBe(0)); + Pulse::ignore(fn () => expect(DB::table('pulse_cache_interactions')->count())->toBe(0)); Pulse::store(app(Ingest::class)); expect(Pulse::entries())->toHaveCount(0); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(1); expect((array) $cacheHits[0])->toEqual([ 'date' => '2000-01-02 03:04:05', @@ -43,7 +43,7 @@ Cache::get('illuminate:'); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(0); }); @@ -53,7 +53,7 @@ Cache::get('laravel:pulse'); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(0); }); @@ -65,7 +65,7 @@ Cache::get('miss'); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(2); expect((array) $cacheHits[0])->toEqual([ 'date' => '2000-01-02 03:04:05', @@ -87,7 +87,7 @@ Cache::get('cache-key'); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(1); expect($cacheHits[0]->user_id)->toBe('567'); }); @@ -97,7 +97,7 @@ Auth::login(User::make(['id' => '567'])); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(1); expect($cacheHits[0]->user_id)->toBe('567'); }); @@ -109,7 +109,7 @@ Auth::logout(); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(1); expect($cacheHits[0]->user_id)->toBe('567'); }); @@ -140,7 +140,7 @@ public function user() Cache::get('cache-key'); Pulse::store(app(Ingest::class)); - $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $cacheHits = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($cacheHits)->toHaveCount(1); expect($cacheHits[0]->user_id)->toBe(null); }); @@ -161,7 +161,7 @@ public function hasUser() Cache::get('cache-key'); Pulse::store(app(Ingest::class)); - Pulse::ignore(fn () => expect(DB::table('pulse_cache_hits')->count())->toBe(0)); + Pulse::ignore(fn () => expect(DB::table('pulse_cache_interactions')->count())->toBe(0)); }); it('handles multiple users being logged in', function () { @@ -172,7 +172,7 @@ public function hasUser() Cache::get('cache-key'); Pulse::store(app(Ingest::class)); - $interactions = Pulse::ignore(fn () => DB::table('pulse_cache_hits')->get()); + $interactions = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get()); expect($interactions)->toHaveCount(3); expect($interactions[0]->user_id)->toBe(null); expect($interactions[1]->user_id)->toBe('567'); diff --git a/tests/Feature/SlowQueriesTest.php b/tests/Feature/SlowQueriesTest.php index eab8985f..e6d8552b 100644 --- a/tests/Feature/SlowQueriesTest.php +++ b/tests/Feature/SlowQueriesTest.php @@ -26,12 +26,12 @@ DB::connection()->statement('select * from users'); expect(Pulse::entries())->toHaveCount(1); - Pulse::ignore(fn () => expect(DB::table('pulse_queries')->count())->toBe(0)); + Pulse::ignore(fn () => expect(DB::table('pulse_slow_queries')->count())->toBe(0)); Pulse::store(app(Ingest::class)); expect(Pulse::entries())->toHaveCount(0); - $queries = Pulse::ignore(fn () => DB::table('pulse_queries')->get()); + $queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get()); expect($queries)->toHaveCount(1); expect((array) $queries[0])->toEqual([ 'date' => '2000-01-02 03:04:00', @@ -50,7 +50,7 @@ DB::table('users')->count(); Pulse::store(app(Ingest::class)); - Pulse::ignore(fn () => expect(DB::table('pulse_queries')->count())->toBe(0)); + Pulse::ignore(fn () => expect(DB::table('pulse_slow_queries')->count())->toBe(0)); }); it('ingests queries equal to the slow query threshold', function () { @@ -62,7 +62,7 @@ DB::table('users')->count(); Pulse::store(app(Ingest::class)); - Pulse::ignore(fn () => expect(DB::table('pulse_queries')->count())->toBe(1)); + Pulse::ignore(fn () => expect(DB::table('pulse_slow_queries')->count())->toBe(1)); }); it('ingests queries over the slow query threshold', function () { @@ -74,7 +74,7 @@ DB::table('users')->count(); Pulse::store(app(Ingest::class)); - Pulse::ignore(fn () => expect(DB::table('pulse_queries')->count())->toBe(1)); + Pulse::ignore(fn () => expect(DB::table('pulse_slow_queries')->count())->toBe(1)); }); it('captures the authenticated user', function () { @@ -84,7 +84,7 @@ DB::table('users')->count(); Pulse::store(app(Ingest::class)); - $queries = Pulse::ignore(fn () => DB::table('pulse_queries')->get()); + $queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get()); expect($queries)->toHaveCount(1); expect($queries[0]->user_id)->toBe('567'); }); @@ -95,7 +95,7 @@ Auth::login(User::make(['id' => '567'])); Pulse::store(app(Ingest::class)); - $queries = Pulse::ignore(fn () => DB::table('pulse_queries')->get()); + $queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get()); expect($queries)->toHaveCount(1); expect($queries[0]->user_id)->toBe('567'); }); @@ -108,7 +108,7 @@ Auth::logout(); Pulse::store(app(Ingest::class)); - $queries = Pulse::ignore(fn () => DB::table('pulse_queries')->get()); + $queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get()); expect($queries)->toHaveCount(1); expect($queries[0]->user_id)->toBe('567'); }); @@ -140,7 +140,7 @@ public function user() DB::table('users')->count(); Pulse::store(app(Ingest::class)); - $queries = Pulse::ignore(fn () => DB::table('pulse_queries')->get()); + $queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get()); expect($queries)->toHaveCount(1); expect($queries[0]->user_id)->toBe(null); }); @@ -162,7 +162,7 @@ public function hasUser() DB::table('users')->count(); Pulse::store(app(Ingest::class)); - Pulse::ignore(fn () => expect(DB::table('pulse_queries')->count())->toBe(0)); + Pulse::ignore(fn () => expect(DB::table('pulse_slow_queries')->count())->toBe(0)); }); it('handles multiple users being logged in', function () { @@ -175,7 +175,7 @@ public function hasUser() Pulse::store(app(Ingest::class)); - $queries = Pulse::ignore(fn () => DB::table('pulse_queries')->get()); + $queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get()); expect($queries)->toHaveCount(3); expect($queries[0]->user_id)->toBe(null); expect($queries[1]->user_id)->toBe('567');