<?php

namespace App\Modules\Network\Commands;

use DB;
use Carbon\Carbon;

use App\Modules\Network\Models\Goliath\GoliathAttack;
use App\Modules\Network\Models\Goliath\GoliathAttackPrefix;
use App\Modules\Network\Models\Prefixes;
use App\Modules\Network\Helpers\ControllerCommons;

class GoliathMonitorAttack extends \App\Console\Commands\BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'goliath:monitor-attacks {--thread-pool=} {--hours=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Monitors goliath appliances to see whether the agents are active.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $threads = $this->option('thread-pool') ?? 2;
        $hours = $this->option('hours') ?? 1;

        $pipe = [];
            
        for ($i = 0; $i < $threads; ++$i) {
            $pipe[$i] = popen("php /var/www/html/creatia/artisan goliath:monitor-attacks-thread --id-max=$threads --id-mod=$i --hours=$hours", 'w');
        }
        
        for ($i = 0; $i < $threads; ++$i) {
            pclose($pipe[$i]);
        }
    
        return 0;
    }
}