fix(statistics): use alpm executor

This commit is contained in:
Jguer 2020-08-04 21:00:07 +01:00 committed by jguer
parent 94f650f4d3
commit 7bcf2ecb4c
No known key found for this signature in database
GPG key ID: 6D6CC9BEA8556B35
5 changed files with 29 additions and 34 deletions

2
cmd.go
View file

@ -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
}

View file

@ -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
}

View file

@ -16,4 +16,5 @@ type RepoPackage interface {
ShouldIgnore() bool
Size() int64
Version() string
Reason() alpm.PkgReason
}

View file

@ -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)

View file

@ -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
}