Skip to content

Commit

Permalink
Only start fetching stars if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelef committed Nov 22, 2023
1 parent a1d22ae commit 592e9c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func main() {
result, _ = clientGQL.GetAllStats(ctx, "zappa/Zappa")
fmt.Println(result)

result, _ = clientGQL.GetAllStats(ctx, "omeid/uconfig")
result, _ = clientGQL.GetAllStats(ctx, "confluentinc/confluent-kafka-go")
fmt.Println(result)

result, _ = clientGQL.GetAllStats(ctx, "1set/gut")
fmt.Println(result)
}
33 changes: 26 additions & 7 deletions repostats/gqlstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (c *ClientGQL) getCommitsShortHistory(ctx context.Context, owner, name stri

currentTime := time.Now()

var uniqueAuthors = make(map[string]struct{})
uniqueAuthors := make(map[string]struct{})

variablesCommits := map[string]any{
"owner": githubv4.String(owner),
Expand Down Expand Up @@ -632,6 +632,8 @@ func (c *ClientGQL) GetCurrentLimits(ctx context.Context) (*RateLimit, error) {
func (c *ClientGQL) GetAllStats(ctx context.Context, ghRepo string) (*stats.RepoStats, error) {
result := stats.RepoStats{}

currentTime := time.Now()

ctx, span := tracer.Start(ctx, "fetch-repo-stats")
defer span.End()

Expand Down Expand Up @@ -662,6 +664,10 @@ func (c *ClientGQL) GetAllStats(ctx context.Context, ghRepo string) (*stats.Repo
}
}

type starred struct {
StarredAt time.Time
}

var query struct {
Repository struct {
Description string
Expand Down Expand Up @@ -690,6 +696,9 @@ func (c *ClientGQL) GetAllStats(ctx context.Context, ghRepo string) (*stats.Repo
} `graphql:"... on Commit"`
}
}
Stargazers struct {
Edges []starred
} `graphql:"stargazers(last: 1)"`
Releases struct {
TotalCount int
Edges []release
Expand Down Expand Up @@ -726,16 +735,26 @@ func (c *ClientGQL) GetAllStats(ctx context.Context, ghRepo string) (*stats.Repo
result.LastReleaseDate = releases[0].Node.CreatedAt
}

stars := query.Repository.Stargazers.Edges

if len(stars) > 0 && result.LastStarDate.IsZero() {
result.LastStarDate = stars[0].StarredAt
}

// 30d stars history
result.StarsHistory, err = c.getStarsHistory(ctx, repoSplit[0], repoSplit[1], result.Stars)
if err != nil {
log.Printf("%v\n", err)
return &result, err

days := currentTime.Sub(result.LastStarDate).Hours() / 24

if days < 30 {
result.StarsHistory, err = c.getStarsHistory(ctx, repoSplit[0], repoSplit[1], result.Stars)
if err != nil {
log.Printf("%v\n", err)
return &result, err
}
}

// 30d commits history
currentTime := time.Now()
days := currentTime.Sub(result.LastCommitDate).Hours() / 24
days = currentTime.Sub(result.LastCommitDate).Hours() / 24

if days < 30 {
result.CommitsHistory, err = c.getCommitsShortHistory(ctx, repoSplit[0], repoSplit[1], result.Commits)
Expand Down
1 change: 0 additions & 1 deletion stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ type RepoStats struct {
DefaultBranch string
MentionableUsers int
CreatedAt time.Time
LastCommitDate time.Time
LastReleaseDate time.Time
LivenessScore float32
StarsHistory
Expand Down

0 comments on commit 592e9c6

Please sign in to comment.