Expanded speedtest results (#14)

This commit is contained in:
Alex Justesen
2022-09-14 15:38:01 -04:00
committed by GitHub
parent bf78bbde25
commit 015b08b48e
5 changed files with 56 additions and 8 deletions
+20 -5
View File
@@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
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 @@ class ExecSpeedtest implements ShouldQueue
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;
}
+1 -1
View File
@@ -48,7 +48,7 @@ class SearchForSpeedtests implements ShouldQueue
$cron = new CronExpression($speedtest['schedule']);
if ($cron->isDue() && $speedtest['enabled']) {
ExecSpeedtest::dispatch(speedtest: $speedtest);
ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: true);
}
}
}
+18
View File
@@ -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',
];
}
+8 -1
View File
@@ -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()),
];
}
}
@@ -15,7 +15,15 @@ return new class extends Migration
{
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();
});