Skip to content

Commit

Permalink
Added Log Slow Query Threshold to Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MGeurts committed Dec 15, 2024
1 parent e62e3c5 commit 884c6c2
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 52 deletions.
33 changes: 13 additions & 20 deletions app/Livewire/Developer/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class Settings extends Component

public SettingsForm $settingsForm;

public bool $logAllQueries = false;

public bool $logAllQueriesSlow = false;

public bool $logAllQueriesNPlusOne = false;

/**
* Mount the component and load initial data.
*/
Expand All @@ -34,13 +28,10 @@ public function mount(): void
// Fetch settings and map them to the properties
$this->settings = Setting::pluck('value', 'key');

$this->settingsForm->logAllQueries = (bool) $this->settings->get('log_all_queries');
$this->settingsForm->logAllQueriesSlow = (bool) $this->settings->get('log_all_queries_slow');
$this->settingsForm->logAllQueriesNPlusOne = (bool) $this->settings->get('log_all_queries_nplusone');

$this->logAllQueries = (bool) $this->settings->get('log_all_queries');
$this->logAllQueriesSlow = (bool) $this->settings->get('log_all_queries_slow');
$this->logAllQueriesNPlusOne = (bool) $this->settings->get('log_all_queries_nplusone');
$this->settingsForm->logAllQueries = (bool) $this->settings->get('log_all_queries');
$this->settingsForm->logAllQueriesSlowThreshold = $this->settings->get('log_all_queries_slow_threshold');
$this->settingsForm->logAllQueriesSlow = (bool) $this->settings->get('log_all_queries_slow');
$this->settingsForm->logAllQueriesNPlusOne = (bool) $this->settings->get('log_all_queries_nplusone');
}

