flexisip-account-manager/flexiapi/app/Console/Commands/ClearStatistics.php
Timothée Jaussoin 396f420aac Fix FLEXIAPI-406 Add an artisan console script to clear statistics after n days
Add it in the daily crons, with 30 days delay
2025-11-19 16:17:21 +01:00

47 lines
1.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\StatisticsCall;
use App\StatisticsMessage;
use Carbon\Carbon;
use Illuminate\Console\Command;
class ClearStatistics extends Command
{
protected $signature = 'app:clear-statistics {days} {--apply}';
protected $description = 'Command description';
public function handle()
{
$calls = StatisticsCall::where(
'created_at',
'<',
Carbon::now()->subDays($this->argument('days'))->toDateTimeString()
);
$messages = StatisticsMessage::where(
'created_at',
'<',
Carbon::now()->subDays($this->argument('days'))->toDateTimeString()
);
$callsCount = $calls->count();
$messagesCount = $messages->count();
if ($this->option('apply')) {
$this->info($callsCount . ' calls statistics in deletion…');
$calls->delete();
$this->info($callsCount . ' calls statistics deleted');
$this->info($messagesCount . ' messages statistics in deletion…');
$messages->delete();
$this->info($messagesCount . ' messages statistics deleted');
return Command::SUCCESS;
}
$this->info($callsCount . ' calls statistics to delete');
$this->info($messagesCount . ' messages statistics to delete');
return Command::SUCCESS;
}
}