mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
75 lines
2.8 KiB
Go
75 lines
2.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package activities
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
"github.com/hanzoai/git/models/organization"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/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
|
|
}
|
|
|
|
// HINT: USER-ACTIVITY-PUSH-COMMITS: it only uses the doer's action time, it doesn't use git commit's time
|
|
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)
|
|
}
|