Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component Monitoring #211

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('components', function (Blueprint $table) {
$table->boolean('checked')->default(false)->after('enabled');
$table->timestamp('checked_at')->nullable()->after('checked');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('components', function (Blueprint $table) {
$table->dropColumn([
'checked',
'checked_at',
]);
});
}
};
2 changes: 2 additions & 0 deletions resources/lang/en/component.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
'group' => 'Group',
'enabled' => 'Enabled',
'created_at' => 'Created at',
'checked_at' => 'Checked at',
'updated_at' => 'Updated at',
'deleted_at' => 'Deleted at',
],
@@ -27,6 +28,7 @@
'component_group_label' => 'Component Group',
'link_label' => 'Link',
'link_helper' => 'An optional link to the component.',
'checked_label' => 'Whether to periodically check the component.',
],
'status' => [
'operational' => 'Operational',
1 change: 1 addition & 0 deletions src/CachetCoreServiceProvider.php
Original file line number Diff line number Diff line change
@@ -169,6 +169,7 @@ private function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Commands\CheckComponentsCommand::class,
Commands\MakeUserCommand::class,
Commands\SendBeaconCommand::class,
Commands\VersionCommand::class,
79 changes: 79 additions & 0 deletions src/Commands/CheckComponentsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Cachet\Commands;

use Cachet\Cachet;
use Cachet\Enums\ComponentStatusEnum;
use Cachet\Models\Component;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

class CheckComponentsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cachet:check';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check the status of all components.';

/**
* Execute the console command.
*/
public function handle()
{
Component::query()
->enabled()
->checked()
->whereNotNull('link')
->get()
->each(function (Component $component) {
$attempts = 1;

try {
$response = Http::withUserAgent(Cachet::USER_AGENT)
->retry(3, function (int $attempt) use (&$attempts): int {
$attempts = $attempt;

return $attempt * 100;
})
->timeout(3)
->get($component->link);
} catch (RequestException|ConnectionException $e) {
$status = match (true) {
$e->getCode() >= 400 => ComponentStatusEnum::partial_outage,
$e->getCode() >= 500 => ComponentStatusEnum::major_outage,

Check failure on line 54 in src/Commands/CheckComponentsCommand.php

GitHub Actions / Static Analysis - P8.3 - L11.x - prefer-lowest

Comparison operation ">=" between int<min, 399> and 500 is always false.

Check failure on line 54 in src/Commands/CheckComponentsCommand.php

GitHub Actions / Static Analysis - P8.3 - L11.x - prefer-stable

Comparison operation ">=" between int<min, 399> and 500 is always false.

Check failure on line 54 in src/Commands/CheckComponentsCommand.php

GitHub Actions / Static Analysis - P8.2 - L11.x - prefer-lowest

Comparison operation ">=" between int<min, 399> and 500 is always false.

Check failure on line 54 in src/Commands/CheckComponentsCommand.php

GitHub Actions / Static Analysis - P8.2 - L11.x - prefer-stable

Comparison operation ">=" between int<min, 399> and 500 is always false.
default => ComponentStatusEnum::partial_outage,
};

$component->update([
'status' => $status,
'checked_at' => now(),
]);

return;
}

$status = match (true) {
$response->successful() && $attempts === 1 => ComponentStatusEnum::operational,
$response->successful() && $attempts > 1 => ComponentStatusEnum::performance_issues,
$response->status() >= 400 => ComponentStatusEnum::partial_outage,
default => ComponentStatusEnum::operational,
};

$component->update([
'status' => $status,
'checked_at' => now(),
]);
});
}
}
7 changes: 7 additions & 0 deletions src/Filament/Resources/ComponentResource.php
Original file line number Diff line number Diff line change
@@ -46,6 +46,8 @@ public static function form(Form $form): Form
->label(__('cachet::component.form.link_label'))
->url()
->label(__('cachet::component.form.link_helper')),
Forms\Components\Toggle::make('checked')
->label(__('cachet::component.form.checked_label')),
]),

Forms\Components\Section::make()->columns(2)->schema([
@@ -86,6 +88,11 @@ public static function table(Table $table): Table
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('checked_at')
->label(__('cachet::component.list.headers.checked_at'))
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->label(__('cachet::component.list.headers.updated_at'))
->dateTime()
14 changes: 14 additions & 0 deletions src/Models/Component.php
Original file line number Diff line number Diff line change
@@ -26,13 +26,16 @@
* @property ComponentStatusEnum $latest_status
* @property ?int $order
* @property ?int $component_group_id
* @property ?bool $checked
* @property ?Carbon $checked_at
* @property ?Carbon $created_at
* @property ?Carbon $updated_at
* @property ?Carbon $deleted_at
* @property bool $enabled
* @property array<string, mixed> $meta
* @property ?ComponentGroup $componentGroup
*
* @method static Builder<static>|static checked()
* @method static Builder<static>|static disabled()
* @method static Builder<static>|static enabled()
* @method static Builder<static>|static outage()
@@ -48,6 +51,7 @@ class Component extends Model

/** @var array<string, string> */
protected $casts = [
'checked' => 'bool',
'status' => ComponentStatusEnum::class,
'order' => 'int',
'enabled' => 'bool',
@@ -64,6 +68,8 @@ class Component extends Model
'component_group_id',
'enabled',
'meta',
'checked',
'checked_at',
];

protected $dispatchesEvents = [
@@ -101,6 +107,14 @@ public function subscribers(): BelongsToMany
return $this->belongsToMany(Subscriber::class, 'subscriptions');
}

/**
* Scope to checked components only.
*/
public function scopeChecked(Builder $query): void
{
$query->where('checked', true);
}

/**
* Scope to disabled components only.
*/
Loading