You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_data_analysis.go 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. func QueryUserStaticData(ctx *context.Context) {
  13. startDate := ctx.Query("startDate")
  14. endDate := ctx.Query("endDate")
  15. log.Info("startDate=" + startDate + " endDate=" + endDate)
  16. startTime, _ := time.Parse("2006-01-02", startDate)
  17. endTime, _ := time.Parse("2006-01-02", endDate)
  18. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  19. ctx.JSON(http.StatusOK, models.QueryUserStaticData(startTime.Unix(), endTime.Unix()))
  20. }
  21. func QueryUserStaticDataPage(ctx *context.Context) {
  22. startDate := ctx.Query("startDate")
  23. endDate := ctx.Query("endDate")
  24. page := ctx.QueryInt("page")
  25. if page <= 0 {
  26. page = 1
  27. }
  28. userName := ctx.Query("userName")
  29. log.Info("startDate=" + startDate + " endDate=" + endDate + " userName=" + userName + " page=" + fmt.Sprint(page))
  30. startTime, _ := time.Parse("2006-01-02", startDate)
  31. endTime, _ := time.Parse("2006-01-02", endDate)
  32. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  33. pageOpts := &models.UserBusinessAnalysisQueryOptions{
  34. ListOptions: models.ListOptions{
  35. Page: page,
  36. PageSize: setting.UI.IssuePagingNum,
  37. },
  38. UserName: userName,
  39. StartTime: startTime.Unix(),
  40. EndTime: endTime.Unix(),
  41. }
  42. mapInterface := make(map[string]interface{})
  43. re, count := models.QueryUserStaticDataPage(pageOpts)
  44. mapInterface["data"] = re
  45. mapInterface["count"] = count
  46. ctx.JSON(http.StatusOK, mapInterface)
  47. }
  48. func TimingCountDataByDate(date string) {
  49. t, _ := time.Parse("2006-01-02", date)
  50. startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  51. endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
  52. //query wiki data
  53. log.Info("start to time count data")
  54. wikiMap := make(map[string]int)
  55. repoList, err := models.GetAllRepositories()
  56. if err != nil {
  57. log.Error("query repo error.")
  58. return
  59. }
  60. log.Info("start to query wiki data")
  61. for _, repoRecord := range repoList {
  62. wikiPath := models.WikiPath(repoRecord.OwnerName, repoRecord.Name)
  63. time, err := git.GetLatestCommitTime(wikiPath)
  64. if err == nil {
  65. log.Info("last commit time:" + time.Format("2006-01-02 15:04:05") + " wikiPath=" + wikiPath)
  66. if time.After(startTime) {
  67. wikiRepo, _, err := FindWikiRepoCommitByWikiPath(wikiPath)
  68. if err != nil {
  69. log.Error("wiki not exist. wikiPath=" + wikiPath)
  70. } else {
  71. log.Info("wiki exist, wikiPath=" + wikiPath)
  72. list, err := wikiRepo.GetCommitByPathAndDays(wikiPath, 1)
  73. if err != nil {
  74. log.Info("err,err=v%", err)
  75. } else {
  76. for logEntry := list.Front(); logEntry != nil; logEntry = logEntry.Next() {
  77. commit := logEntry.Value.(*git.Commit)
  78. log.Info("commit msg=" + commit.CommitMessage + " time=" + commit.Committer.When.Format("2006-01-02 15:04:05") + " user=" + commit.Committer.Name)
  79. if _, ok := wikiMap[commit.Committer.Name]; !ok {
  80. wikiMap[commit.Committer.Name] = 1
  81. } else {
  82. wikiMap[commit.Committer.Name] += 1
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. //other user info data
  91. models.CounDataByDate(wikiMap, startTime, endTime)
  92. }
  93. func TimingCountData() {
  94. log.Info("start to time count data")
  95. currentTimeNow := time.Now()
  96. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  97. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  98. TimingCountDataByDate(startTime)
  99. }