Skip to content

Commit 015b08b

Browse files
authored
Expanded speedtest results (#14)
1 parent bf78bbd commit 015b08b

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

app/Jobs/ExecSpeedtest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Foundation\Bus\Dispatchable;
1111
use Illuminate\Queue\InteractsWithQueue;
1212
use Illuminate\Queue\SerializesModels;
13+
use Illuminate\Support\Facades\Log;
1314
use Symfony\Component\Process\Exception\ProcessFailedException;
1415
use Symfony\Component\Process\Process;
1516

@@ -23,7 +24,8 @@ class ExecSpeedtest implements ShouldQueue
2324
* @return void
2425
*/
2526
public function __construct(
26-
public array|null $speedtest = null
27+
public array|null $speedtest = null,
28+
public bool $scheduled = false
2729
) {}
2830

2931
/**
@@ -54,11 +56,24 @@ public function handle()
5456
return 0;
5557
}
5658

57-
$output = $process->getOutput();
59+
try {
60+
$output = $process->getOutput();
61+
$results = json_decode($output, true);
5862

59-
Result::create([
60-
'data' => $output,
61-
]);
63+
Result::create([
64+
'ping' => $results['ping']['latency'],
65+
'download' => $results['download']['bandwidth'],
66+
'upload' => $results['upload']['bandwidth'],
67+
'server_id' => $results['server']['id'],
68+
'server_name' => $results['server']['name'],
69+
'server_host' => $results['server']['host'] . ':' . $results['server']['port'],
70+
'url' => $results['result']['url'],
71+
'scheduled' => $this->scheduled,
72+
'data' => $output,
73+
]);
74+
} catch (\Exception $e) {
75+
Log::error($e->getMessage());
76+
}
6277

6378
return 0;
6479
}

app/Jobs/SearchForSpeedtests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function handle()
4848
$cron = new CronExpression($speedtest['schedule']);
4949

5050
if ($cron->isDue() && $speedtest['enabled']) {
51-
ExecSpeedtest::dispatch(speedtest: $speedtest);
51+
ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: true);
5252
}
5353
}
5454
}

app/Models/Result.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ class Result extends Model
2222
* @var array
2323
*/
2424
protected $fillable = [
25+
'ping',
26+
'download',
27+
'upload',
28+
'server_id',
29+
'server_host',
30+
'server_name',
31+
'url',
32+
'scheduled',
2533
'data',
2634
];
35+
36+
/**
37+
* The attributes that should be cast.
38+
*
39+
* @var array
40+
*/
41+
protected $casts = [
42+
'scheduled' => 'boolean',
43+
'created_at' => 'datetime',
44+
];
2745
}

database/factories/ResultFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ class ResultFactory extends Factory
1717
public function definition()
1818
{
1919
return [
20-
//
20+
'ping' => fake()->randomFloat(2, 0, 100),
21+
'download' => fake()->randomNumber(),
22+
'upload' => fake()->randomNumber(),
23+
'server_id' => fake()->randomNumber(5, true),
24+
'server_host' => fake()->url(),
25+
'server_name' => fake()->word(),
26+
'url' => fake()->url(),
27+
'data' => json_encode(fake()->words()),
2128
];
2229
}
2330
}

database/migrations/2022_08_31_202106_create_results_table.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ public function up()
1515
{
1616
Schema::create('results', function (Blueprint $table) {
1717
$table->id();
18-
$table->json('data');
18+
$table->float('ping', 8, 3);
19+
$table->unsignedBigInteger('download'); // will be stored in bytes
20+
$table->unsignedBigInteger('upload'); // will be stored in bytes
21+
$table->integer('server_id')->nullable();
22+
$table->string('server_host')->nullable();
23+
$table->string('server_name')->nullable();
24+
$table->string('url')->nullable();
25+
$table->boolean('scheduled')->default(false);
26+
$table->json('data'); // is a dump of the cli output in case we want more fields later
1927
$table->timestamp('created_at')
2028
->useCurrent();
2129
});

0 commit comments

Comments
 (0)