using Microsoft.AspNetCore.Mvc.RazorPages; using PlanTempusAdmin.Models; using PlanTempusAdmin.Services; namespace PlanTempusAdmin.Pages; public class IndexModel : PageModel { private readonly CaddyService _caddyService; private readonly BackupService _backupService; private readonly ForgejoService _forgejoService; private readonly AzureStorageService _azureService; // Caddy public bool CaddyRunning { get; set; } public int HostCount { get; set; } // Backup public bool BackupDbConnected { get; set; } public BackupSummary BackupSummary { get; set; } = new(); public string? LastBackupAge { get; set; } public bool LastBackupOk { get; set; } // Forgejo public bool ForgejoConnected { get; set; } public ForgejoDashboard ForgejoDashboard { get; set; } = new(); // Azure Storage public bool AzureConnected { get; set; } public AzureStorageDashboard AzureDashboard { get; set; } = new(); public IndexModel(CaddyService caddyService, BackupService backupService, ForgejoService forgejoService, AzureStorageService azureService) { _caddyService = caddyService; _backupService = backupService; _forgejoService = forgejoService; _azureService = azureService; } public async Task OnGetAsync() { // Caddy status CaddyRunning = await _caddyService.IsRunningAsync(); if (CaddyRunning) { var hosts = await _caddyService.GetHostsAsync(); HostCount = hosts.Count; } // Backup status BackupDbConnected = await _backupService.TestConnectionAsync(); if (BackupDbConnected) { BackupSummary = await _backupService.GetSummaryAsync(); if (BackupSummary.LastBackup.HasValue) { var age = DateTime.Now - BackupSummary.LastBackup.Value; LastBackupAge = FormatAge(age); LastBackupOk = age.TotalHours < 24; } } // Forgejo status ForgejoConnected = await _forgejoService.TestConnectionAsync(); if (ForgejoConnected) { ForgejoDashboard = await _forgejoService.GetDashboardAsync(); } // Azure Storage status AzureConnected = await _azureService.TestConnectionAsync(); if (AzureConnected) { AzureDashboard = await _azureService.GetDashboardAsync(); } } private string FormatAge(TimeSpan age) { if (age.TotalMinutes < 60) return $"{(int)age.TotalMinutes}m siden"; if (age.TotalHours < 24) return $"{(int)age.TotalHours}t siden"; return $"{(int)age.TotalDays}d siden"; } }