本站源代码
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.

54 lines
1.8KB

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