From 9c2d4ec0d13c84ff31f25806ca24a16de6e9879f Mon Sep 17 00:00:00 2001 From: Norbert Pfeiler Date: Sun, 6 Feb 2022 10:53:34 +0100 Subject: [PATCH] feat(stats): add statistics about pacman and yay cache sizes (#1679) --- print.go | 6 ++++++ query.go | 51 ++++++++++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/print.go b/print.go index 0819b0ae5..5bfcd9edd 100644 --- a/print.go +++ b/print.go @@ -84,6 +84,12 @@ func localStatistics(ctx context.Context, dbExecutor db.Executor) error { text.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames))))) text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln)))) text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize)))) + + for path, size := range info.pacmanCaches { + text.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size)))) + } + + text.Infoln(gotext.Get("Size of yay cache %s: %s", config.BuildDir, text.Cyan(text.Human(info.yayCache)))) fmt.Println(text.Bold(text.Cyan("==========================================="))) text.Infoln(gotext.Get("Ten biggest packages:")) biggestPackages(dbExecutor) diff --git a/query.go b/query.go index 20ecfdf79..524806f72 100644 --- a/query.go +++ b/query.go @@ -3,7 +3,9 @@ package main import ( "context" "fmt" + "io/fs" "os" + "path/filepath" aur "github.com/Jguer/aur" alpm "github.com/Jguer/go-alpm/v2" @@ -190,36 +192,39 @@ func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []str return hanging } -// Statistics returns statistics about packages installed in system. -func statistics(dbExecutor db.Executor) *struct { - Totaln int - Expln int - TotalSize int64 -} { - var ( - totalSize int64 +func getFolderSize(path string) (size int64) { + _ = filepath.WalkDir(path, func(p string, entry fs.DirEntry, err error) error { + info, _ := entry.Info() + size += info.Size() + return nil + }) - localPackages = dbExecutor.LocalPackages() - totalInstalls = 0 - explicitInstalls = 0 - ) + return size +} - for _, pkg := range localPackages { - totalSize += pkg.ISize() - totalInstalls++ +// Statistics returns statistics about packages installed in system. +func statistics(dbExecutor db.Executor) (res struct { + Totaln int + Expln int + TotalSize int64 + pacmanCaches map[string]int64 + yayCache int64 +}) { + for _, pkg := range dbExecutor.LocalPackages() { + res.TotalSize += pkg.ISize() + res.Totaln++ if pkg.Reason() == alpm.PkgReasonExplicit { - explicitInstalls++ + res.Expln++ } } - info := &struct { - Totaln int - Expln int - TotalSize int64 - }{ - totalInstalls, explicitInstalls, totalSize, + res.pacmanCaches = make(map[string]int64) + for _, path := range config.Runtime.PacmanConf.CacheDir { + res.pacmanCaches[path] = getFolderSize(path) } - return info + res.yayCache = getFolderSize(config.BuildDir) + + return }