Browse Source

fix(statistics): use alpm executor

Jguer 4 years ago
parent
commit
7bcf2ecb4c
5 changed files with 30 additions and 35 deletions
  1. 1 1
      cmd.go
  2. 9 0
      pkg/db/alpm.go
  3. 1 0
      pkg/db/executor.go
  4. 5 16
      print.go
  5. 14 18
      query.go

+ 1 - 1
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
 	}

+ 9 - 0
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
+}

+ 1 - 0
pkg/db/executor.go

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

+ 5 - 16
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)

+ 14 - 18
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
-
-	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++
+} {
+	var totalSize int64
+	localPackages := dbExecutor.LocalPackages()
+	totalInstalls := 0
+	explicitInstalls := 0
+
+	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
 }