mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 21:49:40 +01:00
chore: add convert bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB)
This commit is contained in:
@@ -37,7 +37,7 @@ type MailConfig struct {
|
||||
}
|
||||
type NotificationData struct {
|
||||
File string
|
||||
BackupSize int64
|
||||
BackupSize string
|
||||
Database string
|
||||
StartTime string
|
||||
EndTime string
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user