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

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. switch {
  19. case setting.UseSQLite3:
  20. groupBy = "strftime('%s', strftime('%Y-%m-%d', created_unix, 'unixepoch'))"
  21. case setting.UseMySQL:
  22. groupBy = "UNIX_TIMESTAMP(DATE(FROM_UNIXTIME(created_unix)))"
  23. case setting.UsePostgreSQL:
  24. groupBy = "extract(epoch from date_trunc('day', to_timestamp(created_unix)))"
  25. case setting.UseMSSQL:
  26. groupBy = "dateadd(DAY,0, datediff(day,0, dateadd(s, created_unix, '19700101')))"
  27. }
  28. err := x.Select(groupBy+" as timestamp, count(user_id) as contributions").
  29. Table("action").
  30. Where("user_id = ?", user.ID).
  31. And("created_unix > ?", (util.TimeStampNow() - 31536000)).
  32. GroupBy("timestamp").
  33. OrderBy("timestamp").
  34. Find(&hdata)
  35. return hdata, err
  36. }