diff --git a/appview/db/profile.go b/appview/db/profile.go index 0184b7ec..745b7969 100644 --- a/appview/db/profile.go +++ b/appview/db/profile.go @@ -20,7 +20,7 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro timeline := models.ProfileTimeline{ ByMonth: make([]models.ByMonth, TimeframeMonths), } - currentMonth := time.Now().Month() + now := time.Now() timeframe := fmt.Sprintf("-%d months", TimeframeMonths) pulls, err := GetPullsByOwnerDid(e, forDid, timeframe) @@ -30,14 +30,14 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro // group pulls by month for _, pull := range pulls { - pullMonth := pull.Created.Month() + monthsAgo := monthsBetween(pull.Created, now) - if currentMonth-pullMonth >= TimeframeMonths { + if monthsAgo >= TimeframeMonths { // shouldn't happen; but times are weird continue } - idx := currentMonth - pullMonth + idx := monthsAgo items := &timeline.ByMonth[idx].PullEvents.Items *items = append(*items, &pull) @@ -53,14 +53,14 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro } for _, issue := range issues { - issueMonth := issue.Created.Month() + monthsAgo := monthsBetween(issue.Created, now) - if currentMonth-issueMonth >= TimeframeMonths { + if monthsAgo >= TimeframeMonths { // shouldn't happen; but times are weird continue } - idx := currentMonth - issueMonth + idx := monthsAgo items := &timeline.ByMonth[idx].IssueEvents.Items *items = append(*items, &issue) @@ -77,18 +77,19 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro if repo.Source != "" { sourceRepo, err = GetRepoByAtUri(e, repo.Source) if err != nil { - return nil, err + // the source repo was not found, skip this bit + log.Println("profile", "err", err) } } - repoMonth := repo.Created.Month() + monthsAgo := monthsBetween(repo.Created, now) - if currentMonth-repoMonth >= TimeframeMonths { + if monthsAgo >= TimeframeMonths { // shouldn't happen; but times are weird continue } - idx := currentMonth - repoMonth + idx := monthsAgo items := &timeline.ByMonth[idx].RepoEvents *items = append(*items, models.RepoEvent{ @@ -100,6 +101,12 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro return &timeline, nil } +func monthsBetween(from, to time.Time) int { + years := to.Year() - from.Year() + months := int(to.Month() - from.Month()) + return years*12 + months +} + func UpsertProfile(tx *sql.Tx, profile *models.Profile) error { defer tx.Rollback() diff --git a/appview/state/profile.go b/appview/state/profile.go index 400725ac..576a934e 100644 --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -163,11 +163,13 @@ func (s *State) profileOverview(w http.ResponseWriter, r *http.Request) { } // populate commit counts in the timeline, using the punchcard - currentMonth := time.Now().Month() + now := time.Now() for _, p := range profile.Punchcard.Punches { - idx := currentMonth - p.Date.Month() - if int(idx) < len(timeline.ByMonth) { - timeline.ByMonth[idx].Commits += p.Count + years := now.Year() - p.Date.Year() + months := int(now.Month() - p.Date.Month()) + monthsAgo := years*12 + months + if monthsAgo >= 0 && monthsAgo < len(timeline.ByMonth) { + timeline.ByMonth[monthsAgo].Commits += p.Count } }