Load heatmap data asynchronously (#36622)
Fixes: https://github.com/go-gitea/gitea/issues/21045 - Move heatmap data loading from synchronous server-side rendering to async client-side fetch via dedicated JSON endpoints - Dashboard and user profile pages no longer block on the expensive heatmap DB query during HTML generation - Use compact `[[timestamp,count]]` JSON format instead of `[{"timestamp":N,"contributions":N}]` to reduce payload size - Public API (`/api/v1/users/{username}/heatmap`) remains unchanged - Heatmap rendering is unchanged, still shows a spinner as before, which will now spin a litte bit longer. Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
activities_model "code.gitea.io/gitea/models/activities"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
)
|
||||
|
||||
func prepareHeatmapURL(ctx *context.Context) {
|
||||
ctx.Data["EnableHeatmap"] = setting.Service.EnableUserHeatmap
|
||||
if !setting.Service.EnableUserHeatmap {
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.Org.Organization == nil {
|
||||
// for individual user
|
||||
ctx.Data["HeatmapURL"] = ctx.Doer.HomeLink() + "/-/heatmap"
|
||||
return
|
||||
}
|
||||
|
||||
// for org or team
|
||||
heatmapURL := ctx.Org.Organization.OrganisationLink() + "/dashboard/-/heatmap"
|
||||
if ctx.Org.Team != nil {
|
||||
heatmapURL += "/" + url.PathEscape(ctx.Org.Team.LowerName)
|
||||
}
|
||||
ctx.Data["HeatmapURL"] = heatmapURL
|
||||
}
|
||||
|
||||
func writeHeatmapJSON(ctx *context.Context, hdata []*activities_model.UserHeatmapData) {
|
||||
data := make([][2]int64, len(hdata))
|
||||
var total int64
|
||||
for i, v := range hdata {
|
||||
data[i] = [2]int64{int64(v.Timestamp), v.Contributions}
|
||||
total += v.Contributions
|
||||
}
|
||||
ctx.JSON(http.StatusOK, map[string]any{
|
||||
"heatmapData": data,
|
||||
"totalContributions": total,
|
||||
})
|
||||
}
|
||||
|
||||
// DashboardHeatmap returns heatmap data as JSON, for the individual user, organization or team dashboard.
|
||||
func DashboardHeatmap(ctx *context.Context) {
|
||||
if !setting.Service.EnableUserHeatmap {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
var data []*activities_model.UserHeatmapData
|
||||
var err error
|
||||
if ctx.Org.Organization == nil {
|
||||
data, err = activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer)
|
||||
} else {
|
||||
data, err = activities_model.GetUserHeatmapDataByOrgTeam(ctx, ctx.Org.Organization, ctx.Org.Team, ctx.Doer)
|
||||
}
|
||||
if err != nil {
|
||||
ctx.ServerError("GetUserHeatmapData", err)
|
||||
return
|
||||
}
|
||||
writeHeatmapJSON(ctx, data)
|
||||
}
|
||||
Reference in New Issue
Block a user