Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Sep 1, 2023
1 parent 78ad5da commit eb34ab0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 224 deletions.
80 changes: 13 additions & 67 deletions src/Livewire/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@

namespace Laravel\Pulse\Livewire;

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache as CacheFacade;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Livewire\Concerns\HasPeriod;
use Laravel\Pulse\Livewire\Concerns\RemembersQueries;
use Laravel\Pulse\Livewire\Concerns\ShouldNotReportUsage;
use Livewire\Component;

class Cache extends Component
{
use HasPeriod, ShouldNotReportUsage;
use HasPeriod, ShouldNotReportUsage, RemembersQueries;

/**
* Render the component.
*/
public function render(callable $cacheInteractionsQuery, callable $monitoredCacheInteractionsQuery): Renderable
{
[$cacheInteractions, $allTime, $allRunAt] = $this->cacheInteractions($cacheInteractionsQuery);
$monitoring = collect(Config::get('pulse.cache_keys'))
->mapWithKeys(fn (string $value, int|string $key) => is_string($key)
? [$key => $value]
: [$value => $value]);

[$monitoredCacheInteractions, $monitoredTime, $monitoredRunAt] = $this->monitoredCacheInteractions($monitoredCacheInteractionsQuery);
[$cacheInteractions, $allTime, $allRunAt] = $this->remember($cacheInteractionsQuery);

[$monitoredCacheInteractions, $monitoredTime, $monitoredRunAt] = $this->remember(
fn ($interval) => $monitoredCacheInteractionsQuery($interval, $monitoring),
md5($monitoring->toJson())
);

return View::make('pulse::livewire.cache', [
'allTime' => $allTime,
Expand All @@ -41,65 +48,4 @@ public function placeholder(): Renderable
{
return View::make('pulse::components.placeholder', ['class' => 'col-span-3']);
}

/**
* All the cache interactions.
*
* @return array{mixed, int, string}
*/
protected function cacheInteractions(callable $query): array
{
return CacheFacade::remember("laravel:pulse:cache-all:{$this->period}", $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

$start = hrtime(true);

$cacheInteractions = $query($this->periodAsInterval());

$time = (int) ((hrtime(true) - $start) / 1000000);

return [$cacheInteractions, $time, $now->toDateTimeString()];
});
}

/**
* The monitored cache interactions.
*
* @return array{mixed, int, string}
*/
protected function monitoredCacheInteractions(callable $query): array
{
return CacheFacade::remember("laravel:pulse:cache-monitored:{$this->period}:{$this->monitoredKeysCacheHash()}", $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

$start = hrtime(true);

$interactions = $query($this->periodAsInterval(), $this->monitoredKeys());

$time = (int) ((hrtime(true) - $start) / 1000000);

return [$interactions, $time, $now->toDateTimeString()];
});
}

/**
* The monitored keys.
*
* @return \Illuminate\Support\Collection<string, string>
*/
protected function monitoredKeys(): Collection
{
return collect(config('pulse.cache_keys'))
->mapWithKeys(fn (string $value, int|string $key) => is_string($key)
? [$key => $value]
: [$value => $value]);
}

/**
* The monitored keys cache hash.
*/
protected function monitoredKeysCacheHash(): string
{
return $this->monitoredKeys()->toJson();
}
}
26 changes: 26 additions & 0 deletions src/Livewire/Concerns/RemembersQueries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Laravel\Pulse\Livewire\Concerns;

use Carbon\CarbonImmutable;
use Illuminate\Support\Benchmark;
use Illuminate\Support\Facades\Cache;

trait RemembersQueries
{
/**
* Remember the query for the current period.
*
* @return array{0: mixed, 1: float, 2: string}
*/
public function remember(callable $query, string $key = ''): array
{
return Cache::remember('laravel:pulse:'.static::class.':'.$key.':'.$this->period, $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

[$value, $duration] = Benchmark::value(fn () => $query($this->periodAsInterval()));

return [$value, $duration, $now->toDateTimeString()];
});
}
}
35 changes: 8 additions & 27 deletions src/Livewire/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

namespace Laravel\Pulse\Livewire;

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Livewire\Concerns\HasPeriod;
use Laravel\Pulse\Livewire\Concerns\RemembersQueries;
use Laravel\Pulse\Livewire\Concerns\ShouldNotReportUsage;
use Livewire\Attributes\Url;
use Livewire\Component;

