Skip to content

Commit

Permalink
Don't make unnecessary paginations if found past days
Browse files Browse the repository at this point in the history
  • Loading branch information
Flexicon committed Sep 2, 2022
1 parent e254615 commit 6267f41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
22 changes: 20 additions & 2 deletions fetch_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ func searchCommits(query string, page int) ([]SearchResultItem, error) {
return nil, errors.Wrap(err, "failed to parse github api response")
}

// Recursively search for commits up to the set max page depth
if len(results.Items) < results.TotalCount && page < viper.GetInt("github.search_page_depth") {
// Recursively search for commits up to the set max page depth if available
hasNextPage := len(results.Items) < results.TotalCount && page < searchPageDepth()
// Traverse through available pages as long as past days haven't started to show up
if !containsDaysInThePast(results.Items) && hasNextPage {
// Wait a bit before searching again - GitHub doesn't like rapid fire search requests now
time.Sleep(5 * time.Second)

Expand Down Expand Up @@ -182,3 +184,19 @@ func saveCommitLog(db *gorm.DB, commits []*Commit) error {
func isSecondaryRateLimitReached(res *http.Response) bool {
return res.StatusCode == 403 && res.Header.Get("X-Ratelimit-Remaining") != "0"
}

func containsDaysInThePast(items []SearchResultItem) bool {
for _, item := range items {
commitDate, err := item.ParseCommitDate()

if err == nil && isDateInThePast(commitDate) {
return true
}
}

return false
}

func searchPageDepth() int {
return viper.GetInt("github.search_page_depth")
}
12 changes: 11 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ func today() time.Time {

// isDateInTheFuture returns true if the date is in the future.
func isDateInTheFuture(date time.Time) bool {
return date.UTC().After(time.Now().UTC())
return date.UTC().After(today().UTC())
}

// isDateInThePast returns true if the date is in the past.
//
// Only checks date - not time.
func isDateInThePast(date time.Time) bool {
y1, m1, d1 := date.UTC().Date()
y2, m2, d2 := today().UTC().Date()

return y1 < y2 || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 < d2)
}

// formatNumber returns a formatted string representation of the given number.
Expand Down

0 comments on commit 6267f41

Please sign in to comment.