mailwolt/app/Livewire/Ui/System/WoltguardCard.php

386 lines
14 KiB
PHP

<?php
namespace App\Livewire\Ui\System;
use App\Models\Setting;
use App\Support\CacheVer;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
class WoltguardCard extends Component
{
/** Gesamter Roh-Input aus dem Health-Cache */
public array $services = [];
/** UI: Status */
public bool $guardOk = false;
public int $okCount = 0;
public int $totalCount = 0;
public int $downCount = 0;
public string $badgeText = 'unbekannt';
public string $badgeIcon = 'ph ph-question';
public string $badgeClass = 'text-white/70 border-white/20 bg-white/10';
/** Liste der ausgefallenen Dienste (für Tooltip/Anzeige) */
public array $downServices = [];
/** Pollintervall (Sek.) */
public int $pollSeconds = 30;
public function mount(): void
{
$this->load();
}
public function render()
{
return view('livewire.ui.system.woltguard-card');
}
/** Manuelles Refresh aus dem UI */
public function refresh(): void
{
$this->load();
}
/* ---------------- intern ---------------- */
protected function load(): void
{
// 1) Versionierter Key holen
// $val = Cache::get(\App\Support\CacheVer::k('health:services'));
//
// // 2) Falls leer → Legacy-Key (altes Format: direkt die Rows)
// if (empty($val)) {
// $legacy = Cache::get('health:services', []);
// if (!empty($legacy) && is_array($legacy)) {
// $val = ['ts' => null, 'rows' => $legacy];
// }
// }
//
// // 3) DB-Fallback (Settings), wenn weiterhin leer
// if (empty($val)) {
// $val = \App\Models\Setting::get('woltguard.services'); // erwartet ['ts'=>..,'rows'=>[..]]
// }
//
// // 4) Shape normalisieren
// $rows = [];
// if (is_array($val)) {
// // neues Format
// if (array_key_exists('rows', $val) && is_array($val['rows'])) {
// $rows = $val['rows'];
// }
// // falls jemand doch wieder nur die Rows speichert
// elseif (isset($val[0]) && is_array($val[0]) && array_key_exists('name', $val[0])) {
// $rows = $val;
// }
// }
// $list = Cache::get(CacheVer::k('health:services'));
// if (empty($list)) {
// // Fallback: aus DB/Redis (persistiert vom Job)
// $list = \App\Support\Setting::get('woltguard.services', []);
// }
//
// $rows = [];
// if (is_array($list)) {
// // neues Format: ['ts'=>..., 'rows'=>[...]]
// if (isset($list['rows']) && is_array($list['rows'])) {
// $rows = $list['rows'];
// }
// // altes Format: direkt die Array-Liste
// elseif (isset($list[0]) && is_array($list[0])) {
// $rows = $list;
// }
// }
$list = Cache::get(CacheVer::k('health:services'));
if (empty($list)) {
// Fallback: persistierter Wert aus DB/Redis
$list = \App\Support\Setting::get('woltguard.services', []);
}
$rows = [];
if (is_array($list)) {
$rows = isset($list['rows']) && is_array($list['rows'])
? $list['rows'] // neues Format
: (isset($list[0]) && is_array($list[0]) ? $list : []); // altes Format
}
$this->services = $rows;
$this->totalCount = count($rows);
$this->okCount = collect($rows)->where('ok', true)->count();
$this->downCount = $this->totalCount - $this->okCount;
$this->guardOk = $this->totalCount > 0 && $this->downCount === 0;
$this->downServices = collect($rows)
->filter(fn($s) => !($s['ok'] ?? false))
->map(fn($s) => (string)($s['name'] ?? 'unbekannt'))
->values()->all();
// Badge
if ($this->totalCount === 0) {
$this->badgeText = 'keine Daten';
$this->badgeIcon = 'ph ph-warning-circle';
$this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
return;
}
if ($this->guardOk) {
$this->badgeText = 'alle Dienste OK';
$this->badgeIcon = 'ph ph-check-circle';
$this->badgeClass = 'text-emerald-300 border-emerald-400/30 bg-emerald-500/10';
} else {
$this->badgeText = $this->downCount >= 3 ? "{$this->downCount} Dienste down" : 'Störung erkannt';
$this->badgeIcon = $this->downCount >= 3 ? 'ph ph-x-circle' : 'ph ph-warning-circle';
$this->badgeClass = $this->downCount >= 3
? 'text-rose-300 border-rose-400/30 bg-rose-500/10'
: 'text-amber-300 border-amber-400/30 bg-amber-500/10';
}
}
// protected function load(): void
// {
// // 1) Primär: versionierter Cache-Key (mit ts/rows)
// $data = Cache::get(CacheVer::k('health:services'));
//
// // 2) Fallback: Settings (DB) wenn Cache leer/fehlend
// if (!is_array($data) || empty($data['rows'])) {
// $data = \App\Models\Setting::get('woltguard.services', []);
// }
//
// // 3) Letzter bekannter Zustand als Notanker (kein Flackern)
// $rows = $data['rows'] ?? [];
// if (empty($rows) && !empty($this->services)) {
// $rows = $this->services;
// }
//
// // ---- Mapping in UI-Props ----
// $this->services = $rows;
// $this->totalCount = count($rows);
// $this->okCount = collect($rows)->where('ok', true)->count();
// $this->downCount = max(0, $this->totalCount - $this->okCount);
// $this->guardOk = ($this->totalCount > 0) && ($this->downCount === 0);
//
// $this->downServices = collect($rows)
// ->filter(fn ($s) => !($s['ok'] ?? false))
// ->map(fn ($s) => (string)($s['name'] ?? 'unbekannt'))
// ->values()
// ->all();
//
// // Badge
// if ($this->totalCount === 0) {
// $this->badgeText = 'keine Daten';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// return;
// }
//
// if ($this->guardOk) {
// $this->badgeText = 'alle Dienste OK';
// $this->badgeIcon = 'ph ph-check-circle';
// $this->badgeClass = 'text-emerald-300 border-emerald-400/30 bg-emerald-500/10';
// } else {
// if ($this->downCount >= 3) {
// $this->badgeText = "{$this->downCount} Dienste down";
// $this->badgeIcon = 'ph ph-x-circle';
// $this->badgeClass = 'text-rose-300 border-rose-400/30 bg-rose-500/10';
// } else {
// $this->badgeText = 'Störung erkannt';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// }
// }
// }
// protected function load(): void
// {
// // 1) Primär: Redis
// $data = Cache::get(CacheVer::k('health:services'));
//
// // 2) Fallback: Settings (DB)
// if (!is_array($data) || empty($data['rows'])) {
// $data = Setting::get('woltguard.services', []);
// }
//
// // 3) Falls beides leer → letzter bekannter Zustand beibehalten
// $rows = $data['rows'] ?? [];
// if (empty($rows) && !empty($this->services)) {
// $rows = $this->services;
// }
//
// // Mapping
// $this->services = $rows;
// $this->totalCount = count($rows);
// $this->okCount = collect($rows)->where('ok', true)->count();
// $this->downCount = max(0, $this->totalCount - $this->okCount);
// $this->guardOk = ($this->totalCount > 0) && ($this->downCount === 0);
//
// $this->downServices = collect($rows)
// ->filter(fn($s) => !($s['ok'] ?? false))
// ->map(fn($s) => (string)($s['name'] ?? 'unbekannt'))
// ->values()
// ->all();
//
// // Badges
// if ($this->totalCount === 0) {
// $this->badgeText = 'keine Daten';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// return;
// }
//
// if ($this->guardOk) {
// $this->badgeText = 'alle Dienste OK';
// $this->badgeIcon = 'ph ph-check-circle';
// $this->badgeClass = 'text-emerald-300 border-emerald-400/30 bg-emerald-500/10';
// } else {
// if ($this->downCount >= 3) {
// $this->badgeText = "{$this->downCount} Dienste down";
// $this->badgeIcon = 'ph ph-x-circle';
// $this->badgeClass = 'text-rose-300 border-rose-400/30 bg-rose-500/10';
// } else {
// $this->badgeText = 'Störung erkannt';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// }
// }
// }
}
// protected function load(): void
// {
// // 1) Primär: versionierter Key
// $list = Cache::get(CacheVer::k('health:services'), []);
// // 2) Fallback: Legacy-Key (nur falls 1) leer)
// if (empty($list)) {
// $list = Cache::get('health:services', []);
// }
//
// $this->services = is_array($list) ? $list : [];
//
// $this->totalCount = count($this->services);
// $this->okCount = collect($this->services)->filter(fn($s) => (bool)($s['ok'] ?? false))->count();
// $this->downCount = $this->totalCount - $this->okCount;
// $this->guardOk = ($this->totalCount > 0) && ($this->downCount === 0);
//
// $this->downServices = collect($this->services)
// ->filter(fn($s) => !($s['ok'] ?? false))
// ->map(fn($s) => (string)($s['name'] ?? 'unbekannt'))
// ->values()
// ->all();
//
// // Badge
// if ($this->totalCount === 0) {
// $this->badgeText = 'keine Daten';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// return;
// }
//
// if ($this->guardOk) {
// $this->badgeText = 'alle Dienste OK';
// $this->badgeIcon = 'ph ph-check-circle';
// $this->badgeClass = 'text-emerald-300 border-emerald-400/30 bg-emerald-500/10';
// } else {
// if ($this->downCount >= 3) {
// $this->badgeText = "{$this->downCount} Dienste down";
// $this->badgeIcon = 'ph ph-x-circle';
// $this->badgeClass = 'text-rose-300 border-rose-400/30 bg-rose-500/10';
// } else {
// $this->badgeText = 'Störung erkannt';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// }
// }
// }
//}
//
//namespace App\Livewire\Ui\System;
//
//use Illuminate\Support\Facades\Cache;
//use Livewire\Component;
//
//class WoltguardCard extends Component
//{
// /** Gesamter Roh-Input aus dem Health-Cache (optional für später) */
// public array $services = [];
//
// /** UI: zusammengefasster Status */
// public bool $guardOk = false; // alle Dienste OK?
// public int $okCount = 0; // wie viele OK
// public int $totalCount = 0; // wie viele gesamt
// public int $downCount = 0; // wie viele down
// public string $badgeText = 'unbekannt';
// public string $badgeIcon = 'ph ph-question';
// public string $badgeClass = 'text-white/70 border-white/20 bg-white/10';
//
// /** Optional: Liste der ausgefallenen Dienste (für Tooltip/weitere Anzeige) */
// public array $downServices = [];
//
// /** Pollintervall steuern (z. B. 30s) */
// public int $pollSeconds = 30;
//
// public function mount(): void
// {
// $this->load();
// }
//
// public function render()
// {
// return view('livewire.ui.system.woltguard-card');
// }
//
// /** Manuelles Refresh aus dem UI */
// public function refresh(): void
// {
// $this->load();
// }
//
// /* ---------------- intern ---------------- */
//
// protected function load(): void
// {
// // Erwartet: Cache::put('health:services', [['name'=>'postfix','ok'=>true], ...])
// $list = Cache::get('health:services', []);
// $this->services = is_array($list) ? $list : [];
//
// $this->totalCount = count($this->services);
// $this->okCount = collect($this->services)->filter(fn ($s) => (bool)($s['ok'] ?? false))->count();
// $this->downCount = $this->totalCount - $this->okCount;
// $this->guardOk = ($this->totalCount > 0) && ($this->downCount === 0);
//
// // Down-Services Namen extrahieren
// $this->downServices = collect($this->services)
// ->filter(fn ($s) => !($s['ok'] ?? false))
// ->map(fn ($s) => (string)($s['name'] ?? 'unbekannt'))
// ->values()
// ->all();
//
// // Badge aufbereiten (Text/Style/Icon)
// if ($this->totalCount === 0) {
// $this->badgeText = 'keine Daten';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// return;
// }
//
// if ($this->guardOk) {
// $this->badgeText = 'alle Dienste OK';
// $this->badgeIcon = 'ph ph-check-circle';
// $this->badgeClass = 'text-emerald-300 border-emerald-400/30 bg-emerald-500/10';
// } else {
// // kleine Abstufung je nach Anzahl der Störungen
// if ($this->downCount >= 3) {
// $this->badgeText = "{$this->downCount} Dienste down";
// $this->badgeIcon = 'ph ph-x-circle';
// $this->badgeClass = 'text-rose-300 border-rose-400/30 bg-rose-500/10';
// } else {
// $this->badgeText = 'Störung erkannt';
// $this->badgeIcon = 'ph ph-warning-circle';
// $this->badgeClass = 'text-amber-300 border-amber-400/30 bg-amber-500/10';
// }
// }
// }
//}