/**
Expand All @@ -49,13 +40,14 @@ public function mount(): void
public function saveSettings(): void
{
$settings = [
'log_all_queries' => $this->settingsForm->logAllQueries,
'log_all_queries_slow' => $this->settingsForm->logAllQueriesSlow,
'log_all_queries_nplusone' => $this->settingsForm->logAllQueriesNPlusOne,
'log_all_queries' => $this->settingsForm->logAllQueries,
'log_all_queries_slow' => $this->settingsForm->logAllQueriesSlow,
'log_all_queries_slow_threshold' => $this->settingsForm->logAllQueriesSlowThreshold,
'log_all_queries_nplusone' => $this->settingsForm->logAllQueriesNPlusOne,
];

foreach ($settings as $key => $value) {
Setting::updateOrCreate(['key' => $key], ['value' => $value ? 1 : 0]);
Setting::updateOrCreate(['key' => $key], ['value' => $value]);
}

// Clear the cache
Expand All @@ -72,9 +64,10 @@ public function resetSettings(): void
public function isDirty(): bool
{
return
$this->settingsForm->logAllQueries != $this->logAllQueries or
$this->settingsForm->logAllQueriesSlow != $this->logAllQueriesSlow or
$this->settingsForm->logAllQueriesNPlusOne != $this->logAllQueriesNPlusOned;
$this->settingsForm->logAllQueries != (bool) $this->settings->get('log_all_queries') or
$this->settingsForm->logAllQueriesSlow != $this->settings->get('log_all_queries_slow_threshold') or
$this->settingsForm->logAllQueriesSlowThreshold != $this->logAllQueriesSlowThreshold or (bool) $this->settings->get('log_all_queries_slow') or
$this->settingsForm->logAllQueriesNPlusOne != (bool) $this->settings->get('log_all_queries_nplusone');
}

/**
Expand Down
16 changes: 14 additions & 2 deletions app/Livewire/Forms/Developer/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ class SettingsForm extends Form

public bool $logAllQueriesSlow = false;

public string $logAllQueriesSlowThreshold = '500';

public bool $logAllQueriesNPlusOne = false;

// -----------------------------------------------------------------------
public function rules(): array
{
return $rules = [];
return $rules = [
'log_all_queries' => ['boolean'],
'log_all_queries_slow' => ['boolean'],
'log_all_queries_slow_threshold' => ['integer'],
'log_all_queries_nplusone' => ['boolean'],
];
}

public function messages(): array
Expand All @@ -28,6 +35,11 @@ public function messages(): array

public function validationAttributes(): array
{
return [];
return [
'log_all_queries' => __('settings.log_all_queries'),
'log_all_queries_slow' => __('settings.log_all_queries_slow'),
'log_all_queries_slow_threshold' => __('settings.log_all_queries_slow_threshold'),
'log_all_queries_nplusone' => __('settings.log_all_queries_nplusone'),
];
}
}
10 changes: 5 additions & 5 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ private function logAllQueries(): void
}

/**
* Log all slow queries (threshold in ms) for debugging purposes.
* Log all slow queries for debugging purposes.
*/
private function LogAllQueriesSlow(int $threshold = 500): void
private function LogAllQueriesSlow(): void
{
if (settings('log_all_queries_slow')) {
DB::listen(function ($query) use ($threshold) {
if ($query->time >= $threshold) {
Log::warning('An individual database query exceeded ' . $threshold . ' ms.', [
DB::listen(function ($query) {
if ($query->time >= settings('log_all_queries_slow_threshold')) {
Log::warning('An individual database query exceeded ' . settings('log_all_queries_slow_threshold') . ' ms.', [
'sql' => $query->sql,
'raw' => $query->toRawSQL(),
]);
Expand Down
1 change: 1 addition & 0 deletions database/seeders/SettingSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function run(): void
$settingsData = [
['key' => 'log_all_queries', 'value' => false],
['key' => 'log_all_queries_slow', 'value' => true],
['key' => 'log_all_queries_slow_threshold', 'value' => 500],
['key' => 'log_all_queries_nplusone', 'value' => true],
];

Expand Down
7 changes: 4 additions & 3 deletions lang/de/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Protokolliere ALLE Datenbankabfragen',
'log_all_queries_slow' => 'Protokolliere alle LANGSAMEN Datenbankabfragen',
'log_all_queries_nplusone' => 'Protokolliere alle (N+1)) Datenbankabfragen',
'log_all_queries' => 'Protokolliere ALLE Datenbankabfragen',
'log_all_queries_slow' => 'Protokolliere alle LANGSAMEN Datenbankabfragen',
'log_all_queries_slow_threshold' => 'Schwelle (in ms)',
'log_all_queries_nplusone' => 'Protokolliere alle (N+1)) Datenbankabfragen',
];
7 changes: 4 additions & 3 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Log ALL database queries',
'log_all_queries_slow' => 'Log all SLOW database queries',
'log_all_queries_nplusone' => 'Log all (N+1) database queries',
'log_all_queries' => 'Log ALL database queries',
'log_all_queries_slow' => 'Log all SLOW database queries',
'log_all_queries_slow_threshold' => 'Threshold (in ms)',
'log_all_queries_nplusone' => 'Log all (N+1) database queries',
];
7 changes: 4 additions & 3 deletions lang/es/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Registrar TODAS las consultas de la base de datos',
'log_all_queries_slow' => 'Registre todas las consultas de bases de datos LENTAS',
'log_all_queries_nplusone' => 'Registrar todas las consultas de la base de datos (N+1)',
'log_all_queries' => 'Registrar TODAS las consultas de la base de datos',
'log_all_queries_slow' => 'Registre todas las consultas de bases de datos LENTAS',
'log_all_queries_slow_threshold' => 'Límite (en ms)',
'log_all_queries_nplusone' => 'Registrar todas las consultas de la base de datos (N+1)',
];
7 changes: 4 additions & 3 deletions lang/fr/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Enregistrez TOUTES les requêtes de base de données',
'log_all_queries_slow' => 'Enregistrez toutes les requêtes de base de données LENTES',
'log_all_queries_nplusone' => 'Enregistrer toutes les requêtes de base de données (N+1)',
'log_all_queries' => 'Enregistrez TOUTES les requêtes de base de données',
'log_all_queries_slow' => 'Enregistrez toutes les requêtes de base de données LENTES',
'log_all_queries_slow_threshold' => 'Seuil (en ms)',
'log_all_queries_nplusone' => 'Enregistrer toutes les requêtes de base de données (N+1)',
];
7 changes: 4 additions & 3 deletions lang/nl/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Registreer ALLE databasequery\'s',
'log_all_queries_slow' => 'Registreer alle TRAGE databasequery\'s',
'log_all_queries_nplusone' => 'Registreer alle (N+1) databasequery\'s',
'log_all_queries' => 'Registreer ALLE databasequery\'s',
'log_all_queries_slow' => 'Registreer alle TRAGE databasequery\'s',
'log_all_queries_slow_threshold' => 'Drempelwaarde (in ms)',
'log_all_queries_nplusone' => 'Registreer alle (N+1) databasequery\'s',
];
7 changes: 4 additions & 3 deletions lang/pt/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Registrar TODAS as consultas do banco de dados',
'log_all_queries_slow' => 'Registrar todas as consultas LENTAS do banco de dados',
'log_all_queries_nplusone' => 'Registrar todas as consultas do banco de dados (N+1)',
'log_all_queries' => 'Registrar TODAS as consultas do banco de dados',
'log_all_queries_slow' => 'Registrar todas as consultas LENTAS do banco de dados',
'log_all_queries_slow_threshold' => 'Limite (em ms)',
'log_all_queries_nplusone' => 'Registrar todas as consultas do banco de dados (N+1)',
];
7 changes: 4 additions & 3 deletions lang/vi/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => 'Đăng nhập TẤT CẢ các truy vấn cơ sở dữ liệu',
'log_all_queries_slow' => 'Ghi lại tất cả các truy vấn cơ sở dữ liệu CHẬM',
'log_all_queries_nplusone' => 'Ghi nhật ký tất cả (N+1) truy vấn cơ sở dữ liệu',
'log_all_queries' => 'Đăng nhập TẤT CẢ các truy vấn cơ sở dữ liệu',
'log_all_queries_slow' => 'Ghi lại tất cả các truy vấn cơ sở dữ liệu CHẬM',
'log_all_queries_slow_threshold' => 'Ngưỡng (tính bằng mili giây)',
'log_all_queries_nplusone' => 'Ghi nhật ký tất cả (N+1) truy vấn cơ sở dữ liệu',
];
7 changes: 4 additions & 3 deletions lang/zh_cn/settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'log_all_queries' => '记录所有数据库查询',
'log_all_queries_slow' => '记录所有 SLOW 数据库查询',
'log_all_queries_nplusone' => 'L记录所有 (N+1) 数据库查询',
'log_all_queries' => '记录所有数据库查询',
'log_all_queries_slow' => '记录所有 SLOW 数据库查询',
'log_all_queries_slow_threshold' => '阈值(以毫秒为单位)',
'log_all_queries_nplusone' => 'L记录所有 (N+1) 数据库查询',
];
10 changes: 9 additions & 1 deletion resources/views/livewire/developer/settings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@
<x-ts-toggle wire:model="settingsForm.logAllQueries" label="{{ __('settings.log_all_queries') }} ?" />
</div>

<div class="col-span-6">
<x-hr.narrow class="col-span-6 !my-0" />

<div class="col-span-3">
<x-ts-toggle wire:model="settingsForm.logAllQueriesSlow" label="{{ __('settings.log_all_queries_slow') }} ?" />
</div>

<div class="col-span-3">
<x-ts-input type="number" min="1" wire:model="settingsForm.logAllQueriesSlowThreshold" label="{{ __('settings.log_all_queries_slow_threshold') }} :" />
</div>

<x-hr.narrow class="col-span-6 !my-0" />

<div class="col-span-6">
<x-ts-toggle wire:model="settingsForm.logAllQueriesNPlusOne" label="{{ __('settings.log_all_queries_nplusone') }} ?" />
</div>
Expand Down

0 comments on commit 884c6c2

Please sign in to comment.