class Exceptions extends Component
{
use HasPeriod, ShouldNotReportUsage;
use HasPeriod, ShouldNotReportUsage, RemembersQueries;

/**
* The view type
Expand All @@ -28,7 +27,12 @@ class Exceptions extends Component
*/
public function render(callable $query): Renderable
{
[$exceptions, $time, $runAt] = $this->exceptions($query);
$orderBy = match ($this->orderBy) {
'last_occurrence' => 'last_occurrence',
default => 'count'
};

[$exceptions, $time, $runAt] = $this->remember(fn ($interval) => $query($interval, $orderBy), $orderBy);

return View::make('pulse::livewire.exceptions', [
'time' => $time,
Expand All @@ -44,27 +48,4 @@ public function placeholder(): Renderable
{
return View::make('pulse::components.placeholder', ['class' => 'col-span-3']);
}

/**
* The exceptions.
*
* @return array{mixed, int, string}
*/
protected function exceptions(callable $query): array
{
return Cache::remember("laravel:pulse:exceptions:{$this->orderBy}:{$this->period}", $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

$start = hrtime(true);

$exceptions = $query($this->periodAsInterval(), match ($this->orderBy) {
'last_occurrence' => 'last_occurrence',
default => 'count'
});

$time = (int) ((hrtime(true) - $start) / 1000000);

return [$exceptions, $time, $now->toDateTimeString()];
});
}
}
27 changes: 3 additions & 24 deletions src/Livewire/SlowJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

namespace Laravel\Pulse\Livewire;

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Livewire\Concerns\HasPeriod;
use Laravel\Pulse\Livewire\Concerns\RemembersQueries;
use Laravel\Pulse\Livewire\Concerns\ShouldNotReportUsage;
use Livewire\Component;

class SlowJobs extends Component
{
use HasPeriod, ShouldNotReportUsage;
use HasPeriod, ShouldNotReportUsage, RemembersQueries;

/**
* Render the component.
*/
public function render(callable $query): Renderable
{
[$slowJobs, $time, $runAt] = $this->slowJobs($query);
[$slowJobs, $time, $runAt] = $this->remember($query);

return View::make('pulse::livewire.slow-jobs', [
'time' => $time,
Expand All @@ -35,24 +34,4 @@ public function placeholder(): Renderable
{
return View::make('pulse::components.placeholder', ['class' => 'col-span-3']);
}

/**
* The slow jobs.
*
* @return array{mixed, int, string}
*/
protected function slowJobs(callable $query): array
{
return Cache::remember("laravel:pulse:slow-jobs:{$this->period}", $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

$start = hrtime(true);

$slowJobs = $query($this->periodAsInterval());

$time = (int) ((hrtime(true) - $start) / 1000000);

return [$slowJobs, $time, $now->toDateTimeString()];
});
}
}
27 changes: 3 additions & 24 deletions src/Livewire/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

namespace Laravel\Pulse\Livewire;

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Client\Factory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Livewire\Concerns\HasPeriod;
use Laravel\Pulse\Livewire\Concerns\RemembersQueries;
use Laravel\Pulse\Livewire\Concerns\ShouldNotReportUsage;
use Livewire\Component;

class SlowOutgoingRequests extends Component
{
use HasPeriod, ShouldNotReportUsage;
use HasPeriod, ShouldNotReportUsage, RemembersQueries;

/**
* Render the component.
*/
public function render(callable $query): Renderable
{
[$slowOutgoingRequests, $time, $runAt] = $this->slowOutgoingRequests($query);
[$slowOutgoingRequests, $time, $runAt] = $this->remember($query);

return View::make('pulse::livewire.slow-outgoing-requests', [
'time' => $time,
Expand All @@ -37,24 +36,4 @@ public function placeholder(): Renderable
{
return View::make('pulse::components.placeholder', ['class' => 'col-span-3']);
}

/**
* The slow outgoing requests.
*
* @return array{mixed, int, string}
*/
protected function slowOutgoingRequests(callable $query): array
{
return Cache::remember("laravel:pulse:slow-outgoing-requests:{$this->period}", $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

$start = hrtime(true);

$slowOutgoingRequests = $query($this->periodAsInterval());

$time = (int) ((hrtime(true) - $start) / 1000000);

return [$slowOutgoingRequests, $time, $now->toDateTimeString()];
});
}
}
27 changes: 3 additions & 24 deletions src/Livewire/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

namespace Laravel\Pulse\Livewire;

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Livewire\Concerns\HasPeriod;
use Laravel\Pulse\Livewire\Concerns\RemembersQueries;
use Laravel\Pulse\Livewire\Concerns\ShouldNotReportUsage;
use Livewire\Component;

class SlowQueries extends Component
{
use HasPeriod, ShouldNotReportUsage;
use HasPeriod, ShouldNotReportUsage, RemembersQueries;

/**
* Render the component.
*/
public function render(callable $query): Renderable
{
[$slowQueries, $time, $runAt] = $this->slowQueries($query);
[$slowQueries, $time, $runAt] = $this->remember($query);

return View::make('pulse::livewire.slow-queries', [
'time' => $time,
Expand All @@ -35,24 +34,4 @@ public function placeholder(): Renderable
{
return View::make('pulse::components.placeholder', ['class' => 'col-span-3']);
}

/**
* The slow queries.
*
* @return array{mixed, int, string}
*/
protected function slowQueries(callable $query): array
{
return Cache::remember("laravel:pulse:slow-queries:{$this->period}", $this->periodCacheDuration(), function () use ($query) {
$now = new CarbonImmutable;

$start = hrtime(true);

$slowQueries = $query($this->periodAsInterval());

$time = (int) ((hrtime(true) - $start) / 1000000);

return [$slowQueries, $time, $now->toDateTimeString()];
});
}
}
Loading

0 comments on commit eb34ab0

Please sign in to comment.