laravel-websockets/src/Console/CleanStatistics.php

53 lines
1.3 KiB
PHP
Raw Normal View History

2018-12-04 10:48:07 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Console;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class CleanStatistics extends Command
{
2020-08-18 17:21:22 +00:00
/**
* The name and signature of the console command.
*
* @var string
*/
2018-12-04 10:48:07 +00:00
protected $signature = 'websockets:clean
2020-08-18 17:21:22 +00:00
{appId? : (optional) The app id that will be cleaned.}
';
/**
* The console command description.
*
* @var string|null
*/
2018-12-04 10:48:07 +00:00
protected $description = 'Clean up old statistics from the websocket log.';
2020-08-18 17:21:22 +00:00
/**
* Run the command.
*
* @return void
*/
2018-12-04 10:48:07 +00:00
public function handle()
{
$this->comment('Cleaning WebSocket Statistics...');
$appId = $this->argument('appId');
$maxAgeInDays = config('websockets.statistics.delete_statistics_older_than_days');
$cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
2020-08-18 17:21:22 +00:00
$class = config('websockets.statistics.model');
2018-12-04 10:48:07 +00:00
2020-08-18 17:21:22 +00:00
$amountDeleted = $class::where('created_at', '<', $cutOffDate)
2018-12-04 10:48:07 +00:00
->when(! is_null($appId), function (Builder $query) use ($appId) {
$query->where('app_id', $appId);
})
->delete();
$this->info("Deleted {$amountDeleted} record(s) from the WebSocket statistics.");
}
2018-12-04 21:22:33 +00:00
}