diff --git a/app/Jobs/ExecSpeedtest.php b/app/Jobs/ExecSpeedtest.php index 90f42966a..43e1b10a6 100644 --- a/app/Jobs/ExecSpeedtest.php +++ b/app/Jobs/ExecSpeedtest.php @@ -10,6 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Log; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; @@ -23,7 +24,8 @@ class ExecSpeedtest implements ShouldQueue * @return void */ public function __construct( - public array|null $speedtest = null + public array|null $speedtest = null, + public bool $scheduled = false ) {} /** @@ -54,11 +56,24 @@ public function handle() return 0; } - $output = $process->getOutput(); + try { + $output = $process->getOutput(); + $results = json_decode($output, true); - Result::create([ - 'data' => $output, - ]); + Result::create([ + 'ping' => $results['ping']['latency'], + 'download' => $results['download']['bandwidth'], + 'upload' => $results['upload']['bandwidth'], + 'server_id' => $results['server']['id'], + 'server_name' => $results['server']['name'], + 'server_host' => $results['server']['host'] . ':' . $results['server']['port'], + 'url' => $results['result']['url'], + 'scheduled' => $this->scheduled, + 'data' => $output, + ]); + } catch (\Exception $e) { + Log::error($e->getMessage()); + } return 0; } diff --git a/app/Jobs/SearchForSpeedtests.php b/app/Jobs/SearchForSpeedtests.php index 86e395689..07b42b67e 100644 --- a/app/Jobs/SearchForSpeedtests.php +++ b/app/Jobs/SearchForSpeedtests.php @@ -48,7 +48,7 @@ public function handle() $cron = new CronExpression($speedtest['schedule']); if ($cron->isDue() && $speedtest['enabled']) { - ExecSpeedtest::dispatch(speedtest: $speedtest); + ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: true); } } } diff --git a/app/Models/Result.php b/app/Models/Result.php index 0a514a180..c983243ba 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -22,6 +22,24 @@ class Result extends Model * @var array */ protected $fillable = [ + 'ping', + 'download', + 'upload', + 'server_id', + 'server_host', + 'server_name', + 'url', + 'scheduled', 'data', ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'scheduled' => 'boolean', + 'created_at' => 'datetime', + ]; } diff --git a/database/factories/ResultFactory.php b/database/factories/ResultFactory.php index c6b23a6fc..272218557 100644 --- a/database/factories/ResultFactory.php +++ b/database/factories/ResultFactory.php @@ -17,7 +17,14 @@ class ResultFactory extends Factory public function definition() { return [ - // + 'ping' => fake()->randomFloat(2, 0, 100), + 'download' => fake()->randomNumber(), + 'upload' => fake()->randomNumber(), + 'server_id' => fake()->randomNumber(5, true), + 'server_host' => fake()->url(), + 'server_name' => fake()->word(), + 'url' => fake()->url(), + 'data' => json_encode(fake()->words()), ]; } } diff --git a/database/migrations/2022_08_31_202106_create_results_table.php b/database/migrations/2022_08_31_202106_create_results_table.php index 9f3acb492..ad01434dc 100644 --- a/database/migrations/2022_08_31_202106_create_results_table.php +++ b/database/migrations/2022_08_31_202106_create_results_table.php @@ -15,7 +15,15 @@ public function up() { Schema::create('results', function (Blueprint $table) { $table->id(); - $table->json('data'); + $table->float('ping', 8, 3); + $table->unsignedBigInteger('download'); // will be stored in bytes + $table->unsignedBigInteger('upload'); // will be stored in bytes + $table->integer('server_id')->nullable(); + $table->string('server_host')->nullable(); + $table->string('server_name')->nullable(); + $table->string('url')->nullable(); + $table->boolean('scheduled')->default(false); + $table->json('data'); // is a dump of the cli output in case we want more fields later $table->timestamp('created_at') ->useCurrent(); });