Skip to content

Cache improvements #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@
*/

'cache_keys' => [
'^post:139$' => 'Post 139',
'^server:1\d{2}$' => 'Servers 100 - 199',
'^flight:.*' => 'All flights',
// '/^user:.\d+:(.*)/' => 'user:*:\1',
// '/^user:.+$/' => 'user:*',
'/(.*)/' => '\1',
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function up(): void
$table->string('key');
$table->boolean('hit');
$table->string('user_id')->nullable();
// TODO: indexes?
$table->index(['date', 'key']);
});

Schema::create('pulse_outgoing_requests', function (Blueprint $table) {
Expand Down
64 changes: 30 additions & 34 deletions resources/views/livewire/cache.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<x-pulse::card :cols="$cols" :rows="$rows" :class="$class">
<x-pulse::card-header
name="Cache"
title="Global Time: {{ $allTime }}; Global run at: {{ $allRunAt }}; Monitored Time: {{ $monitoredTime }}; Monitored run at: {{ $monitoredRunAt }};"
title="Global Time: {{ $allTime }}; Global run at: {{ $allRunAt }}; Key Time: {{ $keyTime }}; Key run at: {{ $keyRunAt }};"
details="past {{ $this->periodForHumans() }}"
>
<x-slot:icon>
Expand All @@ -25,39 +25,35 @@
}"
:class="[loadingNewDataset ? 'opacity-25 animate-pulse' : '', 'space-y-6']"
>
<div class="grid grid-cols-3 gap-3 text-center">
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ number_format($allCacheInteractions->hits) }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Hits
</span>
</div>
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ number_format($allCacheInteractions->count - $allCacheInteractions->hits) }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Misses
</span>
</div>
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ $allCacheInteractions->count > 0 ? round(($allCacheInteractions->hits / $allCacheInteractions->count) * 100, 2).'%' : '-' }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Hit Rate
</span>
</div>
</div>
@if ($monitoredCacheInteractions->isEmpty())
<div class="flex flex-col items-center justify-center p-4 py-6">
<div class="bg-gray-50 dark:bg-gray-800 rounded-full text-xs leading-none px-2 py-1 text-gray-500 dark:text-gray-400">
No keys configured to monitor
@if ($allCacheInteractions->count === 0)
<x-pulse::no-results />
@else
<div class="grid grid-cols-3 gap-3 text-center">
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ number_format($allCacheInteractions->hits) }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Hits
</span>
</div>
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ number_format($allCacheInteractions->count - $allCacheInteractions->hits) }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Misses
</span>
</div>
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ $allCacheInteractions->count > 0 ? round(($allCacheInteractions->hits / $allCacheInteractions->count) * 100, 2).'%' : '-' }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Hit Rate
</span>
</div>
</div>
@else
<x-pulse::table>
<colgroup>
<col width="100%" />
Expand All @@ -67,14 +63,14 @@
</colgroup>
<x-pulse::thead>
<tr>
<x-pulse::th class="text-left">Name</x-pulse::th>
<x-pulse::th class="text-left">Key</x-pulse::th>
<x-pulse::th class="text-right">Hits</x-pulse::th>
<x-pulse::th class="text-right">Misses</x-pulse::th>
<x-pulse::th class="text-right whitespace-nowrap">Hit Rate</x-pulse::th>
</tr>
</x-pulse::thead>
<tbody>
@foreach ($monitoredCacheInteractions as $interaction)
@foreach ($cacheKeyInteractions->take(100) as $interaction)
<tr class="h-2 first:h-0"></tr>
<tr wire:key="{{ $interaction->key }}">
<x-pulse::td>
Expand Down
21 changes: 7 additions & 14 deletions src/Livewire/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laravel\Pulse\Livewire\Concerns\RemembersQueries;
use Laravel\Pulse\Livewire\Concerns\ShouldNotReportUsage;
use Laravel\Pulse\Queries\CacheInteractions;
use Laravel\Pulse\Queries\CacheKeyInteractions;
use Laravel\Pulse\Queries\MonitoredCacheInteractions;
use Livewire\Attributes\Lazy;

