You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_heatmap.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.package models
  4. package models
  5. import (
  6. "code.gitea.io/gitea/modules/setting"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. )
  9. // UserHeatmapData represents the data needed to create a heatmap
  10. type UserHeatmapData struct {
  11. Timestamp timeutil.TimeStamp `json:"timestamp"`
  12. Contributions int64 `json:"contributions"`
  13. }
  14. // GetUserHeatmapDataByUser returns an array of UserHeatmapData
  15. func GetUserHeatmapDataByUser(user, doer *User) ([]*UserHeatmapData, error) {
  16. return getUserHeatmapData(user, nil, doer)
  17. }
  18. // GetUserHeatmapDataByUserTeam returns an array of UserHeatmapData
  19. func GetUserHeatmapDataByUserTeam(user *User, team *Team, doer *User) ([]*UserHeatmapData, error) {
  20. return getUserHeatmapData(user, team, doer)
  21. }
  22. func getUserHeatmapData(user *User, team *Team, doer *User) ([]*UserHeatmapData, error) {
  23. hdata := make([]*UserHeatmapData, 0)
  24. if !activityReadable(user, doer) {
  25. return hdata, nil
  26. }
  27. // Group by 15 minute intervals which will allow the client to accurately shift the timestamp to their timezone.
  28. // The interval is based on the fact that there are timezones such as UTC +5:30 and UTC +12:45.
  29. groupBy := "created_unix / 900 * 900"
  30. groupByName := "timestamp" // We need this extra case because mssql doesn't allow grouping by alias
  31. switch {
  32. case setting.Database.UseMySQL:
  33. groupBy = "created_unix DIV 900 * 900"
  34. case setting.Database.UseMSSQL:
  35. groupByName = groupBy
  36. }
  37. cond, err := activityQueryCondition(GetFeedsOptions{
  38. RequestedUser: user,
  39. RequestedTeam: team,
  40. Actor: doer,
  41. IncludePrivate: true, // don't filter by private, as we already filter by repo access
  42. IncludeDeleted: true,
  43. // * Heatmaps for individual users only include actions that the user themself did.
  44. // * For organizations actions by all users that were made in owned
  45. // repositories are counted.
  46. OnlyPerformedBy: !user.IsOrganization(),
  47. })
  48. if err != nil {
  49. return nil, err
  50. }
  51. return hdata, x.
  52. Select(groupBy+" AS timestamp, count(user_id) as contributions").
  53. Table("action").
  54. Where(cond).
  55. And("created_unix > ?", timeutil.TimeStampNow()-31536000).
  56. GroupBy(groupByName).
  57. OrderBy("timestamp").
  58. Find(&hdata)
  59. }