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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.UseSQLite3:
  21. groupBy = "strftime('%s', strftime('%Y-%m-%d', created_unix, 'unixepoch'))"
  22. case setting.UseMySQL:
  23. groupBy = "UNIX_TIMESTAMP(DATE(FROM_UNIXTIME(created_unix)))"
  24. case setting.UsePostgreSQL:
  25. groupBy = "extract(epoch from date_trunc('day', to_timestamp(created_unix)))"
  26. case setting.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. }