package templates import ( "backea/internal/backup" "backea/templates/layouts" "fmt" "sort" "time" ) // FormatSize formats byte size to human-readable format func FormatSize(size int64) string { const unit = 1024 if size < unit { return fmt.Sprintf("%d B", size) } div, exp := int64(unit), 0 for n := size / unit; n >= unit; n /= unit { div *= unit exp++ } return fmt.Sprintf("%.2f %cB", float64(size)/float64(div), "KMGTPE"[exp]) } // FormatTime formats time to a readable format func FormatTime(t time.Time) string { return t.Format("Jan 02, 2006 15:04:05") } // FormatTimeSince formats the duration since a time in a human-readable way func FormatTimeSince(t time.Time) string { timeSince := time.Since(t) hours := int(timeSince.Hours()) if hours < 1 { return fmt.Sprintf("%d minutes ago", int(timeSince.Minutes())) } return fmt.Sprintf("%d hours ago", hours) } // GetStatusClass returns the appropriate status class based on backup age func GetStatusClass(t time.Time) string { timeSince := time.Since(t) hours := int(timeSince.Hours()) if hours > 72 { return "Failed" } else if hours > 24 { return "Warning" } return "Healthy" } // FormatServiceName returns a display-friendly service name func FormatServiceName(groupName, serviceIndex string) string { if serviceIndex == "" { return groupName } return fmt.Sprintf("%s - %s", groupName, serviceIndex) } // CalculateTotalSize calculates total size of all backups func CalculateTotalSize(backups []backup.BackupInfo) int64 { var total int64 for _, b := range backups { total += b.Size } return total } // CalculateGroupTotalSize calculates total size of all backups for a service group func CalculateGroupTotalSize(serviceGroup map[string][]backup.BackupInfo) int64 { var total int64 for _, backups := range serviceGroup { for _, b := range backups { total += b.Size } } return total } // GetGroupTotalBackupCount returns the total number of backups across all services in a group func GetGroupTotalBackupCount(serviceGroup map[string][]backup.BackupInfo) int { count := 0 for _, backups := range serviceGroup { count += len(backups) } return count } // GetLatestBackupTime returns the most recent backup time for a service group func GetLatestBackupTime(serviceGroup map[string][]backup.BackupInfo) (time.Time, bool) { var latestTime time.Time found := false for _, backups := range serviceGroup { if len(backups) > 0 && (latestTime.IsZero() || backups[0].CreationTime.After(latestTime)) { latestTime = backups[0].CreationTime found = true } } return latestTime, found } // GetGroupStatus returns the status of a service group based on the most recent backup func GetGroupStatus(serviceGroup map[string][]backup.BackupInfo) string { latestTime, found := GetLatestBackupTime(serviceGroup) if !found { return "No Backups" } return GetStatusClass(latestTime) } // ServiceProviderInfo holds the backup strategy info for a service type ServiceProviderInfo struct { Type string Provider string Directory string } // BackupWithService represents a backup with its service identifier type BackupWithService struct { ServiceIndex string Backup backup.BackupInfo } // GetSortedBackups collects all backups from a service group and sorts them by time func GetSortedBackups(serviceGroup map[string][]backup.BackupInfo) []BackupWithService { var allBackups []BackupWithService // Collect all backups with their service indices for serviceIndex, backups := range serviceGroup { for _, b := range backups { allBackups = append(allBackups, BackupWithService{ ServiceIndex: serviceIndex, Backup: b, }) } } // Sort by creation time (newest first) sort.Slice(allBackups, func(i, j int) bool { return allBackups[i].Backup.CreationTime.After(allBackups[j].Backup.CreationTime) }) return allBackups } // Home renders the homepage with lazy-loaded backup information templ Home(serviceBackups map[string]map[string][]backup.BackupInfo, serviceConfigs map[string]map[string]ServiceProviderInfo, sortedGroupNames []string, groupDirectories map[string]string) { @layouts.Base("Backea - Backup Dashboard") {
Unified guardians.
No backup services configured or no backups found.
Total Size
--
Backups
--
Last Backup
--
Status
--
Total Size
{ FormatSize(CalculateGroupTotalSize(serviceBackups)) }
Backups
{ fmt.Sprintf("%d", GetGroupTotalBackupCount(serviceBackups)) }
Last Backup
if latestTime, found := GetLatestBackupTime(serviceBackups); found {{ FormatTimeSince(latestTime) }
} else {Never
}Status
if status := GetGroupStatus(serviceBackups); status == "No Backups" {No Backups
} else if status == "Failed" {Failed
} else if status == "Warning" {Warning
} else {Healthy
}Service | Date | Size | Type | Retention | Location | Actions |
---|
No backups found for this service.
Backups will appear here when created.
Service | Date | Size | Type | Retention | Location | Actions |
---|
No backups found for this service.
Backups will appear here when created.