Skip to content

Commit

Permalink
Merge branch '5.x' of github.com:Power-Components/livewire-powergrid …
Browse files Browse the repository at this point in the history
…into 5.x
  • Loading branch information
luanfreitasdev committed Feb 24, 2024
2 parents 4fab33f + 6ed7d0e commit dc1492d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 100 deletions.
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ parameters:
- './src/Components/Exports/OpenSpout/*'
- './src/Components/Actions/Macros.php'
- './src/Helpers/Actions.php'
- './src/Livewire/MeasurementCard.php'
- './src/Recorders/PowerGridRecorder.php'
- './src/Livewire/PerformanceCard.php'
- './src/Recorders/PowerGridPerformanceRecorder.php'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<x-pulse::scroll :expand="$expand">
<div class="grid grid-cols-1 gap-2">
@if ($measurements->isEmpty())
@if ($records->isEmpty())
<x-pulse::no-results />
@elseif (!$config['enabled'])
<div class="h-full flex flex-col items-center justify-center p-4">
Expand Down Expand Up @@ -53,26 +53,26 @@
</tr>
</x-pulse::thead>
<tbody>
@foreach ($measurements as $measurement)
@foreach ($records as $record)
<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 class="block text-xs text-gray-900 dark:text-gray-100 truncate" title="{{ data_get($record, 'tableName') }}">
{{ data_get($record, 'tableName') }}
</code>
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
{{ data_get($measurement, 'retrieveData') }} ms
{{ data_get($record, 'retrieveDataInMs') }} ms
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
{{ data_get($measurement, 'queriesTime') }} ms
{{ data_get($record, 'queriesTimeInMs') }} ms
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
{{ count(data_get($measurement, 'queries') ) }}
{{ count(data_get($record, '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'));
$createdAt = \Illuminate\Support\Carbon::createFromTimestamp(data_get($record, 'timestamp'));
@endphp
{{ $createdAt->diffForHumans() }}
</x-pulse::td>
Expand Down
22 changes: 0 additions & 22 deletions src/Events/MeasureRetrieveData.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Events/PowerGridPerformanceData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Events;

class PowerGridPerformanceData
{
/**
* @param string $tableName Name of the table where the data was retrieved.
* @param float $retrieveDataInMs Total time spent on the data retrieval operation.
* @param float $queriesTimeInMs Total time spent on executing queries.
* @param bool $isCached
* @param array $queries List of queries executed (query, binding, time).
*/
public function __construct(
public string $tableName,
public float $retrieveDataInMs,
public float $queriesTimeInMs = 0,
public bool $isCached = false,
public array $queries = [],
) {
}
}
53 changes: 0 additions & 53 deletions src/Livewire/MeasurementCard.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/Livewire/PerformanceCard.php
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\PowerGridPerformanceRecorder;

#[Lazy]
class PerformanceCard extends \Laravel\Pulse\Livewire\Card
{
public function render(): View
{
$config = Config::get('pulse.recorders.' . PowerGridPerformanceRecorder::class);

$averageRetrieveData = 0;
$averageQueriesTime = 0;
$records = collect();

if (data_get($config, 'enabled')) {
$records = \Laravel\Pulse\Facades\Pulse::values('powergrid-performance')
->map(function ($item) {
/** @var array $value */
$value = json_decode($item->value, true);
$item->tableName = $value['tableName'];
$item->retrieveDataInMs = $value['retrieveDataInMs'];
$item->queriesTimeInMs = $value['queriesTimeInMs'];
$item->isCached = $value['isCached'];
$item->queries = $value['queries'];
unset($item->value);

return $item;
})
->sort(fn ($a, $b) => $b->timestamp <=> $a->timestamp)
->take(10)
->values();

$averageRetrieveData = $records->avg(fn ($item) => $item->retrieveDataInMs);
$averageQueriesTime = $records->avg(fn ($item) => $item->queriesTimeInMs);
}

return view(
'livewire-powergrid::livewire.performance-card',
compact(
'records',
'averageQueriesTime',
'averageRetrieveData',
'config'
)
);
}
}
12 changes: 6 additions & 6 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Livewire\{Attributes\Computed, Component, WithPagination};

use PowerComponents\LivewirePowerGrid\Events\MeasureRetrieveData;
use PowerComponents\LivewirePowerGrid\Events\PowerGridPerformanceData;

use PowerComponents\LivewirePowerGrid\Themes\ThemeBase;
use Throwable;
Expand Down Expand Up @@ -171,10 +171,10 @@ protected function getCachedData(): mixed
$queriesTime = collect($queries)->sum('time');

app(Dispatcher::class)->dispatch(
new MeasureRetrieveData(
new PowerGridPerformanceData(
$this->tableName,
retrieveData: $retrieveData,
queriesTime: $queriesTime,
retrieveDataInMs: $retrieveData,
queriesTimeInMs: $queriesTime,
queries: $queries,
)
);
Expand All @@ -201,10 +201,10 @@ protected function getCachedData(): mixed
$time = round((microtime(true) - $start) * 1000);

app(Dispatcher::class)->dispatch(
new MeasureRetrieveData(
new PowerGridPerformanceData(
$this->tableName,
$time,
cached: true,
isCached: true,
)
);

Expand Down
4 changes: 2 additions & 2 deletions src/Providers/PowerGridServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PowerComponents\LivewirePowerGrid\Components\Filters\FilterManager;
use PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager;
use PowerComponents\LivewirePowerGrid\Themes\ThemeManager;
use PowerComponents\LivewirePowerGrid\{Livewire\LazyChild, Livewire\MeasurementCard, PowerGridManager};
use PowerComponents\LivewirePowerGrid\{Livewire\LazyChild, Livewire\PerformanceCard, PowerGridManager};

/** @codeCoverageIgnore */
class PowerGridServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -57,7 +57,7 @@ public function register(): void
Livewire::component('lazy-child', LazyChild::class);

if (class_exists(\Laravel\Pulse\Facades\Pulse::class)) {
Livewire::component('powergrid-measurement-card', MeasurementCard::class);
Livewire::component('powergrid-performance-card', PerformanceCard::class);
}

Macros::boot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
use Carbon\CarbonImmutable;
use Illuminate\Config\Repository;
use Laravel\Pulse\Pulse;
use PowerComponents\LivewirePowerGrid\Events\MeasureRetrieveData;
use PowerComponents\LivewirePowerGrid\Events\PowerGridPerformanceData;

class PowerGridRecorder
class PowerGridPerformanceRecorder
{
public string $listen = MeasureRetrieveData::class;
public string $listen = PowerGridPerformanceData::class;

public function __construct(
protected Pulse $pulse,
protected Repository $config
) {
}

public function record(MeasureRetrieveData $class): void
public function record(PowerGridPerformanceData $class): void
{
$now = CarbonImmutable::now();

$measurement = collect($class);
$data = collect($class);

$this->pulse->set(
type: 'powergrid-measurements',
type: 'powergrid-performance',
key: uniqid(),
value: $measurement,
value: $data,
timestamp: $now->getTimestamp()
);
}
Expand Down

0 comments on commit dc1492d

Please sign in to comment.