Skip to content

Commit

Permalink
Added liveness score
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelef committed Nov 18, 2023
1 parent eac8f66 commit eb2b738
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,12 @@ github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgI
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.0 h1:1eHu3/pUSWaOgltNK3WJFaywKsTIr/PwvHyDmi0lQA0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.0/go.mod h1:HyABWq60Uy1kjJSa2BVOxUVao8Cdick5AWSKPutqy6U=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo=
go.opentelemetry.io/otel v1.20.0 h1:vsb/ggIY+hUjD/zCAQHpzTmndPqv/ml2ArbsbfBYTAc=
go.opentelemetry.io/otel v1.20.0/go.mod h1:oUIGj3D77RwJdM6PPZImDpSZGDvkD9fhesHny69JFrs=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/metric v1.20.0 h1:ZlrO8Hu9+GAhnepmRGhSU7/VkpjrNowxRN9GyKR4wzA=
go.opentelemetry.io/otel/metric v1.20.0/go.mod h1:90DRw3nfK4D7Sm/75yQ00gTJxtkBxX+wu6YaNymbpVM=
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
go.opentelemetry.io/otel/trace v1.20.0 h1:+yxVAPZPbQhbC3OfAkeIVTky6iTFpcr4SiY9om7mXSQ=
go.opentelemetry.io/otel/trace v1.20.0/go.mod h1:HJSK7F/hA5RlzpZ0zKDCHCDHm556LCDtKaAo6JmBFUU=
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
78 changes: 78 additions & 0 deletions repostats/gqlstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,87 @@ func (c *ClientGQL) GetAllStats(ctx context.Context, ghRepo string) (*stats.Repo
depFetcher.GetDepsList(ctx, c.restyClient, ghRepo, &result)
}

getLivenessScore(ctx, c.restyClient, ghRepo, &result)

return &result, nil
}

func getLivenessScore(ctx context.Context, restyClient *resty.Client, ghRepo string, result *stats.RepoStats) {
score := float32(0.0)

// calculate days since last commit
if !result.LastCommitDate.IsZero() {
days := time.Now().Sub(result.LastCommitDate).Hours() / 24

switch {
case days <= 1:
score += 30
break
case days < 7:
score += 10
break
case days < 14:
score += 5
break
case days < 30:
score += 2
break
}
}

// calculate days since last star
if !result.LastStarDate.IsZero() {
days := time.Now().Sub(result.LastStarDate).Hours() / 24
switch {
case days <= 1:
score += 20
break
case days < 7:
score += 10
break
case days < 14:
score += 5
break
case days < 30:
score += 2
break
}
}

switch {
case result.AddedLast14d > 30:
score += 30
break
case result.AddedLast14d > 20:
score += 20
break
case result.AddedLast14d > 5:
score += 5
break
}

switch {
case result.AddedLast24H > 30:
score += 10
break
case result.AddedLast24H > 20:
score += 5
break
case result.AddedLast24H > 5:
score += 2
break
}

if result.Archived {
score -= 30
}

// score should be between 0 and 100
score = float32(math.Max(0, math.Min(100, float64(score))))

result.LivenessScore = score
}

func (c *ClientGQL) GetTotalStars(ctx context.Context, ghRepo string) (int, time.Time, error) {
repoSplit := strings.Split(ghRepo, "/")

Expand Down
3 changes: 3 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type RepoStats struct {
CreatedAt time.Time
LastCommitDate time.Time
LastReleaseDate time.Time
LivenessScore float32
StarsHistory
GoRepo
}
Expand All @@ -84,6 +85,7 @@ Archived: %t
Mentionable Users: %d
Default Branch: %s
%s
Liveness Score: %.2f
Go version: %s
Go Direct dependencies: %d
`, rs.GHPath,
Expand All @@ -99,6 +101,7 @@ Go Direct dependencies: %d
rs.MentionableUsers,
rs.DefaultBranch,
rs.StarsHistory,
rs.LivenessScore,
rs.GoVersion,
len(rs.DirectDeps))
}

0 comments on commit eb2b738

Please sign in to comment.