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 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 *User) ([]*UserHeatmapData, error) {
  16. hdata := make([]*UserHeatmapData, 0)
  17. if user.KeepActivityPrivate {
  18. return hdata, nil
  19. }
  20. var groupBy string
  21. var groupByName = "timestamp" // We need this extra case because mssql doesn't allow grouping by alias
  22. switch {
  23. case setting.Database.UseSQLite3:
  24. groupBy = "strftime('%s', strftime('%Y-%m-%d', created_unix, 'unixepoch'))"
  25. case setting.Database.UseMySQL:
  26. groupBy = "UNIX_TIMESTAMP(DATE(FROM_UNIXTIME(created_unix)))"
  27. case setting.Database.UsePostgreSQL:
  28. groupBy = "extract(epoch from date_trunc('day', to_timestamp(created_unix)))"
  29. case setting.Database.UseMSSQL:
  30. groupBy = "datediff(SECOND, '19700101', dateadd(DAY, 0, datediff(day, 0, dateadd(s, created_unix, '19700101'))))"
  31. groupByName = groupBy
  32. }
  33. sess := x.Select(groupBy+" AS timestamp, count(user_id) as contributions").
  34. Table("action").
  35. Where("user_id = ?", user.ID).
  36. And("created_unix > ?", (timeutil.TimeStampNow() - 31536000))
  37. // * Heatmaps for individual users only include actions that the user themself
  38. // did.
  39. // * For organizations actions by all users that were made in owned
  40. // repositories are counted.
  41. if user.Type == UserTypeIndividual {
  42. sess = sess.And("act_user_id = ?", user.ID)
  43. }
  44. err := sess.GroupBy(groupByName).
  45. OrderBy("timestamp").
  46. Find(&hdata)
  47. return hdata, err
  48. }