chore: add convert bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB)

This commit is contained in:
2024-12-12 13:28:09 +01:00
parent d880f40108
commit 1b60ca6fd2
11 changed files with 37 additions and 18 deletions

View File

@@ -37,7 +37,7 @@ type MailConfig struct {
}
type NotificationData struct {
File string
BackupSize int64
BackupSize string
Database string
StartTime string
EndTime string

View File

@@ -254,3 +254,19 @@ func CronNextTime(cronExpr string) time.Time {
next := schedule.Next(now)
return next
}
// ConvertBytes converts bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB).
func ConvertBytes(bytes uint64) string {
const (
MiB = 1024 * 1024
GiB = MiB * 1024
)
switch {
case bytes >= GiB:
return fmt.Sprintf("%.2f GiB", float64(bytes)/float64(GiB))
case bytes >= MiB:
return fmt.Sprintf("%.2f MiB", float64(bytes)/float64(MiB))
default:
return fmt.Sprintf("%d bytes", bytes)
}
}