mirror of
https://github.com/go-gitea/gitea.git
synced 2026-02-25 05:56:21 +01:00
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>
74 lines
2.7 KiB
Go
74 lines
2.7 KiB
Go
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package activities
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/models/organization"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
)
|
|
|
|
// UserHeatmapData represents the data needed to create a heatmap
|
|
type UserHeatmapData struct {
|
|
Timestamp timeutil.TimeStamp `json:"timestamp"`
|
|
Contributions int64 `json:"contributions"`
|
|
}
|
|
|
|
// GetUserHeatmapDataByUser returns an array of UserHeatmapData, it checks whether doer can access user's activity
|
|
func GetUserHeatmapDataByUser(ctx context.Context, user, doer *user_model.User) ([]*UserHeatmapData, error) {
|
|
return getUserHeatmapData(ctx, user, nil, doer)
|
|
}
|
|
|
|
// GetUserHeatmapDataByOrgTeam returns an array of UserHeatmapData, it checks whether doer can access org's activity
|
|
func GetUserHeatmapDataByOrgTeam(ctx context.Context, org *organization.Organization, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
|
|
return getUserHeatmapData(ctx, org.AsUser(), team, doer)
|
|
}
|
|
|
|
func getUserHeatmapData(ctx context.Context, user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
|
|
hdata := make([]*UserHeatmapData, 0)
|
|
|
|
if !ActivityReadable(user, doer) {
|
|
return hdata, nil
|
|
}
|
|
|
|
// Group by 15 minute intervals which will allow the client to accurately shift the timestamp to their timezone.
|
|
// The interval is based on the fact that there are timezones such as UTC +5:30 and UTC +12:45.
|
|
groupBy := "created_unix / 900 * 900"
|
|
groupByName := "timestamp" // We need this extra case because mssql doesn't allow grouping by alias
|
|
switch {
|
|
case setting.Database.Type.IsMySQL():
|
|
groupBy = "created_unix DIV 900 * 900"
|
|
case setting.Database.Type.IsMSSQL():
|
|
groupByName = groupBy
|
|
}
|
|
|
|
cond, err := ActivityQueryCondition(ctx, GetFeedsOptions{
|
|
RequestedUser: user,
|
|
RequestedTeam: team,
|
|
Actor: doer,
|
|
IncludePrivate: true, // don't filter by private, as we already filter by repo access
|
|
IncludeDeleted: true,
|
|
// * Heatmaps for individual users only include actions that the user themself did.
|
|
// * For organizations actions by all users that were made in owned
|
|
// repositories are counted.
|
|
OnlyPerformedBy: !user.IsOrganization(),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return hdata, db.GetEngine(ctx).
|
|
Select(groupBy+" AS timestamp, count(user_id) as contributions").
|
|
Table("action").
|
|
Where(cond).
|
|
And("created_unix > ?", timeutil.TimeStampNow()-(366+7)*86400). // (366+7) days to include the first week for the heatmap
|
|
GroupBy(groupByName).
|
|
OrderBy("timestamp").
|
|
Find(&hdata)
|
|
}
|