Files
watcher/Watcher/Controllers/HomeController.cs

64 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
using Watcher.Data;
using Watcher.Models;
using Watcher.ViewModels;
namespace Watcher.Controllers
{
[Authorize]
public class HomeController : Controller
{
private readonly AppDbContext _context;
public HomeController(AppDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var preferredUserName = User.FindFirst("preferred_username")?.Value;
var user = await _context.Users
.Where(u => u.PreferredUsername == preferredUserName)
.FirstOrDefaultAsync();
var viewModel = new DashboardViewModel
{
ActiveServers = await _context.Servers.CountAsync(s => s.IsOnline),
OfflineServers = await _context.Servers.CountAsync(s => !s.IsOnline),
RunningContainers = await _context.Containers.CountAsync(c => c.IsRunning),
FailedContainers = await _context.Containers.CountAsync(c => !c.IsRunning),
LastLogin = user?.LastLogin ?? DateTime.MinValue
};
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);
}
}
}