gitea_hat/models/activities/user_heatmap.go

56 lines
1.5 KiB
Go

package activities
import (
gitea_activities_models "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
gitea_user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
)
type UserHeatmapData struct {
Timestamp timeutil.TimeStamp `json:"timestamp"`
Contributions int64 `json:"contributions"`
}
func GetUserHeatmapDataByTimestampRange(user *gitea_user_model.User, team *organization.Team, doer *gitea_user_model.User, startAt, endAt timeutil.TimeStamp) ([]*UserHeatmapData, error) {
hdata := make([]*UserHeatmapData, 0)
if !gitea_activities_models.ActivityReadable(user, doer) {
return hdata, nil
}
groupBy := "created_unix / 900 * 900"
groupByName := "timestamp"
switch {
case setting.Database.UseMySQL:
groupBy = "created_unix DIV 900 * 900"
case setting.Database.UseMSSQL:
groupByName = groupBy
}
cond, err := activityQueryCondition(gitea_activities_models.GetFeedsOptions{
RequestedUser: user,
RequestedTeam: team,
Actor: doer,
IncludePrivate: true,
IncludeDeleted: true,
OnlyPerformedBy: !user.IsOrganization(),
})
if err != nil {
return nil, err
}
return hdata, db.GetEngine(db.DefaultContext).
Select(groupBy+" AS timestamp, count(user_id) as contributions").
Table("action").
Where(cond).
And("created_unix >= ?", startAt).
And("created_unix <= ?", endAt).
GroupBy(groupByName).
OrderBy("timestamp").
Find(&hdata)
}