Monorepo for Tangled tangled.org

appview/{db,state}: handle index out-of-bound for punchcards

The date compare should be done with years.

Also, previous puchcard didn't work except for December because `idx`
can be negative while iterating the punchcard. Not sure why most
account pages aren't affected

Signed-off-by: Seongmin Lee <git@boltless.me>

boltless.me 352e99aa 422468f9

verified
Changed files
+26 -34
appview
+22 -31
appview/db/profile.go
··· 16 17 const TimeframeMonths = 7 18 19 func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) { 20 timeline := models.ProfileTimeline{ 21 ByMonth: make([]models.ByMonth, TimeframeMonths), 22 } 23 - currentMonth := time.Now().Month() 24 timeframe := fmt.Sprintf("-%d months", TimeframeMonths) 25 26 pulls, err := GetPullsByOwnerDid(e, forDid, timeframe) ··· 30 31 // group pulls by month 32 for _, pull := range pulls { 33 - pullMonth := pull.Created.Month() 34 35 - if currentMonth-pullMonth >= TimeframeMonths { 36 - // shouldn't happen; but times are weird 37 - continue 38 } 39 - 40 - idx := currentMonth - pullMonth 41 - items := &timeline.ByMonth[idx].PullEvents.Items 42 - 43 - *items = append(*items, &pull) 44 } 45 46 issues, err := GetIssues( ··· 53 } 54 55 for _, issue := range issues { 56 - issueMonth := issue.Created.Month() 57 58 - if currentMonth-issueMonth >= TimeframeMonths { 59 - // shouldn't happen; but times are weird 60 - continue 61 } 62 - 63 - idx := currentMonth - issueMonth 64 - items := &timeline.ByMonth[idx].IssueEvents.Items 65 - 66 - *items = append(*items, &issue) 67 } 68 69 repos, err := GetRepos(e, 0, orm.FilterEq("did", forDid)) ··· 81 } 82 } 83 84 - repoMonth := repo.Created.Month() 85 86 - if currentMonth-repoMonth >= TimeframeMonths { 87 - // shouldn't happen; but times are weird 88 - continue 89 } 90 - 91 - idx := currentMonth - repoMonth 92 - 93 - items := &timeline.ByMonth[idx].RepoEvents 94 - *items = append(*items, models.RepoEvent{ 95 - Repo: &repo, 96 - Source: sourceRepo, 97 - }) 98 } 99 100 return &timeline, nil
··· 16 17 const TimeframeMonths = 7 18 19 + func MonthsApart(from, to time.Time) int { 20 + fromYear, fromMonth, _ := from.Date() 21 + toYear, toMonth, _ := to.Date() 22 + return (toYear-fromYear)*12 + int(toMonth-fromMonth) 23 + } 24 + 25 func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) { 26 timeline := models.ProfileTimeline{ 27 ByMonth: make([]models.ByMonth, TimeframeMonths), 28 } 29 + now := time.Now() 30 timeframe := fmt.Sprintf("-%d months", TimeframeMonths) 31 32 pulls, err := GetPullsByOwnerDid(e, forDid, timeframe) ··· 36 37 // group pulls by month 38 for _, pull := range pulls { 39 + idx := MonthsApart(pull.Created, now) 40 41 + if 0 <= idx && idx < TimeframeMonths { 42 + items := &timeline.ByMonth[idx].PullEvents.Items 43 + *items = append(*items, &pull) 44 } 45 } 46 47 issues, err := GetIssues( ··· 54 } 55 56 for _, issue := range issues { 57 + idx := MonthsApart(issue.Created, now) 58 59 + if 0 <= idx && idx < TimeframeMonths { 60 + items := &timeline.ByMonth[idx].IssueEvents.Items 61 + *items = append(*items, &issue) 62 } 63 } 64 65 repos, err := GetRepos(e, 0, orm.FilterEq("did", forDid)) ··· 77 } 78 } 79 80 + idx := MonthsApart(repo.Created, now) 81 82 + if 0 <= idx && idx < TimeframeMonths { 83 + items := &timeline.ByMonth[idx].RepoEvents 84 + *items = append(*items, models.RepoEvent{ 85 + Repo: &repo, 86 + Source: sourceRepo, 87 + }) 88 } 89 } 90 91 return &timeline, nil
+4 -3
appview/state/profile.go
··· 163 } 164 165 // populate commit counts in the timeline, using the punchcard 166 - currentMonth := time.Now().Month() 167 for _, p := range profile.Punchcard.Punches { 168 - idx := currentMonth - p.Date.Month() 169 - if int(idx) < len(timeline.ByMonth) { 170 timeline.ByMonth[idx].Commits += p.Count 171 } 172 }
··· 163 } 164 165 // populate commit counts in the timeline, using the punchcard 166 + now := time.Now() 167 for _, p := range profile.Punchcard.Punches { 168 + idx := db.MonthsApart(p.Date, now) 169 + 170 + if 0 <= idx && idx < len(timeline.ByMonth) { 171 timeline.ByMonth[idx].Commits += p.Count 172 } 173 }