Skip to content

Commit

Permalink
Closes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
csavelief committed May 21, 2024
1 parent 45a9668 commit 39e5c4a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/Enums/ServerStatusEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Enums;

enum ServerStatusEnum: string
{
case RUNNING = 'running';
case UNKNOWN = 'unknown';
case DOWN = 'down';
}
39 changes: 39 additions & 0 deletions app/Models/YnhServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Enums\ServerStatusEnum;
use App\Enums\SshTraceStateEnum;
use App\Hashing\TwHasher;
use App\Helpers\AdversaryMeter;
Expand Down Expand Up @@ -133,6 +134,44 @@ public function domain(): ?YnhDomain
return $this->domains->where('is_principal', true)->first();
}

public function status(): ServerStatusEnum
{
if (!$this->isReady()) {
return ServerStatusEnum::DOWN;
}

// Check if status is running
$minDate = Carbon::today()->subMinutes(10);
$isRunning = collect(DB::select("
SELECT COUNT(*) AS count
FROM ynh_osquery
WHERE ynh_server_id = {$this->id}
-- AND name IN ('memory_available_snapshot', 'disk_available_snapshot')
AND calendar_time >= '{$minDate->toDateString()}'
"))->first();

if ($isRunning->count > 0) {
return ServerStatusEnum::RUNNING;
}

// Check if status is unknown
$minDate = $minDate->subMinutes(10);
$isUnknown = collect(DB::select("
SELECT COUNT(*) AS count
FROM ynh_osquery
WHERE ynh_server_id = {$this->id}
-- AND name IN ('memory_available_snapshot', 'disk_available_snapshot')
AND calendar_time >= '{$minDate->toDateString()}'
"))->first();

if ($isUnknown->count > 0) {
return ServerStatusEnum::UNKNOWN;
}

// Here, the server is probably down :-(
return ServerStatusEnum::DOWN;
}

public function sshKeyPair(): SshKeyPair
{
$keys = new SshKeyPair();
Expand Down
8 changes: 7 additions & 1 deletion resources/views/home/cards/_servers.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@
@foreach($servers->sortBy('name') as $server)
<tr>
<td class="ps-4" width="25px">
<span class="{{ $server->is_ready ? 'tw-dot-green' : 'tw-dot-red' }}"></span>
@if($server->status() === \App\Enums\ServerStatusEnum::RUNNING)
<span class="tw-dot-green"></span>
@elseif($server->status() === \App\Enums\ServerStatusEnum::UNKNOWN)
<span class="tw-dot-orange"></span>
@else
<span class="tw-dot-red"></span>
@endif
</td>
<td>
<span class="font-lg mb-3 fw-bold">
Expand Down

0 comments on commit 39e5c4a

Please sign in to comment.