Expand All @@ -20,27 +21,19 @@ class Cache extends Card
/**
* Render the component.
*/
public function render(CacheInteractions $cacheInteractionsQuery, MonitoredCacheInteractions $monitoredCacheInteractionsQuery): Renderable
public function render(CacheInteractions $cacheInteractionsQuery, CacheKeyInteractions $cacheKeyInteractionsQuery): Renderable
{
$monitoring = collect(Config::get('pulse.cache_keys'))
->mapWithKeys(fn (string $value, int|string $key) => is_string($key)
? [$key => $value]
: [$value => $value]);
[$cacheInteractions, $allTime, $allRunAt] = $this->remember($cacheInteractionsQuery, 'all');

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

[$monitoredCacheInteractions, $monitoredTime, $monitoredRunAt] = $this->remember(
fn ($interval) => $monitoredCacheInteractionsQuery($interval, $monitoring),
md5($monitoring->toJson())
);
[$cacheKeyInteractions, $keyTime, $keyRunAt] = $this->remember($cacheKeyInteractionsQuery, 'keys');

return View::make('pulse::livewire.cache', [
'allTime' => $allTime,
'allRunAt' => $allRunAt,
'monitoredTime' => $monitoredTime,
'monitoredRunAt' => $monitoredRunAt,
'allCacheInteractions' => $cacheInteractions,
'monitoredCacheInteractions' => $monitoredCacheInteractions,
'keyTime' => $keyTime,
'keyRunAt' => $keyRunAt,
'cacheKeyInteractions' => $cacheKeyInteractions,
]);
}
}
10 changes: 3 additions & 7 deletions src/Queries/CacheInteractions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ public function __invoke(Interval $interval): object
{
$now = new CarbonImmutable();

$cacheInteractions = $this->connection()->table('pulse_cache_interactions')
return $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];

$cacheInteractions->hits = (int) $cacheInteractions->hits;

return $cacheInteractions;
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->first() ?? (object) ['count' => 0, 'hits' => '0'];
}
}
47 changes: 47 additions & 0 deletions src/Queries/CacheKeyInteractions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Laravel\Pulse\Queries;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterval as Interval;
use Illuminate\Config\Repository;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use stdClass;

/**
* @internal
*/
class CacheKeyInteractions
{
use Concerns\InteractsWithConnection;

/**
* Create a new query instance.
*/
public function __construct(
protected DatabaseManager $db,
protected Repository $config,
) {
//
}

/**
* Run the query.
*
* @return \Illuminate\Support\Collection<string, object>
*/
public function __invoke(Interval $interval): Collection
{
$now = new CarbonImmutable();

return $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())
->groupBy('key')
->orderByDesc('count')
->limit(101)
->get();
}
}
2 changes: 1 addition & 1 deletion src/Queries/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(Interval $interval, string $orderBy): Collection

return $this->connection()->table('pulse_exceptions')
->selectRaw('class, location, COUNT(*) AS count, MAX(date) AS last_occurrence')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->groupBy('class', 'location')
->orderByDesc($orderBy)
->get();
Expand Down
80 changes: 0 additions & 80 deletions src/Queries/MonitoredCacheInteractions.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Queries/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __invoke(Interval $interval): Collection
->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])
->where('date', '>=', $now->ceilSeconds($interval->totalSeconds / $maxDataPoints)->subSeconds((int) $interval->totalSeconds)),
->where('date', '>', $now->ceilSeconds($interval->totalSeconds / $maxDataPoints)->subSeconds((int) $interval->totalSeconds)),
'grouped'
)
->groupBy('server', 'bucket')
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/SlowJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(Interval $interval): Collection

return $this->connection()->table('pulse_jobs')
->selectRaw('`job`, SUM(slow) as count, MAX(slowest) as slowest')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('slow', '>', 0)
->groupBy('job')
->orderByDesc('slowest')
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(Interval $interval): Collection

return $this->connection()->table('pulse_outgoing_requests')
->selectRaw('`uri`, COUNT(*) as count, MAX(duration) AS slowest')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('duration', '>=', $this->config->get('pulse.slow_outgoing_request_threshold'))
->groupBy('uri')
->orderByDesc('slowest')
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(Interval $interval): Collection

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('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->groupBy('sql')
->orderByDesc('slowest')
->get();
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/SlowRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __invoke(Interval $interval): Collection

return $this->connection()->table('pulse_requests')
->selectRaw('route, COUNT(*) as count, MAX(duration) AS slowest')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('duration', '>=', $this->config->get('pulse.slow_endpoint_threshold'))
->groupBy('route')
->orderByDesc('slowest')
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/Usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke(Interval $interval, string $type): Collection
fn (Builder $query) => $query->from('pulse_requests'))
->selectRaw('user_id, COUNT(*) as count')
->whereNotNull('user_id')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->when($type === 'slow_endpoint_counts',
fn (Builder $query) => $query->where('duration', '>=', $this->config->get('pulse.slow_endpoint_threshold')))
->groupBy('user_id')
Expand Down
Loading