Skip to content
Snippets Groups Projects
Verified Commit c446a55f authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

feat: add user agent processing functionality

- Create `ProcessUserAgentJob` for detecting if a user agent is a bot and logging the result.
- Implement `ProcessUserAgentsCommand` to handle batch processing of user agents from the database.
parent 27b7fb69
No related branches found
No related tags found
1 merge request!59Ajouter service ia & détection bots user agents
Pipeline #1031 passed
<?php
namespace App\Console\Commands;
use App\Jobs\ProcessUserAgentJob;
use Illuminate\Console\Command;
use SlProjects\LaravelRequestLogger\app\Models\UserAgent;
class ProcessUserAgentsCommand extends Command
{
protected $signature = 'process:user-agents';
protected $description = 'Process user agents to detect if they are bots';
public function handle(): void
{
$userAgents = UserAgent::join('user_agent_metadata', 'user_agents.id', '=', 'user_agent_metadata.user_agent_id')
->whereNull('user_agent_metadata.is_bot')
->select('user_agents.id', 'user_agents.user_agent')
->get();
if ($userAgents->isEmpty()) {
$this->info('No user agents to process.');
return;
}
foreach ($userAgents as $userAgent) {
ProcessUserAgentJob::dispatch($userAgent);
}
$this->info('Jobs dispatched for processing '.$userAgents->count().' user agents.');
}
}
<?php
namespace App\Jobs;
use App\Models\UserAgentMetadata;
use App\Services\AiProviderService;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use SlProjects\LaravelRequestLogger\app\Models\UserAgent;
class ProcessUserAgentJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(private readonly UserAgent $userAgent) {}
public function handle(AiProviderService $aiProviderService): void {
try {
$result = $aiProviderService->prompt(
'You are a bot detector designed to output JSON. ',
"Is this user agent a bot? Please respond in the format {'is_bot': true/false}. The user agent is: {$this->userAgent->user_agent}"
);
$isBot = $result['is_bot'] ?? false;
UserAgentMetadata::create([
'user_agent_id' => $this->userAgent->id,
'is_bot' => $isBot,
]);
Log::info("UserAgent processed: {$this->userAgent->user_agent}, is_bot: {$isBot}");
} catch (Exception $e) {
Log::error("Failed to process UserAgent: {$this->userAgent->user_agent}", [
'exception' => $e->getMessage(),
]);
$this->fail($e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment