Created
July 30, 2022 15:52
-
-
Save Alexisgt01/2444aafe16034ee7fb99a22fe26490dd to your computer and use it in GitHub Desktop.
Laravel Telescope - Custom prune entries by type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands\Telescope; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
use Laravel\Telescope\EntryType; | |
class CustomTelescopePrune extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'telescope:custom-prune'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Customize prune stale entries from the Telescope database by type'; | |
// Delete telescope data scoped by type with the number of hours to keep | |
protected array $types = [ | |
EntryType::BATCH => 24, | |
EntryType::CACHE => 24, | |
EntryType::CLIENT_REQUEST => 24, | |
EntryType::COMMAND => 24, | |
EntryType::DUMP => 24, | |
EntryType::EVENT => 24, | |
EntryType::GATE => 24, | |
EntryType::JOB => 24, | |
EntryType::LOG => 24, | |
EntryType::VIEW => 24, | |
EntryType::SCHEDULED_TASK => 24, | |
EntryType::REQUEST => 24, | |
EntryType::REDIS => 24, | |
EntryType::QUERY => 24, | |
EntryType::NOTIFICATION => 24, | |
EntryType::MODEL => 24, | |
EntryType::MAIL => 24, | |
EntryType::EXCEPTION => 24, | |
]; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
foreach ($this->types as $type => $hours) { | |
$this->info("{$this->prune($type, $hours)} entries pruned for $type type."); | |
} | |
} | |
public function prune(string $type, int $hours) | |
{ | |
$query = DB::table('telescope_entries')->where('created_at', '<', now()->subHours($hours))->where('type', $type); | |
$totalDeleted = 0; | |
do { | |
$deleted = $query->take(1000)->delete(); | |
$totalDeleted += $deleted; | |
} while ($deleted !== 0); | |
return $totalDeleted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment