generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e6a4cd
commit 35f552c
Showing
7 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace OhDear\OhDearPulse\Checks; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
abstract class Check | ||
{ | ||
protected ?string $name = null; | ||
|
||
protected ?string $label = null; | ||
|
||
public static function new(): static | ||
{ | ||
return app(static::class); | ||
} | ||
|
||
public function name(string $name): static | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function label(string $label): static | ||
{ | ||
$this->label = $label; | ||
|
||
return $this; | ||
} | ||
|
||
public function getLabel(): string | ||
{ | ||
if ($this->label) { | ||
return $this->label; | ||
} | ||
|
||
$name = $this->getName(); | ||
|
||
return Str::of($name)->snake()->replace('_', ' ')->title(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
if ($this->name) { | ||
return $this->name; | ||
} | ||
|
||
$baseName = class_basename(static::class); | ||
|
||
return Str::of($baseName)->beforeLast('Check'); | ||
} | ||
|
||
abstract public function run(): Result; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace OhDear\OhDearPulse\Checks\Checks; | ||
|
||
use Laravel\Pulse\Facades\Pulse; | ||
use OhDear\OhDearPulse\Checks\Check; | ||
use OhDear\OhDearPulse\Checks\Result; | ||
use Spatie\Regex\Regex; | ||
use Symfony\Component\Process\Process; | ||
|
||
class UsedDiskSpaceCheck extends Check | ||
{ | ||
protected int $warningThreshold = 70; | ||
|
||
protected int $errorThreshold = 90; | ||
|
||
protected ?string $filesystemName = null; | ||
|
||
public function filesystemName(string $filesystemName): self | ||
{ | ||
$this->filesystemName = $filesystemName; | ||
|
||
return $this; | ||
} | ||
|
||
public function warnWhenUsedSpaceIsAbovePercentage(int $percentage): self | ||
{ | ||
$this->warningThreshold = $percentage; | ||
|
||
return $this; | ||
} | ||
|
||
public function failWhenUsedSpaceIsAbovePercentage(int $percentage): self | ||
{ | ||
$this->errorThreshold = $percentage; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
$diskSpaceUsedPercentage = $this->getDiskUsagePercentage(); | ||
|
||
$result = Result::make() | ||
->meta(['disk_space_used_percentage' => $diskSpaceUsedPercentage]) | ||
->shortSummary($diskSpaceUsedPercentage.'%'); | ||
|
||
if ($diskSpaceUsedPercentage > $this->errorThreshold) { | ||
return $result->failed("The disk is almost full ({$diskSpaceUsedPercentage}% used)."); | ||
} | ||
|
||
if ($diskSpaceUsedPercentage > $this->warningThreshold) { | ||
return $result->warning("The disk is almost full ({$diskSpaceUsedPercentage}% used)."); | ||
} | ||
|
||
return $result->ok(); | ||
} | ||
|
||
protected function getDiskUsagePercentage(): int | ||
{ | ||
$firstServer = Pulse::values('system')->first(); | ||
|
||
$values = json_decode($firstServer->value, flags: JSON_THROW_ON_ERROR); | ||
|
||
dd($values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace OhDear\OhDearPulse\Checks; | ||
|
||
use Carbon\CarbonInterface; | ||
use Illuminate\Support\Str; | ||
use OhDear\OhDearPulse\Enums\Status; | ||
|
||
class Result | ||
{ | ||
/** @var array<string, string|int|bool> */ | ||
public array $meta = []; | ||
|
||
public Check $check; | ||
|
||
public ?CarbonInterface $ended_at; | ||
|
||
public static function make(string $message = ''): self | ||
{ | ||
return new self(Status::Ok, $message); | ||
} | ||
|
||
public function __construct( | ||
public Status $status, | ||
public string $notificationMessage = '', | ||
public string $shortSummary = '', | ||
) { | ||
} | ||
|
||
public function shortSummary(string $shortSummary): self | ||
{ | ||
$this->shortSummary = $shortSummary; | ||
|
||
return $this; | ||
} | ||
|
||
public function getShortSummary(): string | ||
{ | ||
if (! empty($this->shortSummary)) { | ||
return $this->shortSummary; | ||
} | ||
|
||
return Str::of($this->status->value)->snake()->replace('_', ' ')->title(); | ||
} | ||
|
||
public function check(Check $check): self | ||
{ | ||
$this->check = $check; | ||
|
||
return $this; | ||
} | ||
|
||
public function notificationMessage(string $notificationMessage): self | ||
{ | ||
$this->notificationMessage = $notificationMessage; | ||
|
||
return $this; | ||
} | ||
|
||
public function getNotificationMessage(): string | ||
{ | ||
$meta = collect($this->meta) | ||
->filter(function ($item) { | ||
return is_scalar($item); | ||
})->toArray(); | ||
|
||
return trans($this->notificationMessage, $meta); | ||
} | ||
|
||
public function ok(string $message = ''): self | ||
{ | ||
$this->notificationMessage = $message; | ||
|
||
$this->status = Status::Ok; | ||
|
||
return $this; | ||
} | ||
|
||
public function warning(string $message = ''): self | ||
{ | ||
$this->notificationMessage = $message; | ||
|
||
$this->status = Status::Warning; | ||
|
||
return $this; | ||
} | ||
|
||
public function failed(string $message = ''): self | ||
{ | ||
$this->notificationMessage = $message; | ||
|
||
$this->status = Status::Failed; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<string, mixed> $meta */ | ||
public function meta(array $meta): self | ||
{ | ||
$this->meta = $meta; | ||
|
||
return $this; | ||
} | ||
|
||
public function endedAt(CarbonInterface $carbon): self | ||
{ | ||
$this->ended_at = $carbon; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace OhDear\OhDearPulse\Enums; | ||
|
||
enum Status: string | ||
{ | ||
case Ok = 'ok'; | ||
case Warning = 'warning'; | ||
case Failed = 'failed'; | ||
case Crashed = 'crashed'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,6 @@ public static function isConfigured() | |
{ | ||
return config('services.oh_dear.pulse.api_key') !== null; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters