Skip to content

Commit f751def

Browse files
authored
Add dispatched_by field to results and update related logic (alexjustesen#2431)
Co-authored-by: Alex Justesen <[email protected]>
1 parent 07a2ada commit f751def

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

app/Actions/Ookla/RunSpeedtest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ class RunSpeedtest
2323
{
2424
use AsAction;
2525

26-
public function handle(bool $scheduled = false, ?int $serverId = null): mixed
26+
public function handle(bool $scheduled = false, ?int $serverId = null, ?int $dispatchedBy = null): mixed
2727
{
2828
$result = Result::create([
2929
'data->server->id' => $serverId,
3030
'service' => ResultService::Ookla,
3131
'status' => ResultStatus::Waiting,
3232
'scheduled' => $scheduled,
33+
'dispatched_by' => $dispatchedBy,
3334
]);
3435

3536
SpeedtestWaiting::dispatch($result);

app/Http/Controllers/Api/V1/SpeedtestController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __invoke(Request $request)
3838

3939
$result = RunSpeedtestAction::run(
4040
serverId: $request->input('server_id'),
41+
dispatchedBy: $request->user()->id,
4142
);
4243

4344
return $this->sendResponse(

app/Http/Resources/V1/ResultResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function toArray(Request $request): array
3434
'healthy' => $this->healthy,
3535
'status' => $this->status,
3636
'scheduled' => $this->scheduled,
37+
'dispatched_by' => $this->dispatched_by,
3738
'comments' => $this->comments,
3839
'data' => $this->data,
3940
'created_at' => $this->created_at->toDateTimestring(),

app/Livewire/Topbar/RunSpeedtestAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function speedtestAction(): Action
5353

5454
RunSpeedtest::run(
5555
serverId: $serverId,
56+
dispatchedBy: Auth::id(),
5657
);
5758

5859
Notification::make()

app/Models/Result.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Factories\HasFactory;
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Database\Eloquent\Prunable;
12+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1213

1314
class Result extends Model
1415
{
@@ -45,4 +46,12 @@ public function prunable(): Builder
4546
{
4647
return static::where('created_at', '<=', now()->subDays(config('speedtest.prune_results_older_than')));
4748
}
49+
50+
/**
51+
* Get the user who dispatched this speedtest.
52+
*/
53+
public function dispatchedBy(): BelongsTo
54+
{
55+
return $this->belongsTo(User::class, 'dispatched_by');
56+
}
4857
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('results', function (Blueprint $table) {
15+
$table->foreignId('dispatched_by')->nullable()->constrained('users')->nullOnDelete();
16+
});
17+
}
18+
};

0 commit comments

Comments
 (0)