diff --git a/models/activities/user_heatmap.go b/models/activities/user_heatmap.go index 0cc3f759c6..11badb77e2 100644 --- a/models/activities/user_heatmap.go +++ b/models/activities/user_heatmap.go @@ -13,6 +13,12 @@ import ( "forgejo.org/modules/timeutil" ) +const ( + // contributionsMaxAgeSeconds How old data to retrieve for the heatmap. + // 371 days to cover the entire heatmap (53 *full* weeks) + contributionsMaxAgeSeconds = 32054400 +) + // UserHeatmapData represents the data needed to create a heatmap type UserHeatmapData struct { Timestamp timeutil.TimeStamp `json:"timestamp"` @@ -62,7 +68,7 @@ func getUserHeatmapData(ctx context.Context, user *user_model.User, team *organi Select(groupBy+" AS timestamp, count(user_id) as contributions"). Table("action"). Where(cond). - And("created_unix > ?", timeutil.TimeStampNow()-31536000). + And("created_unix >= ?", timeutil.TimeStampNow()-contributionsMaxAgeSeconds). GroupBy("timestamp"). OrderBy("timestamp"). Find(&hdata) diff --git a/models/activities/user_heatmap_test.go b/models/activities/user_heatmap_test.go index d922f9a78b..34308cb3d4 100644 --- a/models/activities/user_heatmap_test.go +++ b/models/activities/user_heatmap_test.go @@ -54,6 +54,10 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { "multiple actions performed with two grouped together", 10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`, }, + { + "test cutoff within", + 40, 40, 1, `[{"timestamp":1577404800,"contributions":1}]`, + }, } // Prepare require.NoError(t, unittest.PrepareTestDatabase()) diff --git a/models/fixtures/action.yml b/models/fixtures/action.yml index b2febb4ed8..f1592d4569 100644 --- a/models/fixtures/action.yml +++ b/models/fixtures/action.yml @@ -74,3 +74,11 @@ is_private: false created_unix: 1680454039 content: '4|' # issueId 5 + +- id: 10 + user_id: 40 + op_type: 1 # create repo + act_user_id: 40 + repo_id: 60 # public + is_private: false + created_unix: 1577404800 # end of heatmap \ No newline at end of file