2026-01-26 21:08:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-27 16:30:46 +00:00
|
|
|
namespace Core\Tenant\Console\Commands;
|
2026-01-26 21:08:59 +00:00
|
|
|
|
2026-01-27 16:30:46 +00:00
|
|
|
use Core\Tenant\Jobs\ComputeUserStats;
|
|
|
|
|
use Core\Tenant\Models\User;
|
2026-01-26 21:08:59 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
|
|
class RefreshUserStats extends Command
|
|
|
|
|
{
|
|
|
|
|
protected $signature = 'users:refresh-stats {--user= : Specific user ID to refresh}';
|
|
|
|
|
|
|
|
|
|
protected $description = 'Refresh cached stats for users';
|
|
|
|
|
|
|
|
|
|
public function handle(): int
|
|
|
|
|
{
|
|
|
|
|
if ($userId = $this->option('user')) {
|
|
|
|
|
$this->refreshUser($userId);
|
|
|
|
|
|
|
|
|
|
return Command::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Refresh all users with stale stats (> 1 hour old)
|
|
|
|
|
$staleUsers = User::where(function ($query) {
|
|
|
|
|
$query->whereNull('stats_computed_at')
|
|
|
|
|
->orWhere('stats_computed_at', '<', now()->subHour());
|
|
|
|
|
})->pluck('id');
|
|
|
|
|
|
|
|
|
|
$this->info("Queuing stats refresh for {$staleUsers->count()} users...");
|
|
|
|
|
|
|
|
|
|
foreach ($staleUsers as $userId) {
|
|
|
|
|
ComputeUserStats::dispatch($userId)->onQueue('stats');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->info('Done! Stats will be computed in background.');
|
|
|
|
|
|
|
|
|
|
return Command::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function refreshUser(int $userId): void
|
|
|
|
|
{
|
|
|
|
|
$user = User::find($userId);
|
|
|
|
|
|
|
|
|
|
if (! $user) {
|
|
|
|
|
$this->error("User {$userId} not found.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->info("Computing stats for user: {$user->name}...");
|
|
|
|
|
ComputeUserStats::dispatchSync($userId);
|
|
|
|
|
$this->info('Done!');
|
|
|
|
|
}
|
|
|
|
|
}
|