Skip to content

Commit 9803cc1

Browse files
committed
Include size of data Directory
1 parent 7dba432 commit 9803cc1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

internal/api/backresthandler.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"io/fs"
1011
"os"
1112
"path"
13+
"path/filepath"
1214
"runtime"
1315
"slices"
1416
"strings"
@@ -854,9 +856,10 @@ func (s *BackrestHandler) GetSummaryDashboard(ctx context.Context, req *connect.
854856
}, nil
855857
}
856858

859+
DataPathSize := directorySize(env.DataDir())
857860
response := &v1.SummaryDashboardResponse{
858861
ConfigPath: env.ConfigFilePath(),
859-
DataPath: env.DataDir(),
862+
DataPath: env.DataDir() + " (" + DataPathSize + ")",
860863
}
861864

862865
if hostname, err := os.Hostname(); err != nil {
@@ -926,6 +929,46 @@ func humanTime(d time.Duration) string {
926929
return fmt.Sprintf("%02dh:%02dm:%02ds", h, m, s)
927930
}
928931

932+
func formatSize(size int64) string {
933+
if size < 1024 {
934+
return fmt.Sprintf("%d B", size)
935+
}
936+
const unit = 1024
937+
if size < unit*unit {
938+
return fmt.Sprintf("%.2f KB", float64(size)/unit)
939+
}
940+
if size < unit*unit*unit {
941+
return fmt.Sprintf("%.2f MB", float64(size)/(unit*unit))
942+
}
943+
if size < unit*unit*unit*unit {
944+
return fmt.Sprintf("%.2f GB", float64(size)/(unit*unit*unit))
945+
}
946+
return fmt.Sprintf("%.2f TB", float64(size)/(unit*unit*unit*unit))
947+
}
948+
949+
func directorySize(path string) string {
950+
var totalSize int64
951+
err := filepath.WalkDir(path, func(p string, d fs.DirEntry, err error) error {
952+
if err != nil {
953+
return err
954+
}
955+
if !d.IsDir() {
956+
info, err := d.Info()
957+
if err != nil {
958+
return err
959+
}
960+
totalSize += info.Size()
961+
}
962+
return nil
963+
})
964+
965+
if err != nil {
966+
return "0"
967+
}
968+
969+
return formatSize(totalSize)
970+
}
971+
929972
func systemOSVersion() string {
930973
if data, err := os.ReadFile("/etc/os-release"); err == nil {
931974
for _, line := range strings.Split(string(data), "\n") {

0 commit comments

Comments
 (0)