-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Measure Retrieve data (#1393)
* Add MeasureRetrieveData event * phpstan * add laravel pulse card * add config enable / disable * fix cypress
- Loading branch information
1 parent
c640aac
commit 2643640
Showing
9 changed files
with
258 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<x-pulse::card :cols="$cols" :rows="$rows" :class="$class" wire:poll.5s=""> | ||
<x-pulse::card-header name="PowerGrid" details="10 most recent records"> | ||
<x-slot:icon>⚡</x-slot:icon> | ||
</x-pulse::card-header> | ||
|
||
<x-pulse::scroll :expand="$expand"> | ||
<div class="grid grid-cols-1 gap-2"> | ||
@if ($measurements->isEmpty()) | ||
<x-pulse::no-results /> | ||
@elseif (!$config['enabled']) | ||
<div class="h-full flex flex-col items-center justify-center p-4"> | ||
<x-pulse::icons.no-pulse class="h-8 w-8 stroke-gray-300 dark:stroke-gray-700" /> | ||
<p class="mt-2 text-sm text-gray-400 dark:text-gray-600"> | ||
PowerGrid metering has been disabled. | ||
</p> | ||
</div> | ||
|
||
@else | ||
<div class="grid grid-cols-2 gap-3 text-center"> | ||
<div class="flex flex-col justify-center sm:block"> | ||
<div class="font-bold text-gray-700 dark:text-gray-300 tabular-nums"> | ||
<span class="uppercase text-xl">{{ round($averageRetrieveData, 2) }}</span> <span class="!text-sm">ms</span> | ||
</div> | ||
|
||
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400"> | ||
retrieve Data | ||
</span> | ||
</div> | ||
<div class="flex flex-col justify-center sm:block"> | ||
<div class="font-bold text-gray-700 dark:text-gray-300 tabular-nums"> | ||
<span class="uppercase text-xl">{{ round($averageQueriesTime, 2) }}</span> <span class="!text-sm">ms</span> | ||
</div> | ||
|
||
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400"> | ||
query Time | ||
</span> | ||
</div> | ||
</div> | ||
<div> | ||
<x-pulse::table> | ||
<colgroup> | ||
<col /> | ||
<col /> | ||
<col /> | ||
</colgroup> | ||
<x-pulse::thead> | ||
<tr> | ||
<x-pulse::th class="text-left">Table</x-pulse::th> | ||
<x-pulse::th class="text-right">Time</x-pulse::th> | ||
<x-pulse::th class="text-right">Query Time</x-pulse::th> | ||
<x-pulse::th class="text-right">Total Queries</x-pulse::th> | ||
<x-pulse::th class="text-right">Created at</x-pulse::th> | ||
</tr> | ||
</x-pulse::thead> | ||
<tbody> | ||
@foreach ($measurements as $measurement) | ||
<tr wire:key="{{ $loop->index }}-spacer" class="h-2 first:h-0"></tr> | ||
<tr wire:key="{{ $loop->index }}-row"> | ||
<x-pulse::td class="max-w-[1px]"> | ||
<code class="block text-xs text-gray-900 dark:text-gray-100 truncate" title="{{ data_get($measurement, 'tableName') }}"> | ||
{{ data_get($measurement, 'tableName') }} | ||
</code> | ||
</x-pulse::td> | ||
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold"> | ||
{{ data_get($measurement, 'retrieveData') }} ms | ||
</x-pulse::td> | ||
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold"> | ||
{{ data_get($measurement, 'queriesTime') }} ms | ||
</x-pulse::td> | ||
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold"> | ||
{{ count(data_get($measurement, 'queries') ) }} | ||
</x-pulse::td> | ||
<x-pulse::td numeric class="text-gray-700 text-sm dark:text-gray-300 font-bold"> | ||
@php | ||
$createdAt = \Illuminate\Support\Carbon::createFromTimestamp(data_get($measurement, 'timestamp')); | ||
@endphp | ||
{{ $createdAt->diffForHumans() }} | ||
</x-pulse::td> | ||
</tr> | ||
@endforeach | ||
</tbody> | ||
</x-pulse::table> | ||
@endif | ||
</div> | ||
</div> | ||
</x-pulse::scroll> | ||
</x-pulse::card> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace PowerComponents\LivewirePowerGrid\Events; | ||
|
||
class MeasureRetrieveData | ||
{ | ||
/** | ||
* @param string $tableName Name of the table where the data was retrieved. | ||
* @param float $retrieveData Total time spent on the data retrieval operation. | ||
* @param float $queriesTime Total time spent on executing queries. | ||
* @param bool $cached Indicates whether the data was retrieved from the cache. | ||
* @param array $queries List of queries executed (query, binding, time). | ||
*/ | ||
public function __construct( | ||
public string $tableName, | ||
public float $retrieveData, | ||
public float $queriesTime = 0, | ||
public bool $cached = false, | ||
public array $queries = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace PowerComponents\LivewirePowerGrid\Livewire; | ||
|
||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Support\Facades\Config; | ||
use Livewire\Attributes\Lazy; | ||
use PowerComponents\LivewirePowerGrid\Recorders\PowerGridRecorder; | ||
|
||
#[Lazy] | ||
class MeasurementCard extends \Laravel\Pulse\Livewire\Card | ||
{ | ||
public function render(): View | ||
{ | ||
$config = Config::get('pulse.recorders.' . PowerGridRecorder::class); | ||
|
||
$averageRetrieveData = 0; | ||
$averageQueriesTime = 0; | ||
$measurements = collect(); | ||
|
||
if (data_get($config, 'enabled')) { | ||
$measurements = \Laravel\Pulse\Facades\Pulse::values('powergrid-measurements') | ||
->map(function ($item) { | ||
/** @var array $value */ | ||
$value = json_decode($item->value, true); | ||
$item->tableName = $value['tableName']; | ||
$item->retrieveData = $value['retrieveData']; | ||
$item->queriesTime = $value['queriesTime']; | ||
$item->cached = $value['cached']; | ||
$item->queries = $value['queries']; | ||
unset($item->value); | ||
|
||
return $item; | ||
}) | ||
->sort(fn ($a, $b) => $b->timestamp <=> $a->timestamp) | ||
->take(10) | ||
->values(); | ||
|
||
$averageRetrieveData = $measurements->avg(fn ($item) => $item->retrieveData); | ||
$averageQueriesTime = $measurements->avg(fn ($item) => $item->queriesTime); | ||
} | ||
|
||
return view( | ||
'livewire-powergrid::livewire.measurement-card', | ||
compact( | ||
'measurements', | ||
'averageQueriesTime', | ||
'averageRetrieveData', | ||
'config' | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace PowerComponents\LivewirePowerGrid\Recorders; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Illuminate\Config\Repository; | ||
use Laravel\Pulse\Pulse; | ||
use PowerComponents\LivewirePowerGrid\Events\MeasureRetrieveData; | ||
|
||
class PowerGridRecorder | ||
{ | ||
public string $listen = MeasureRetrieveData::class; | ||
|
||
public function __construct( | ||
protected Pulse $pulse, | ||
protected Repository $config | ||
) { | ||
} | ||
|
||
public function record(MeasureRetrieveData $class): void | ||
{ | ||
$now = CarbonImmutable::now(); | ||
|
||
$measurement = collect($class); | ||
|
||
$this->pulse->set( | ||
type: 'powergrid-measurements', | ||
key: uniqid(), | ||
value: $measurement, | ||
timestamp: $now->getTimestamp() | ||
); | ||
} | ||
} |