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