Skip to content
This repository has been archived by the owner on Oct 12, 2020. It is now read-only.

Commit

Permalink
Fix infinite loading & percentile calculation on small samples (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ullaakut committed Aug 13, 2020
1 parent f9a4875 commit 001aa12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func detectFakeStars(ctx *context.Context) error {
disgo.Infoln(style.Important("This repository appears to have a low amount of stargazers. Trust calculations might not be accurate."))
}

// For now, we only fetch contributions until 2013. It will be configurable later on
// For now, we only fetch contributions since 2013. It will be configurable later on
// once the algorithm is more accurate and more data has been fetched.
if !ctx.ScanAll && totalUsers > ctx.Stars {
disgo.Infof("Fetching contributions for %d users up to year %d\n", ctx.Stars, 2013)
Expand Down
7 changes: 4 additions & 3 deletions pkg/gql/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"strings"
"time"

"github.com/cenkalti/backoff/v3"
"github.com/Ullaakut/astronomer/pkg/context"
"github.com/Ullaakut/disgo"
"github.com/Ullaakut/disgo/style"
"github.com/cenkalti/backoff/v3"
"github.com/vbauerster/mpb/v4"
"github.com/vbauerster/mpb/v4/decor"
)
Expand Down Expand Up @@ -190,7 +190,7 @@ func FetchContributions(ctx *context.Context, cursors []string, untilYear int) (
requestBody := buildRequestBody(ctx, fetchContributionsRequest, contribPagination)
client := &http.Client{}

progress, bar := setupProgressBar(len(cursors) + 1)
progress, bar := setupProgressBar(len(cursors))
defer progress.Wait()

// If we are scanning only a portion of stargazers, the
Expand All @@ -209,7 +209,6 @@ func FetchContributions(ctx *context.Context, cursors []string, untilYear int) (
// Iterate on pages of user contributions, following the cursors generated
// in fetchStargazers.
for page := 1; page <= totalPages; page++ {

currentCursor := getCursor(cursors, page, isReverseOrder)

// If this isn't the first page, inject the cursor value.
Expand Down Expand Up @@ -333,6 +332,8 @@ func FetchContributions(ctx *context.Context, cursors []string, untilYear int) (
}
}

bar.Abort(true)

return users, nil
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/trust/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strconv"
"time"

"github.com/montanaflynn/stats"
"github.com/Ullaakut/astronomer/pkg/context"
"github.com/Ullaakut/astronomer/pkg/gql"
"github.com/Ullaakut/disgo"
"github.com/Ullaakut/disgo/style"
"github.com/montanaflynn/stats"
)

// Factor represents one of the trust factors used to compte
Expand Down Expand Up @@ -181,6 +181,12 @@ func buildComparativeReport(trustData map[FactorName][]float64) (*Report, error)
}

for _, percentile := range percentiles {
// Skip percentiles if the random sample is too small to have percentiles.
if currentStarsReport.Percentiles[percentile].TrustPercent == 0 {
report.Percentiles = nil
break
}

if firstStarsReport.Percentiles[percentile].TrustPercent <= currentStarsReport.Percentiles[percentile].TrustPercent {
report.Percentiles[percentile] = firstStarsReport.Percentiles[percentile]
} else {
Expand All @@ -196,6 +202,10 @@ func buildComparativeReport(trustData map[FactorName][]float64) (*Report, error)
}

for _, percentile := range percentiles {
// Skip percentiles if the random sample is too small to have percentiles.
if report.Percentiles[percentile].TrustPercent == 0 {
break
}
allTrust = append(allTrust, report.Percentiles[percentile].TrustPercent)
}

Expand Down

0 comments on commit 001aa12

Please sign in to comment.