From 1ce0c3050ebc7e182b718c611d14cfb5f1d95c1e Mon Sep 17 00:00:00 2001 From: DevMiner Date: Thu, 18 Jul 2024 22:16:44 +0200 Subject: [PATCH] refactor(report): change estimated statistics reporting to match what community members suggested Fixes #28 --- dependency_change.go | 26 +++++++++------- report.go | 70 +++++++++++++++++++++++--------------------- version_size_diff.go | 14 ++++++--- 3 files changed, 62 insertions(+), 48 deletions(-) diff --git a/dependency_change.go b/dependency_change.go index d91847a..fccd537 100644 --- a/dependency_change.go +++ b/dependency_change.go @@ -56,7 +56,7 @@ func replaceDeps() { if err != nil { l.Error().Err(err).Msg("Failed to parse package-lock.json") } else { - dep.Subdependencies = int64(len(lock.Packages)) + dep.Subdependencies = uint64(len(lock.Packages)) } l.Info().Msgf("Package size: %s", humanize.Bytes(dep.Size)) @@ -254,7 +254,7 @@ func promptPackage(npmClient *npm.Client, dockerC *docker_client.Client) *packag TotalDownloads: downloads.Total(), DownloadsLastWeek: downloadsLastWeek, Size: size, - Subdependencies: int64(len(b.Lockfile.Packages)), + Subdependencies: uint64(len(b.Lockfile.Packages)), }.Calculate() return b @@ -280,7 +280,7 @@ type stats struct { TotalDownloads uint64 DownloadsLastWeek *uint64 Size uint64 - Subdependencies int64 + Subdependencies uint64 } func (s stats) Calculate() calculatedStats { @@ -310,10 +310,10 @@ type calculatedStats struct { TrafficLastWeek *uint64 PercentDownloadsOfVersion *float64 Size uint64 - Subdependencies int64 + Subdependencies uint64 } -func (s calculatedStats) PercentOfPackageSubdependencies(outer int64) float64 { +func (s calculatedStats) PercentOfPackageSubdependencies(outer uint64) float64 { return calculatePercentage(float64(s.Subdependencies), float64(outer)) } @@ -329,6 +329,14 @@ func (s calculatedStats) FormattedPercentOfPackageTraffic(outer uint64) string { return fmtPercent(calculatePercentage(float64(*s.TrafficLastWeek), float64(outer))) } +func (s calculatedStats) FormattedPercentDownloadsOfVersion() string { + if s.PercentDownloadsOfVersion == nil { + return "N/A" + } + + return fmtPercent(*s.PercentDownloadsOfVersion) +} + func (s calculatedStats) FormattedTrafficLastWeek() string { if s.TrafficLastWeek == nil { return "N/A" @@ -345,12 +353,8 @@ func (s calculatedStats) FormattedDownloadsLastWeek() string { return fmtInt(int64(*s.DownloadsLastWeek)) } -func (s calculatedStats) FormattedPercentDownloadsOfVersion() string { - if s.PercentDownloadsOfVersion == nil { - return "N/A" - } - - return fmtPercent(*s.PercentDownloadsOfVersion) +func (s calculatedStats) FormattedSubdependencies() string { + return fmtInt(int64(s.Subdependencies)) } func calculatePercentage(part, total float64) float64 { diff --git a/report.go b/report.go index 0db1212..410f63b 100644 --- a/report.go +++ b/report.go @@ -33,7 +33,7 @@ func printReport( packageJson := package_.JSON downloadsLastWeek := modifiedPackage.Stats.DownloadsLastWeek oldPackageSize := modifiedPackage.Stats.Size - oldSubdependencies := int64(len(modifiedPackage.Lockfile.Packages)) + oldSubdependencies := uint64(len(modifiedPackage.Lockfile.Packages)) modifiedPackageName := boldYellow.Sprint(packageJson.String()) @@ -67,21 +67,21 @@ func printReport( } for _, p := range removedDependencies { - info := deps[p.String()] + stats := deps[p.String()] - pcDLs := info.FormattedPercentDownloadsOfVersion() - pcSize := float64(info.Size) * 100 / float64(oldPackageSize) - pcTrafficOfPackageFmt := info.FormattedPercentOfPackageTraffic(oldPackageSize) - pcSubdeps := info.PercentOfPackageSubdependencies(oldSubdependencies) + pcDLs := stats.FormattedPercentDownloadsOfVersion() + pcSize := float64(stats.Size) * 100 / float64(oldPackageSize) + pcTrafficOfPackageFmt := stats.FormattedPercentOfPackageTraffic(oldPackageSize) + pcSubdeps := stats.PercentOfPackageSubdependencies(oldSubdependencies) upperDLsFmt := modifiedPackage.Stats.FormattedDownloadsLastWeek() upperDLsTrafficFmt := modifiedPackage.Stats.FormattedTrafficLastWeek() - dlsFmt := info.FormattedDownloadsLastWeek() - trafficFmt := info.FormattedTrafficLastWeek() + dlsFmt := stats.FormattedDownloadsLastWeek() + trafficFmt := stats.FormattedTrafficLastWeek() if *fShortMode { - fmt.Printf(" %s %s: %s\n", color.RedString("-"), boldYellow.Sprint(p.String()), humanize.Bytes(info.Size)) + fmt.Printf(" %s %s: %s\n", color.RedString("-"), boldYellow.Sprint(p.String()), humanize.Bytes(stats.Size)) fmt.Printf( " %s: %s %s\n", bold.Sprint("DLs last week"), @@ -96,14 +96,14 @@ func printReport( " %s %s: %s %s\n", color.RedString("-"), boldYellow.Sprint(p.String()), - humanize.Bytes(info.Size), + humanize.Bytes(stats.Size), grayParens("%s%%", fmtPercent(pcSize)), ) fmt.Printf( " %s: %s %s\n", bold.Sprint("Downloads last week"), dlsFmt, - grayParens("%s%% from %s", pcDLs, boldYellow.Sprint(info.Version)), + grayParens("%s%% from %s", pcDLs, boldYellow.Sprint(stats.Version)), ) fmt.Printf( " %s: %s %s\n", @@ -120,7 +120,7 @@ func printReport( fmt.Printf( " %s: %s %s\n", bold.Sprint("Subdependencies"), - fmtInt(info.Subdependencies), + stats.FormattedSubdependencies(), grayParens("%s%%", fmtPercent(pcSubdeps)), ) } @@ -164,15 +164,14 @@ func printReport( fmt.Printf( " %s: %s %s\n", bold.Sprint("Subdependencies"), - fmtInt(info.Subdependencies), + info.FormattedSubdependencies(), grayParens("%s%%", fmtPercent(pcSubdeps)), ) } } fmt.Println() - reportSizeDifference(oldPackageSize, newPackageSize, downloadsLastWeek, modifiedPackage.Stats.TotalDownloads) - reportSubdependencies(oldSubdependencies, newSubdependencies) + reportEstimatedStatistics(oldPackageSize, newPackageSize, downloadsLastWeek, modifiedPackage.Stats.TotalDownloads, oldSubdependencies, newSubdependencies) } func reportPackageInfo(modifiedPackage *packageInfo, showLatestVersionHint bool, indentation int) { @@ -211,7 +210,7 @@ func reportPackageInfo(modifiedPackage *packageInfo, showLatestVersionHint bool, grayParens("%s%%", dlsFmt), ) fmt.Printf("%s %s: %s\n", indent, bold.Sprint("Estimated traffic last week"), modifiedPackage.Stats.FormattedTrafficLastWeek()) - fmt.Printf("%s %s: %s\n", indent, bold.Sprint("Subdependencies"), fmtInt(modifiedPackage.Stats.Subdependencies)) + fmt.Printf("%s %s: %s\n", indent, bold.Sprint("Subdependencies"), modifiedPackage.Stats.FormattedSubdependencies()) if showLatestVersionHint { latestVersion := packageInfo.LatestVersion @@ -226,7 +225,7 @@ func reportPackageInfo(modifiedPackage *packageInfo, showLatestVersionHint bool, } } -func reportSizeDifference(oldSize uint64, newSize uint64, downloads *uint64, totalDownloads uint64) { +func reportEstimatedStatistics(oldSize, newSize uint64, downloads *uint64, totalDownloads, oldSubdependencies, newSubdependencies uint64) { indicatorColor := boldGreen if newSize > oldSize { indicatorColor = boldRed @@ -239,6 +238,7 @@ func reportSizeDifference(oldSize uint64, newSize uint64, downloads *uint64, tot oldTrafficLastWeekFmt, estNewTrafficFmt, estTrafficChangeFmt := formattedTraffic(downloads, oldSize, newSize) scaledOldTrafficLastWeekFmt, scaledEstTrafficNextWeekFmt, scaledEstTrafficChangeFmt := formattedTraffic(&totalDownloads, oldSize, newSize) + oldSubdepsFmt, estSubdepsFmt, subdepsChangeFmt := reportSubdependencies(oldSubdependencies, newSubdependencies) if *fShortMode { fmt.Printf( @@ -261,25 +261,35 @@ func reportSizeDifference(oldSize uint64, newSize uint64, downloads *uint64, tot return } + bold.Println("Estimated new statistics:") fmt.Printf( - "%s: %s %s %s %s\n", - bold.Sprint("Estimated package size"), + " %s: %s %s %s %s\n", + bold.Sprint("Package size"), humanize.Bytes(oldSize), arrow, indicatorColor.Sprintf(humanize.Bytes(newSize)), grayParens("%s", pcSizeFmt), ) fmt.Printf( - "%s: %s %s %s %s\n", - bold.Sprint("Estimated traffic over a week"), + " %s: %s %s %s %s\n", + bold.Sprint("Subdependencies"), + oldSubdepsFmt, + arrow, + estSubdepsFmt, + grayParens("%s", subdepsChangeFmt), + ) + bold.Println(" Traffic with last week's downloads:") + fmt.Printf( + " %s: %s %s %s %s\n", + bold.Sprint("For current version"), oldTrafficLastWeekFmt, arrow, estNewTrafficFmt, grayParens("%s", estTrafficChangeFmt), ) fmt.Printf( - "%s: %s %s %s %s\n", - bold.Sprint("Estimated traffic over a week @ 100% downloads"), + " %s: %s %s %s %s\n", + bold.Sprint("For all versions"), scaledOldTrafficLastWeekFmt, arrow, indicatorColor.Sprint(scaledEstTrafficNextWeekFmt), @@ -287,23 +297,17 @@ func reportSizeDifference(oldSize uint64, newSize uint64, downloads *uint64, tot ) } -func reportSubdependencies(oldSubdependencies, newSubdependencies int64) { +func reportSubdependencies(oldSubdependencies, newSubdependencies uint64) (string, string, string) { indicatorColor := boldGray if oldSubdependencies > newSubdependencies { indicatorColor = boldGreen } else if oldSubdependencies < newSubdependencies { indicatorColor = boldRed } - subdepsFmt := indicatorColor.Sprint(fmtInt(newSubdependencies)) + subdepsFmt := indicatorColor.Sprint(fmtInt(int64(newSubdependencies))) + difference := newSubdependencies - oldSubdependencies - fmt.Printf( - "%s: %s %s %s %s\n", - bold.Sprint("Subdependencies"), - fmtInt(oldSubdependencies), - arrow, - subdepsFmt, - grayParens("%s", indicatorColor.Sprint(fmtInt(newSubdependencies-oldSubdependencies))), - ) + return fmtInt(int64(oldSubdependencies)), subdepsFmt, indicatorColor.Sprint(fmtInt(int64(difference))) } func grayParens(s string, args ...any) string { diff --git a/version_size_diff.go b/version_size_diff.go index bdb5e3c..71712c8 100644 --- a/version_size_diff.go +++ b/version_size_diff.go @@ -20,8 +20,14 @@ func calculateVersionSizeChange() { fmt.Println() reportPackageInfo(&pkg.New, false, 0) fmt.Println() - reportSizeDifference(pkg.Old.Stats.Size, pkg.New.Stats.Size, pkg.Old.Stats.DownloadsLastWeek, pkg.New.Stats.TotalDownloads) - reportSubdependencies(int64(len(pkg.Old.Lockfile.Packages)), int64(len(pkg.New.Lockfile.Packages))) + reportEstimatedStatistics( + pkg.Old.Stats.Size, + pkg.New.Stats.Size, + pkg.Old.Stats.DownloadsLastWeek, + pkg.New.Stats.TotalDownloads, + pkg.Old.Stats.Subdependencies, + pkg.New.Stats.Subdependencies, + ) } func promptPackageVersions(npmClient *npm.Client, dockerC *docker_client.Client) *packageVersionsInfo { @@ -109,7 +115,7 @@ func promptPackageVersions(npmClient *npm.Client, dockerC *docker_client.Client) log.Fatal().Err(err).Msg("Failed to parse old package-lock.json") } - oldStats.Subdependencies = int64(len(b.Old.Lockfile.Packages)) + oldStats.Subdependencies = uint64(len(b.Old.Lockfile.Packages)) }() go func() { @@ -125,7 +131,7 @@ func promptPackageVersions(npmClient *npm.Client, dockerC *docker_client.Client) log.Fatal().Err(err).Msg("Failed to parse new package-lock.json") } - newStats.Subdependencies = int64(len(b.New.Lockfile.Packages)) + newStats.Subdependencies = uint64(len(b.New.Lockfile.Packages)) }() wg.Wait()