From 7bcf2ecb4ca98b845da194632e60f60eb9143deb Mon Sep 17 00:00:00 2001 From: Jguer Date: Tue, 4 Aug 2020 21:00:07 +0100 Subject: [PATCH] fix(statistics): use alpm executor --- cmd.go | 2 +- pkg/db/alpm.go | 9 +++++++++ pkg/db/executor.go | 1 + print.go | 21 +++++---------------- query.go | 30 +++++++++++++----------------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/cmd.go b/cmd.go index dca5a7ec..74e29749 100644 --- a/cmd.go +++ b/cmd.go @@ -237,7 +237,7 @@ func handlePrint(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, dbExecuto case cmdArgs.ExistsArg("c", "complete"): err = completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false) case cmdArgs.ExistsArg("s", "stats"): - err = localStatistics(alpmHandle) + err = localStatistics(dbExecutor) default: err = nil } diff --git a/pkg/db/alpm.go b/pkg/db/alpm.go index 1f47e955..f376f4bc 100644 --- a/pkg/db/alpm.go +++ b/pkg/db/alpm.go @@ -320,3 +320,12 @@ func (ae *AlpmExecutor) RepoUpgrades(enableDowngrade bool) (upgrade.UpSlice, err func (ae *AlpmExecutor) AlpmArch() (string, error) { return ae.handle.Arch() } + +func (ae *AlpmExecutor) BiggestPackages() []RepoPackage { + localPackages := []RepoPackage{} + _ = ae.localDB.PkgCache().SortBySize().ForEach(func(pkg alpm.Package) error { + localPackages = append(localPackages, RepoPackage(&pkg)) + return nil + }) + return localPackages +} diff --git a/pkg/db/executor.go b/pkg/db/executor.go index 5002010f..003af4ac 100644 --- a/pkg/db/executor.go +++ b/pkg/db/executor.go @@ -16,4 +16,5 @@ type RepoPackage interface { ShouldIgnore() bool Size() int64 Version() string + Reason() alpm.PkgReason } diff --git a/print.go b/print.go index da68a9b6..debbb3b4 100644 --- a/print.go +++ b/print.go @@ -9,8 +9,6 @@ import ( "github.com/leonelquinteros/gotext" rpc "github.com/mikkeloscar/aur" - "github.com/Jguer/go-alpm" - "github.com/Jguer/yay/v10/pkg/db" "github.com/Jguer/yay/v10/pkg/query" "github.com/Jguer/yay/v10/pkg/settings" @@ -144,14 +142,8 @@ func PrintInfo(a *rpc.Pkg, extendedInfo bool) { } // BiggestPackages prints the name of the ten biggest packages in the system. -func biggestPackages(alpmHandle *alpm.Handle) { - localDB, err := alpmHandle.LocalDB() - if err != nil { - return - } - - pkgCache := localDB.PkgCache() - pkgS := pkgCache.SortBySize().Slice() +func biggestPackages(dbExecutor *db.AlpmExecutor) { + pkgS := dbExecutor.BiggestPackages() if len(pkgS) < 10 { return @@ -164,11 +156,8 @@ func biggestPackages(alpmHandle *alpm.Handle) { } // localStatistics prints installed packages statistics. -func localStatistics(alpmHandle *alpm.Handle) error { - info, err := statistics(alpmHandle) - if err != nil { - return err - } +func localStatistics(dbExecutor *db.AlpmExecutor) error { + info := statistics(dbExecutor) _, remoteNames, err := query.GetPackageNamesBySource(config.Runtime.DBExecutor) if err != nil { @@ -183,7 +172,7 @@ func localStatistics(alpmHandle *alpm.Handle) error { text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(text.Human(info.TotalSize)))) fmt.Println(bold(cyan("==========================================="))) text.Infoln(gotext.Get("Ten biggest packages:")) - biggestPackages(alpmHandle) + biggestPackages(dbExecutor) fmt.Println(bold(cyan("==========================================="))) query.AURInfoPrint(remoteNames, config.RequestSplitN) diff --git a/query.go b/query.go index 7a6b2956..4b383785 100644 --- a/query.go +++ b/query.go @@ -383,25 +383,21 @@ func hangingPackages(removeOptional bool, alpmHandle *alpm.Handle) (hanging []st } // Statistics returns statistics about packages installed in system -func statistics(alpmHandle *alpm.Handle) (*struct { +func statistics(dbExecutor *db.AlpmExecutor) *struct { Totaln int Expln int TotalSize int64 -}, error) { - var tS int64 // TotalSize - var nPkg int - var ePkg int +} { + var totalSize int64 + localPackages := dbExecutor.LocalPackages() + totalInstalls := 0 + explicitInstalls := 0 - localDB, err := alpmHandle.LocalDB() - if err != nil { - return nil, err - } - - for _, pkg := range localDB.PkgCache().Slice() { - tS += pkg.ISize() - nPkg++ - if pkg.Reason() == 0 { - ePkg++ + for _, pkg := range localPackages { + totalSize += pkg.ISize() + totalInstalls++ + if pkg.Reason() == alpm.PkgReasonExplicit { + explicitInstalls++ } } @@ -410,8 +406,8 @@ func statistics(alpmHandle *alpm.Handle) (*struct { Expln int TotalSize int64 }{ - nPkg, ePkg, tS, + totalInstalls, explicitInstalls, totalSize, } - return info, err + return info }