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.

43 lines
1.5 KiB

  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/util"
  8. )
  9. // UserHeatmapData represents the data needed to create a heatmap
  10. type UserHeatmapData struct {
  11. Timestamp util.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. err := x.Select(groupBy+" AS timestamp, count(user_id) as contributions").
  31. Table("action").
  32. Where("user_id = ?", user.ID).
  33. And("created_unix > ?", (util.TimeStampNow() - 31536000)).
  34. GroupBy(groupByName).
  35. OrderBy("timestamp").
  36. Find(&hdata)
  37. return hdata, err
  38. }