Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor
  • Loading branch information
svenvg93 committed Nov 21, 2024
commit 13e45c1cd8a00aab2649dc0dd01acd4adeef80a2
87 changes: 87 additions & 0 deletions app/Jobs/CheckAndUpdateThresholds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Jobs;

use App\Helpers\Number;
use App\Models\Result;
use App\Settings\ThresholdSettings;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CheckAndUpdateThresholds
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $result;

/**
* Create a new job instance.
*/
public function __construct(Result $result)
{
$this->result = $result;
}

/**
* Execute the job.
*/
public function handle(): void
{
$thresholds = app(ThresholdSettings::class);

// Determine if thresholds are enabled
$thresholdsEnabled = $thresholds->absolute_enabled;

// Convert bits to Mbits if needed
$downloadInMbits = ! is_null($this->result->download)
? Number::bitsToMagnitude($this->result->download_bits, 2, 'mbit')
: null;
$uploadInMbits = ! is_null($this->result->upload)
? Number::bitsToMagnitude($this->result->upload_bits, 2, 'mbit')
: null;

// Only check thresholds if they are enabled and not set to 0
$downloadBreached = $thresholdsEnabled
&& $thresholds->absolute_download > 0
&& $downloadInMbits !== null
&& $downloadInMbits < $thresholds->absolute_download;

$uploadBreached = $thresholdsEnabled
&& $thresholds->absolute_upload > 0
&& $uploadInMbits !== null
&& $uploadInMbits < $thresholds->absolute_upload;

$pingBreached = $thresholdsEnabled
&& $thresholds->absolute_ping > 0
&& $this->result->ping !== null
&& $this->result->ping > $thresholds->absolute_ping;

// Calculate individual statuses
$downloadStatus = $thresholdsEnabled && $thresholds->absolute_download > 0
? ($downloadBreached ? 'Failed' : 'Passed')
: 'NotChecked';

$uploadStatus = $thresholdsEnabled && $thresholds->absolute_upload > 0
? ($uploadBreached ? 'Failed' : 'Passed')
: 'NotChecked';

$pingStatus = $thresholdsEnabled && $thresholds->absolute_ping > 0
? ($pingBreached ? 'Failed' : 'Passed')
: 'NotChecked';

// Calculate the overall status
$overallStatus = $thresholdsEnabled
? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed')
: 'NotChecked';

// Update all relevant fields in the database
$this->result->update([
'threshold_breached_overall' => $overallStatus,
'threshold_breached_download' => $downloadStatus,
'threshold_breached_upload' => $uploadStatus,
'threshold_breached_ping' => $pingStatus,
]);
}
}
3 changes: 2 additions & 1 deletion app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enums\ResultStatus;
use App\Events\SpeedtestCompleted;
use App\Events\SpeedtestFailed;
use App\Jobs\CheckAndUpdateThresholds;
use App\Models\Result;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function handle(): void
]);

// Ensure thresholds are checked and updated
$this->result->checkAndUpdateThresholds();
CheckAndUpdateThresholds::dispatch($this->result);

SpeedtestCompleted::dispatch($this->result);
}
Expand Down
37 changes: 0 additions & 37 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace App\Models;

use App\Enums\ResultStatus;
use App\Helpers\Number;
use App\Settings\ThresholdSettings;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -304,39 +302,4 @@ protected function uploadlatencyiqm(): Attribute
get: fn () => Arr::get($this->data, 'upload.latency.iqm'),
);
}

public function checkAndUpdateThresholds(): void
{
$thresholds = app(ThresholdSettings::class);

// Determine if thresholds are enabled
$thresholdsEnabled = $thresholds->absolute_enabled;

// Convert bits to Mbits if needed
$downloadInMbits = ! is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null;
$uploadInMbits = ! is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null;

// Determine if thresholds are breached or NotChecked
$downloadBreached = $thresholdsEnabled && $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download;
$uploadBreached = $thresholdsEnabled && $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload;
$pingBreached = $thresholdsEnabled && $this->ping !== null && $this->ping > $thresholds->absolute_ping;

// Calculate individual statuses
$downloadStatus = $thresholdsEnabled ? ($downloadBreached ? 'Failed' : 'Passed') : 'NotChecked';
$uploadStatus = $thresholdsEnabled ? ($uploadBreached ? 'Failed' : 'Passed') : 'NotChecked';
$pingStatus = $thresholdsEnabled ? ($pingBreached ? 'Failed' : 'Passed') : 'NotChecked';

// Calculate the overall status
$overallStatus = $thresholdsEnabled
? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed')
: 'NotChecked';

// Update all relevant fields in the database
$this->update([
'threshold_breached_overall' => $overallStatus,
'threshold_breached_download' => $downloadStatus,
'threshold_breached_upload' => $uploadStatus,
'threshold_breached_ping' => $pingStatus,
]);
}
}