12-05-2026 Frontend dev
This commit is contained in:
parent
405df0a122
commit
5b8bdf4182
779 changed files with 480564 additions and 6241 deletions
78
app/Services/Admin/AdminRequestPerformanceMetrics.php
Normal file
78
app/Services/Admin/AdminRequestPerformanceMetrics.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Admin;
|
||||
|
||||
use Illuminate\Database\Events\QueryExecuted;
|
||||
|
||||
class AdminRequestPerformanceMetrics
|
||||
{
|
||||
private bool $active = false;
|
||||
|
||||
private int $queryCount = 0;
|
||||
|
||||
private float $databaseTimeMs = 0.0;
|
||||
|
||||
/**
|
||||
* @var list<array{sql: string, time_ms: float, connection: ?string}>
|
||||
*/
|
||||
private array $slowQueries = [];
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
$this->active = true;
|
||||
$this->queryCount = 0;
|
||||
$this->databaseTimeMs = 0.0;
|
||||
$this->slowQueries = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{database_time_ms: float, query_count: int, slow_queries: list<array{sql: string, time_ms: float, connection: ?string}>}
|
||||
*/
|
||||
public function snapshot(): array
|
||||
{
|
||||
return [
|
||||
'database_time_ms' => round($this->databaseTimeMs, 2),
|
||||
'query_count' => $this->queryCount,
|
||||
'slow_queries' => $this->slowQueries,
|
||||
];
|
||||
}
|
||||
|
||||
public function stop(): void
|
||||
{
|
||||
$this->active = false;
|
||||
}
|
||||
|
||||
public function record(QueryExecuted $query): void
|
||||
{
|
||||
if (! $this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->queryCount++;
|
||||
$this->databaseTimeMs += $query->time;
|
||||
|
||||
if ($query->time < $this->slowQueryThresholdMs()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (count($this->slowQueries) >= $this->maxSlowQueries()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->slowQueries[] = [
|
||||
'sql' => $query->sql,
|
||||
'time_ms' => round($query->time, 2),
|
||||
'connection' => $query->connectionName,
|
||||
];
|
||||
}
|
||||
|
||||
private function slowQueryThresholdMs(): int
|
||||
{
|
||||
return (int) config('admin_performance.slow_requests.slow_query_threshold_ms', 50);
|
||||
}
|
||||
|
||||
private function maxSlowQueries(): int
|
||||
{
|
||||
return max(0, (int) config('admin_performance.slow_requests.max_slow_queries', 5));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue