Merge pull request 'Partials erstellt und auto-repload auf 30 Sekunden Interval gesetzt' (#28) from feature/Site-Reloading into development

Reviewed-on: daniel-hbn/Watcher#28
This commit is contained in:
2025-06-20 18:30:22 +00:00
6 changed files with 151 additions and 134 deletions

View File

@@ -38,5 +38,26 @@ namespace Watcher.Controllers
return View(viewModel);
}
public IActionResult DashboardStats()
{
Console.WriteLine("Dashboard aktualisiert");
var servers = _context.Servers.ToList();
var containers = _context.Containers.ToList();
var now = DateTime.UtcNow;
var model = new DashboardViewModel
{
ActiveServers = servers.Count(s => (now - s.LastSeen).TotalSeconds <= 120),
OfflineServers = servers.Count(s => (now - s.LastSeen).TotalSeconds > 120),
//RunningContainers = containers.Count(c => (now - c.LastSeen).TotalSeconds <= 120),
//FailedContainers = containers.Count(c => (now - c.LastSeen).TotalSeconds > 120),
LastLogin = DateTime.Now // Oder was auch immer hier richtig ist
};
return PartialView("_DashboardStats", model);
}
}
}

View File

@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -48,7 +49,7 @@ public class ServerController : Controller
_context.Servers.Add(server);
await _context.SaveChangesAsync();
return RedirectToAction("Overview");
return Redirect("Server/Overview");
}
[HttpPost]
@@ -103,4 +104,18 @@ public class ServerController : Controller
return View(vm);
}
public async Task<IActionResult> ServerCardsPartial()
{
var servers = _context.Servers.ToList();
foreach (var server in servers)
{
server.IsOnline = (DateTime.UtcNow - server.LastSeen).TotalSeconds <= 120;
await _context.SaveChangesAsync();
}
return PartialView("_ServerCard", servers); // korrekt
}
}

View File

@@ -5,25 +5,12 @@
<h1 class="mb-4"><i class="bi bi-speedometer2 me-2"></i>Dashboard</h1>
<div class="row g-4">
<div class="col-md-6">
<div class="card p-3">
<h2 class="h5"><i class="bi bi-hdd-network me-2"></i>Server</h2>
<p><i class="bi bi-check-circle-fill text-success me-1"></i>Online: <strong>@Model.ActiveServers</strong></p>
<p><i class="bi bi-x-circle-fill text-danger me-1"></i>Offline: <strong>@Model.OfflineServers</strong></p>
<a href="/Server/Overview" class="btn btn-link px-0"><i class="bi bi-box-arrow-in-right me-1"></i>Zu den Servern</a>
</div>
</div>
<div id="dashboard-stats">
@await Html.PartialAsync("_DashboardStats", Model)
</div>
<div class="col-md-6">
<div class="card p-3">
<h2 class="h5"><i class="bi bi-box-seam me-2"></i>Container</h2>
<p><i class="bi bi-play-circle-fill text-success me-1"></i>Laufend: <strong>@Model.RunningContainers</strong></p>
<p><i class="bi bi-exclamation-circle-fill text-danger me-1"></i>Fehlerhaft: <strong>@Model.FailedContainers</strong></p>
<a href="/Container/Overview" class="btn btn-link px-0"><i class="bi bi-box-arrow-in-right me-1"></i>Zu den Containern</a>
</div>
</div>
<div class="row g-4 mt-4">
<div class="col-12">
<div class="card p-3">
<h2 class="h5"><i class="bi bi-graph-up me-2"></i>Uptime letzte 24h</h2>
@@ -42,3 +29,25 @@
</div>
</div>
</div>
@section Scripts {
<script>
async function loadDashboardStats() {
try {
const response = await fetch('/Home/DashboardStats');
if (response.ok) {
const html = await response.text();
document.getElementById('dashboard-stats').innerHTML = html;
} else {
console.error('Fehler beim Nachladen der Dashboard-Statistiken');
}
} catch (err) {
console.error('Netzwerkfehler beim Nachladen der Dashboard-Statistiken:', err);
}
}
// Initial laden und dann alle 30 Sekunden
loadDashboardStats();
setInterval(loadDashboardStats, 30000);
</script>
}

View File

@@ -0,0 +1,17 @@
@model Watcher.ViewModels.DashboardViewModel
<div class="grid grid-cols-2 gap-6">
<div class="bg-white shadow rounded-2xl p-4">
<h2 class="text-xl font-semibold mb-2">Server</h2>
<p>🟢 Online: <strong>@Model.ActiveServers</strong></p>
<p>🔴 Offline: <strong>@Model.OfflineServers</strong></p>
<a href="/Server/Overview" class="text-blue-500 hover:underline mt-2 inline-block">→ Zu den Servern</a>
</div>
<div class="bg-white shadow rounded-2xl p-4">
<h2 class="text-xl font-semibold mb-2">Container</h2>
<p>🟢 Laufend: <strong>@Model.RunningContainers</strong></p>
<p>🔴 Fehlerhaft: <strong>@Model.FailedContainers</strong></p>
<a href="/Container/Overview" class="text-blue-500 hover:underline mt-2 inline-block">→ Zu den Containern</a>
</div>
</div>

View File

@@ -0,0 +1,51 @@
@model IEnumerable<Watcher.Models.Server>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
@foreach (var s in Model)
{
<div class="bg-blue rounded-xl shadow p-5 border border-gray-200 flex flex-col gap-4">
<div class="flex justify-between items-center">
<h2 class="text-lg font-semibold text-gray-800">
<i class="bi bi-cpu me-1 text-gray-600"></i>(#@s.Id) @s.Name
</h2>
<span class="text-sm px-2 py-1 rounded font-medium
@(s.IsOnline ? "bg-green-100 text-green-700" : "bg-red-100 text-red-700")">
<i class="bi @(s.IsOnline ? "bi-check-circle" : "bi-x-circle") me-1"></i>
@(s.IsOnline ? "Online" : "Offline")
</span>
</div>
<div class="flex gap-6">
<!-- Linke Seite: Infos, nimmt ca. 40% Breite -->
<div class="flex-1 max-w-[40%] text-sm space-y-1 text-gray-700">
<div><i class="bi bi-globe me-1 text-gray-500"></i><strong>IP:</strong> @s.IPAddress</div>
<div><i class="bi bi-hdd me-1 text-gray-500"></i><strong>Typ:</strong> @s.Type</div>
<div><i class="bi bi-calendar-check me-1 text-gray-500"></i><strong>Erstellt:</strong>
@s.CreatedAt.ToLocalTime().ToString("dd.MM.yyyy HH:mm")</div>
<div><i class="bi bi-clock me-1 text-gray-500"></i><strong>Last-Seen:</strong>
@s.LastSeen.ToLocalTime().ToString("dd.MM.yyyy HH:mm")</div>
</div>
<!-- Rechte Seite: Zwei Diagramme nebeneinander, je ca. 50% von 60% = 30% insgesamt -->
<div class="flex-1 flex gap-4 max-w-[60%]">
</div>
</div>
<div class="flex justify-end gap-3">
<a asp-action="EditServer" asp-route-id="@s.Id" class="text-blue-600 hover:text-blue-800 font-medium">
<i class="bi bi-pencil-square me-1"></i>Bearbeiten
</a>
<form asp-action="Delete" asp-route-id="@s.Id" method="post"
onsubmit="return confirm('Diesen Server wirklich löschen?');">
<button type="submit" class="text-red-600 hover:text-red-800 font-medium">
<i class="bi bi-trash me-1"></i>Löschen
</button>
</form>
</div>
</div>
}
</div>

View File

@@ -13,124 +13,28 @@
</a>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
@foreach (var s in Model.Servers)
{
<div class="bg-blue rounded-xl shadow p-5 border border-gray-200 flex flex-col gap-4">
<div class="flex justify-between items-center">
<h2 class="text-lg font-semibold text-gray-800">
<i class="bi bi-cpu me-1 text-gray-600"></i>(#@s.Id) @s.Name
</h2>
<span class="text-sm px-2 py-1 rounded font-medium
@(s.IsOnline ? "bg-green-100 text-green-700" : "bg-red-100 text-red-700")">
<i class="bi @(s.IsOnline ? "bi-check-circle" : "bi-x-circle") me-1"></i>
@(s.IsOnline ? "Online" : "Offline")
</span>
</div>
<div class="flex gap-6">
<!-- Linke Seite: Infos, nimmt ca. 40% Breite -->
<div class="flex-1 max-w-[40%] text-sm space-y-1 text-gray-700">
<div><i class="bi bi-globe me-1 text-gray-500"></i><strong>IP:</strong> @s.IPAddress</div>
<div><i class="bi bi-hdd me-1 text-gray-500"></i><strong>Typ:</strong> @s.Type</div>
<div><i class="bi bi-calendar-check me-1 text-gray-500"></i><strong>Erstellt:</strong>
@s.CreatedAt.ToLocalTime().ToString("dd.MM.yyyy HH:mm")</div>
<div><i class="bi bi-clock me-1 text-gray-500"></i><strong>Last-Seen:</strong>
@s.LastSeen.ToLocalTime().ToString("dd.MM.yyyy HH:mm")</div>
</div>
<!-- Rechte Seite: Zwei Diagramme nebeneinander, je ca. 50% von 60% = 30% insgesamt -->
<div class="flex-1 flex gap-4 max-w-[60%]">
</div>
</div>
<div class="flex justify-end gap-3">
<a asp-action="EditServer" asp-route-id="@s.Id" class="text-blue-600 hover:text-blue-800 font-medium">
<i class="bi bi-pencil-square me-1"></i>Bearbeiten
</a>
<form asp-action="Delete" asp-route-id="@s.Id" method="post"
onsubmit="return confirm('Diesen Server wirklich löschen?');">
<button type="submit" class="text-red-600 hover:text-red-800 font-medium">
<i class="bi bi-trash me-1"></i>Löschen
</button>
</form>
</div>
</div>
}
<div id="server-cards-container">
@await Html.PartialAsync("_ServerCard", Model.Servers)
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
@section Scripts {
<script>
document.addEventListener("DOMContentLoaded", function () {
@foreach (var s in Model.Servers)
{
<text>
var ctxCpu_@s.Id = document.getElementById("cpuChart-@s.Id").getContext('2d');
var cpuChart_@s.Id = new Chart(ctxCpu_@s.Id, {
type: 'bar',
data: {
labels: ['0s', '5s', '10s', '15s', '20s'],
datasets: [{
label: 'CPU %',
data: [12, 19, 7, 15, 10], // Beispielwerte
backgroundColor: 'rgba(59, 130, 246, 0.7)', // Tailwind blue-500 transparent
borderColor: 'rgba(59, 130, 246, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
animation: false,
scales: {
y: {
beginAtZero: true,
max: 100
}
},
plugins: {
legend: {
display: false
}
}
<script>
async function loadServerCards() {
try {
const response = await fetch('/Server/ServerCardsPartial');
if (response.ok) {
const html = await response.text();
document.getElementById('server-cards-container').innerHTML = html;
} else {
console.error('Fehler beim Nachladen der Serverkarten');
}
});
var ctxRam_@s.Id = document.getElementById("ramChart-@s.Id").getContext('2d');
var ramChart_@s.Id = new Chart(ctxRam_@s.Id, {
type: 'bar',
data: {
labels: ['0s', '5s', '10s', '15s', '20s'],
datasets: [{
label: 'RAM %',
data: [45, 40, 55, 48, 50], // Beispielwerte
backgroundColor: 'rgba(16, 185, 129, 0.7)', // Tailwind green-500 transparent
borderColor: 'rgba(16, 185, 129, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
animation: false,
scales: {
y: {
beginAtZero: true,
max: 100
}
},
plugins: {
legend: {
display: false
}
}
}
});
</text>
} catch (err) {
console.error('Netzwerkfehler beim Nachladen der Serverkarten:', err);
}
}
});
</script>
}
// Initial laden und dann alle 30 Sekunden
loadServerCards();
setInterval(loadServerCards, 30000);
</script>
}