Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 13, 2023
1 parent 8e6a4cd commit 35f552c
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^8.1",
"illuminate/contracts": "^10.0",
"laravel/pulse": "^1.0@beta",
"laravel/pulse": "1.x-dev",
"ohdearapp/ohdear-php-sdk": "^3.7",
"spatie/laravel-package-tools": "^1.14.0"
},
Expand Down
57 changes: 57 additions & 0 deletions src/Checks/Check.php
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;


}
67 changes: 67 additions & 0 deletions src/Checks/Checks/UsedDiskSpaceCheck.php
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);
}
}
111 changes: 111 additions & 0 deletions src/Checks/Result.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Enums/Status.php
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';
}
2 changes: 2 additions & 0 deletions src/OhDearPulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public static function isConfigured()
{
return config('services.oh_dear.pulse.api_key') !== null;
}


}
7 changes: 7 additions & 0 deletions src/OhDearPulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@ public function packageBooted()

return new OhDear(config('services.oh_dear.pulse.api_key'));
});

/*
OhDearPulse::monitor(
DiskSpaceCheck::new()
->warnIfTooHigh(80)
);
*/
}
}

0 comments on commit 35f552c

Please sign in to comment.