From ddbc24c46b4876449b8d69bba1d660e74869dfec Mon Sep 17 00:00:00 2001 From: Granular9241 Date: Wed, 21 May 2025 05:25:39 +0200 Subject: [PATCH] fix: include activity needed for entire heatmap (#7893) Problem: Only data from the past 365 days (31536000s) is loaded. However, on the activity heatmap, there are 53 cols, each is 7 rows, so squares for at most 371 days (32054400s) are on the heatmap. This results in a few blank squares on the very left side, despite activity. Solution: Simply changing 31536000s (365d) to 32054400s (371d) to load all the needed activity. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7893 Reviewed-by: Gusted Reviewed-by: 0ko <0ko@noreply.codeberg.org> Co-authored-by: Granular9241 Co-committed-by: Granular9241 --- models/activities/user_heatmap.go | 8 +++++++- models/activities/user_heatmap_test.go | 4 ++++ models/fixtures/action.yml | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) 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