Skip to content

Commit c6de745

Browse files
committed
Implement status API endpoint
1 parent 79b153a commit c6de745

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

resources/views/components/status-bar.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<div {{ $attributes->class(['rounded-md p-4 bg-custom-200 dark:bg-custom-400 border border-custom-400'])->style([
22
Illuminate\Support\Arr::toCssStyles([
33
\Filament\Support\get_color_css_variables(
4-
$color,
4+
$status->getColor(),
55
shades: [100, 200, 400, 800],
66
),
77
]),
88
]) }}>
99
<div class="flex sm:items-center gap-3">
1010
@svg($status->getIcon(), 'size-5 text-custom-800')
1111
<div class="flex flex-1 justify-between items-center">
12-
<p class="text-sm md:text-base text-custom-800">{{ $label }}</p>
12+
<p class="text-sm md:text-base text-custom-800">{{ $status->getLabel() }}</p>
1313
{{-- <p class="mt-2 sm:mt-3 text-xs sm:text-sm md:ml-6 md:mt-0 text-custom-800">--}}
1414
{{-- Updated <time datetime="2008-02-14 20:00" title="2008-02-14 20:00" class="underline decoration-dotted underline-offset-2 cursor-help">10 minutes</time> ago--}}
1515
{{-- </p>--}}

routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cachet\Http\Controllers\Api\MetricController;
1010
use Cachet\Http\Controllers\Api\MetricPointController;
1111
use Cachet\Http\Controllers\Api\ScheduleController;
12+
use Cachet\Http\Controllers\Api\StatusController;
1213
use Illuminate\Support\Facades\Route;
1314

1415
Route::apiResources([
@@ -30,3 +31,4 @@
3031

3132
Route::get('/ping', [GeneralController::class, 'ping'])->name('ping');
3233
Route::get('/version', [GeneralController::class, 'version'])->name('version');
34+
Route::get('/status', StatusController::class)->name('status');

src/Http/Controllers/Api/GeneralController.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,4 @@ public function version(): JsonResponse
2727
],
2828
]);
2929
}
30-
31-
/**
32-
* Get the Cachet status and message.
33-
*/
34-
public function status(): JsonResponse
35-
{
36-
return response()->json([
37-
'data' => [
38-
// 'status' => Cachet::status(),
39-
// 'message' => Cachet::statusMessage(),
40-
],
41-
]);
42-
}
4330
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Cachet\Http\Controllers\Api;
4+
5+
use Cachet\Status;
6+
7+
class StatusController
8+
{
9+
/**
10+
* Get the current system status.
11+
*/
12+
public function __invoke(Status $status)
13+
{
14+
return response()->json([
15+
'data' => [
16+
'status' => $status->current()->name,
17+
'message' => $status->current()->getLabel(),
18+
],
19+
]);
20+
}
21+
}

src/Status.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,25 @@ class Status
1717
protected ?object $incidents = null;
1818

1919
/**
20-
* @return array{status: string, label: string, color: string}
20+
* Get the current system status as an enum.
2121
*/
22-
public function current(): array
22+
public function current(): SystemStatusEnum
2323
{
2424
$components = $this->components();
2525

2626
if ($this->majorOutage()) {
27-
return [
28-
'status' => SystemStatusEnum::major_outage,
29-
'label' => SystemStatusEnum::major_outage->getLabel(),
30-
'color' => SystemStatusEnum::major_outage->getColor(),
31-
];
27+
return SystemStatusEnum::major_outage;
3228
}
3329

3430
if ((int) $components->total - (int) $components->operational === 0) {
3531
$incidents = $this->incidents();
3632

3733
if ((int) $incidents->total === 0 || ((int) $incidents->total > 0 && (int) $incidents->unresolved === 0)) {
38-
return [
39-
'status' => SystemStatusEnum::operational,
40-
'label' => SystemStatusEnum::operational->getLabel(),
41-
'color' => SystemStatusEnum::operational->getColor(),
42-
];
34+
return SystemStatusEnum::operational;
4335
}
4436
}
4537

46-
return [
47-
'status' => SystemStatusEnum::partial_outage,
48-
'label' => SystemStatusEnum::partial_outage->getLabel(),
49-
'color' => SystemStatusEnum::partial_outage->getColor(),
50-
];
38+
return SystemStatusEnum::partial_outage;
5139
}
5240

5341
/**

src/View/Components/StatusBar.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99

1010
class StatusBar extends ViewComponent
1111
{
12+
/**
13+
* Create a new component instance.
14+
*/
15+
public function __construct(protected readonly Status $status)
16+
{
17+
//
18+
}
19+
1220
/**
1321
* Get the view / contents that represent the component.
1422
*/
1523
public function render(): View|Closure|string
1624
{
1725
return view('cachet::components.status-bar', [
18-
...app(Status::class)->current(),
26+
'status' => $this->status->current(),
1927
]);
2028
}
2129
}

0 commit comments

Comments
 (0)