Skip to content

Commit

Permalink
Add ability to cleanup old runs from database.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritvars Zvejnieks committed May 30, 2023
1 parent b83a4ef commit ed59ded
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ return [
'exclude_routes' => env('XHGUI_ROUTE_EXCLUDE', ''),
],

// If you want to clean up profiling data after a certain amount of days
// set the appropriate database connection name here (make sure it's configured in config/database.php)
// set the results table name and the number of days after which the data should be deleted (0 won't delete).
// Setting the connection to null will disable the cleanup as well.
// If using mongoDB, ignore this and @see https://github.com/perftools/xhgui#limiting-mongodb-disk-usage
'database' => [
'connection' => env('XHGUI_DB_CONNECTION', null),
'table_results' => env('XHGUI_TABLE', 'results'),
'expire_after_days' => env('XHGUI_EXPIRE_AFTER_DAYS', 0),
],

// This is the configuration for the perftools/php-profiler. See the README for more information.
// Note that calling config('xhgui.<key>') will not work for these values.
// If you must, you can use config('xhgui')['<key>.<key>'].
Expand Down
11 changes: 11 additions & 0 deletions config/xhgui.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
'exclude_routes' => env('XHGUI_ROUTE_EXCLUDE', ''),
],

// If you want to clean up profiling data after a certain amount of days
// set the appropriate database connection name here (make sure it's configured in config/database.php)
// set the results table name and the number of days after which the data should be deleted (0 won't delete).
// Setting the connection to null will disable the cleanup.
// If using mongoDB, ignore this and @see https://github.com/perftools/xhgui#limiting-mongodb-disk-usage
'database' => [
'connection' => env('XHGUI_DB_CONNECTION', null),
'table_results' => env('XHGUI_TABLE', 'results'),
'expire_after_days' => env('XHGUI_EXPIRE_AFTER_DAYS', 0),
],

// This is the configuration for the perftools/php-profiler. See the README for more information.
// Note that calling config('xhgui.<key>') will not work for these values.
// If you must, you can use config('xhgui')['<key>.<key>'].
Expand Down
23 changes: 23 additions & 0 deletions src/Actions/ExpireRuns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Ritvarsz\LaravelXhgui\Actions;

use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;

class ExpireRuns
{
public function handle() {
$config = config('xhgui.database');
$connection = $config['connection'] ?? null;
$tableResults = $config['table_results'] ?? null;
$expireAfterDays = $config['expire_after_days'] ?? 0;

if ($expireAfterDays === 0 || $connection === null) {
return;
}

$expireDate = Date::today()->subDays($expireAfterDays)->toDateTimeString();

DB::connection($connection)->table($tableResults)->where('request_date', '<', $expireDate)->delete();
}
}
12 changes: 12 additions & 0 deletions src/XHGuiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Ritvarsz\LaravelXhgui;

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Ritvarsz\LaravelXhgui\Actions\ExpireRuns;
use Ritvarsz\LaravelXhgui\XHGuiMiddleware;

class XHGuiServiceProvider extends ServiceProvider
Expand All @@ -17,12 +19,22 @@ public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/xhgui.php' => config_path('xhgui.php')], 'xhgui-config');

return;
}

if (!config('xhgui.enabled')) {
return;
}

// Cleanup old runs
try {
$this->app->make(ExpireRuns::class)->handle();
} catch (\Exception $e) {
Log::error('Failed to cleanup old XHGui runs: ' . $e->getMessage());
}

// Add the middleware
$kernel = $this->app[Kernel::class];
$kernel->prependMiddleware(XHGuiMiddleware::class);
}
Expand Down

0 comments on commit ed59ded

Please sign in to comment.