Skip to content

Commit

Permalink
Merge pull request #31 from TheDevMinerTV/refactor/estimated-stats-re…
Browse files Browse the repository at this point in the history
…porting

refactor(report): change estimated statistics reporting to match what community members suggested
  • Loading branch information
TheDevMinerTV authored Jul 18, 2024
2 parents cf6d830 + 1ce0c30 commit 8494760
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 48 deletions.
26 changes: 15 additions & 11 deletions dependency_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -280,7 +280,7 @@ type stats struct {
TotalDownloads uint64
DownloadsLastWeek *uint64
Size uint64
Subdependencies int64
Subdependencies uint64
}

func (s stats) Calculate() calculatedStats {
Expand Down Expand Up @@ -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))
}

Expand All @@ -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"
Expand All @@ -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 {
Expand Down
70 changes: 37 additions & 33 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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"),
Expand All @@ -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",
Expand All @@ -120,7 +120,7 @@ func printReport(
fmt.Printf(
" %s: %s %s\n",
bold.Sprint("Subdependencies"),
fmtInt(info.Subdependencies),
stats.FormattedSubdependencies(),
grayParens("%s%%", fmtPercent(pcSubdeps)),
)
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -261,49 +261,53 @@ 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),
grayParens("%s", scaledEstTrafficChangeFmt),
)
}

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 {
Expand Down
14 changes: 10 additions & 4 deletions version_size_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
Expand Down

0 comments on commit 8494760

Please sign in to comment.