Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Aug 25, 2023
1 parent aa1aa2f commit 40a6cb6
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 50 deletions.
12 changes: 6 additions & 6 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
}
Expand Down
16 changes: 8 additions & 8 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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$#"
Expand Down
4 changes: 2 additions & 2 deletions src/PulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/CacheInteractions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/MonitoredCacheInteractions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Queries/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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) [
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
2 changes: 1 addition & 1 deletion src/Recorders/CacheInteractions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @internal
*/
class HttpRequests
class Requests
{
/**
* The table to record to.
Expand Down
2 changes: 1 addition & 1 deletion src/Recorders/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Recorders/SystemStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/CacheInteractionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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',
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand Down Expand Up @@ -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);
});
Expand All @@ -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 () {
Expand All @@ -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');
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/SlowQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand Down Expand Up @@ -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);
});
Expand All @@ -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 () {
Expand All @@ -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');
Expand Down

0 comments on commit 40a6cb6

Please sign in to comment.