Skip to content

Commit

Permalink
Add card helper methods for retrieving data (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher authored Dec 13, 2023
1 parent 047161e commit bfd305b
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 70 deletions.
17 changes: 5 additions & 12 deletions src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ public function purge(?array $types = null): void;
* Retrieve values for the given type.
*
* @param list<string> $keys
* @return \Illuminate\Support\Collection<
* int,
* array<
* string,
* array{
* timestamp: int,
* type: string,
* key: string,
* value: string
* }
* >
* >
* @return \Illuminate\Support\Collection<string, object{
* timestamp: int,
* key: string,
* value: string
* }>
*/
public function values(string $type, ?array $keys = null): Collection;

Expand Down
9 changes: 2 additions & 7 deletions src/Livewire/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\CacheInteractions as CacheInteractionsRecorder;
use Livewire\Attributes\Lazy;

Expand All @@ -24,11 +23,7 @@ public function render(): Renderable
{
[$cacheInteractions, $allTime, $allRunAt] = $this->remember(
fn () => with(
Pulse::aggregateTotal(
['cache_hit', 'cache_miss'],
'count',
$this->periodAsInterval(),
),
$this->aggregateTotal(['cache_hit', 'cache_miss'], 'count'),
fn ($results) => (object) [
'hits' => $results['cache_hit'] ?? 0,
'misses' => $results['cache_miss'] ?? 0,
Expand All @@ -38,7 +33,7 @@ public function render(): Renderable
);

[$cacheKeyInteractions, $keyTime, $keyRunAt] = $this->remember(
fn () => Pulse::aggregateTypes(['cache_hit', 'cache_miss'], 'count', $this->periodAsInterval())
fn () => $this->aggregateTypes(['cache_hit', 'cache_miss'], 'count')
->map(function ($row) {
return (object) [
'key' => $row->key,
Expand Down
77 changes: 77 additions & 0 deletions src/Livewire/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Laravel\Pulse\Livewire;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Livewire\Component;
use Livewire\Livewire;

abstract class Card extends Component
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* The number of columns to span.
*
Expand Down Expand Up @@ -69,4 +72,78 @@ protected function css()
{
return null;
}

/**
* Retrieve values for the given type.
*
* @param list<string> $keys
* @return \Illuminate\Support\Collection<string, object{
* timestamp: int,
* key: string,
* value: string
* }>
*/
protected function values(string $type, ?array $keys = null): Collection
{
return Pulse::values($type, $keys);
}

/**
* Retrieve aggregate values for plotting on a graph.
*
* @param list<string> $types
* @param 'count'|'min'|'max'|'sum'|'avg' $aggregate
* @return \Illuminate\Support\Collection<string, \Illuminate\Support\Collection<string, \Illuminate\Support\Collection<string, int|null>>>
*/
protected function graph(array $types, string $aggregate): Collection
{
return Pulse::graph($types, $aggregate, $this->periodAsInterval());
}

/**
* Retrieve aggregate values for the given type.
*
* @param 'count'|'min'|'max'|'sum'|'avg'|list<'count'|'min'|'max'|'sum'|'avg'> $aggregates
* @return \Illuminate\Support\Collection<int, mixed>
*/
protected function aggregate(
string $type,
string|array $aggregates,
?string $orderBy = null,
string $direction = 'desc',
int $limit = 101,
): Collection {
return Pulse::aggregate($type, $aggregates, $this->periodAsInterval(), $orderBy, $direction, $limit);
}

/**
* Retrieve aggregate values for the given types.
*
* @param string|list<string> $types
* @param 'count'|'min'|'max'|'sum'|'avg' $aggregate
* @return \Illuminate\Support\Collection<int, mixed>
*/
protected function aggregateTypes(
string|array $types,
string $aggregate,
?string $orderBy = null,
string $direction = 'desc',
int $limit = 101,
): Collection {
return Pulse::aggregateTypes($types, $aggregate, $this->periodAsInterval(), $orderBy, $direction, $limit);
}

/**
* Retrieve an aggregate total for the given types.
*
* @param string|list<string> $types
* @param 'count'|'min'|'max'|'sum'|'avg' $aggregate
* @return \Illuminate\Support\Collection<string, int>
*/
protected function aggregateTotal(
array|string $types,
string $aggregate,
): Collection {
return Pulse::aggregateTotal($types, $aggregate, $this->periodAsInterval());
}
}
6 changes: 1 addition & 5 deletions src/Livewire/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\Exceptions as ExceptionsRecorder;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\Url;
Expand All @@ -17,8 +16,6 @@
#[Lazy]
class Exceptions extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Ordering.
*
Expand All @@ -33,10 +30,9 @@ class Exceptions extends Card
public function render(): Renderable
{
[$exceptions, $time, $runAt] = $this->remember(
fn () => Pulse::aggregate(
fn () => $this->aggregate(
'exception',
['max', 'count'],
$this->periodAsInterval(),
match ($this->orderBy) {
'latest' => 'max',
default => 'count'
Expand Down
6 changes: 1 addition & 5 deletions src/Livewire/Queues.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\Queues as QueuesRecorder;
use Livewire\Attributes\Lazy;
use Livewire\Livewire;
Expand All @@ -17,17 +16,14 @@
#[Lazy]
class Queues extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Render the component.
*/
public function render(): Renderable
{
[$queues, $time, $runAt] = $this->remember(fn () => Pulse::graph(
[$queues, $time, $runAt] = $this->remember(fn () => $this->graph(
['queued', 'processing', 'processed', 'released', 'failed'],
'count',
$this->periodAsInterval(),
));

if (Livewire::isLivewireRequest()) {
Expand Down
7 changes: 2 additions & 5 deletions src/Livewire/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Livewire\Attributes\Lazy;
use Livewire\Livewire;

Expand All @@ -15,17 +14,15 @@
#[Lazy]
class Servers extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Render the component.
*/
public function render(): Renderable
{
[$servers, $time, $runAt] = $this->remember(function () {
$graphs = Pulse::graph(['cpu', 'memory'], 'avg', $this->periodAsInterval());
$graphs = $this->graph(['cpu', 'memory'], 'avg');

return Pulse::values('system')
return $this->values('system')
->map(function ($system, $slug) use ($graphs) {
$values = json_decode($system->value, flags: JSON_THROW_ON_ERROR);

Expand Down
6 changes: 1 addition & 5 deletions src/Livewire/SlowJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\SlowJobs as SlowJobsRecorder;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\Url;
Expand All @@ -16,8 +15,6 @@
#[Lazy]
class SlowJobs extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Ordering.
*
Expand All @@ -32,10 +29,9 @@ class SlowJobs extends Card
public function render(): Renderable
{
[$slowJobs, $time, $runAt] = $this->remember(
fn () => Pulse::aggregate(
fn () => $this->aggregate(
'slow_job',
['max', 'count'],
$this->periodAsInterval(),
match ($this->orderBy) {
'count' => 'count',
default => 'max',
Expand Down
6 changes: 1 addition & 5 deletions src/Livewire/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\SlowOutgoingRequests as SlowOutgoingRequestsRecorder;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\Url;
Expand All @@ -16,8 +15,6 @@
#[Lazy]
class SlowOutgoingRequests extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Ordering.
*
Expand All @@ -32,10 +29,9 @@ class SlowOutgoingRequests extends Card
public function render(): Renderable
{
[$slowOutgoingRequests, $time, $runAt] = $this->remember(
fn () => Pulse::aggregate(
fn () => $this->aggregate(
'slow_outgoing_request',
['max', 'count'],
$this->periodAsInterval(),
match ($this->orderBy) {
'count' => 'count',
default => 'max',
Expand Down
6 changes: 1 addition & 5 deletions src/Livewire/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\SlowQueries as SlowQueriesRecorder;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\Url;
Expand All @@ -16,8 +15,6 @@
#[Lazy]
class SlowQueries extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Ordering.
*
Expand All @@ -32,10 +29,9 @@ class SlowQueries extends Card
public function render(): Renderable
{
[$slowQueries, $time, $runAt] = $this->remember(
fn () => Pulse::aggregate(
fn () => $this->aggregate(
'slow_query',
['max', 'count'],
$this->periodAsInterval(),
match ($this->orderBy) {
'count' => 'count',
default => 'max',
Expand Down
6 changes: 1 addition & 5 deletions src/Livewire/SlowRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Recorders\SlowRequests as SlowRequestsRecorder;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\Url;
Expand All @@ -16,8 +15,6 @@
#[Lazy]
class SlowRequests extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* Ordering.
*
Expand All @@ -32,10 +29,9 @@ class SlowRequests extends Card
public function render(): Renderable
{
[$slowRequests, $time, $runAt] = $this->remember(
fn () => Pulse::aggregate(
fn () => $this->aggregate(
'slow_request',
['max', 'count'],
$this->periodAsInterval(),
match ($this->orderBy) {
'count' => 'count',
default => 'max',
Expand Down
5 changes: 1 addition & 4 deletions src/Livewire/Usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#[Lazy]
class Usage extends Card
{
use Concerns\HasPeriod, Concerns\RemembersQueries;

/**
* The type of usage to show.
*
Expand All @@ -44,14 +42,13 @@ public function render(): Renderable

[$userRequestCounts, $time, $runAt] = $this->remember(
function () use ($type) {
$counts = Pulse::aggregate(
$counts = $this->aggregate(
match ($type) {
'requests' => 'user_request',
'slow_requests' => 'slow_user_request',
'jobs' => 'user_job',
},
'count',
$this->periodAsInterval(),
limit: 10,
);

Expand Down
Loading

0 comments on commit bfd305b

Please sign in to comment.