Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions app/Jobs/ExecSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
) {}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/SearchForSpeedtests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
18 changes: 18 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
9 changes: 8 additions & 1 deletion database/factories/ResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down