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 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/360EntSecGroup-Skylar/excelize/v2"
  14. )
  15. func QueryUserStaticData(ctx *context.Context) {
  16. startDate := ctx.Query("startDate")
  17. endDate := ctx.Query("endDate")
  18. log.Info("startDate=" + startDate + " endDate=" + endDate)
  19. startTime, _ := time.Parse("2006-01-02", startDate)
  20. endTime, _ := time.Parse("2006-01-02", endDate)
  21. endTime = endTime.AddDate(0, 0, 1)
  22. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  23. ctx.JSON(http.StatusOK, models.QueryUserStaticData(startTime.Unix(), endTime.Unix()))
  24. }
  25. func QueryUserStaticDataPage(ctx *context.Context) {
  26. startDate := ctx.Query("startDate")
  27. endDate := ctx.Query("endDate")
  28. page := ctx.QueryInt("page")
  29. if page <= 0 {
  30. page = 1
  31. }
  32. pageSize := ctx.QueryInt("pageSize")
  33. if pageSize <= 0 {
  34. pageSize = setting.UI.IssuePagingNum
  35. }
  36. userName := ctx.Query("userName")
  37. IsReturnFile := ctx.QueryBool("IsReturnFile")
  38. log.Info("startDate=" + startDate + " endDate=" + endDate + " userName=" + userName + " page=" + fmt.Sprint(page))
  39. startTime, _ := time.Parse("2006-01-02", startDate)
  40. endTime, _ := time.Parse("2006-01-02", endDate)
  41. endTime = endTime.AddDate(0, 0, 1)
  42. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  43. if IsReturnFile {
  44. page = -1
  45. pageSize = -1
  46. }
  47. pageOpts := &models.UserBusinessAnalysisQueryOptions{
  48. ListOptions: models.ListOptions{
  49. Page: page,
  50. PageSize: pageSize,
  51. },
  52. UserName: userName,
  53. StartTime: startTime.Unix(),
  54. EndTime: endTime.Unix(),
  55. }
  56. mapInterface := make(map[string]interface{})
  57. re, count := models.QueryUserStaticDataPage(pageOpts)
  58. mapInterface["data"] = re
  59. mapInterface["count"] = count
  60. if IsReturnFile {
  61. //writer exec file.
  62. xlsx := excelize.NewFile()
  63. sheetName := ctx.Tr("user.static.sheetname")
  64. index := xlsx.NewSheet(sheetName)
  65. dataHeader := map[string]string{
  66. "A1": ctx.Tr("user.static.id"),
  67. "B1": ctx.Tr("user.static.name"),
  68. "C1": ctx.Tr("user.static.codemergecount"),
  69. "D1": ctx.Tr("user.static.issuecount"),
  70. "E1": ctx.Tr("user.static.commentcount"),
  71. "F1": ctx.Tr("user.static.focusrepocount"),
  72. "G1": ctx.Tr("user.static.starrepocount"),
  73. "H1": ctx.Tr("user.static.logincount"),
  74. "I1": ctx.Tr("user.static.watchedcount"),
  75. "J1": ctx.Tr("user.static.commitcodesize"),
  76. "K1": ctx.Tr("user.static.solveissuecount"),
  77. "L1": ctx.Tr("user.static.encyclopediascount"),
  78. "M1": ctx.Tr("user.static.createrepocount"),
  79. "N1": ctx.Tr("user.static.openiindex"),
  80. "O1": ctx.Tr("user.static.registdate"),
  81. "P1": ctx.Tr("user.static.countdate"),
  82. }
  83. for k, v := range dataHeader {
  84. //设置单元格的值
  85. xlsx.SetCellValue(sheetName, k, v)
  86. }
  87. for i, userRecord := range re {
  88. rows := fmt.Sprint(i + 2)
  89. xlsx.SetCellValue(sheetName, "A"+rows, userRecord.ID)
  90. xlsx.SetCellValue(sheetName, "B"+rows, userRecord.Name)
  91. xlsx.SetCellValue(sheetName, "C"+rows, userRecord.CodeMergeCount)
  92. xlsx.SetCellValue(sheetName, "D"+rows, userRecord.IssueCount)
  93. xlsx.SetCellValue(sheetName, "E"+rows, userRecord.CommentCount)
  94. xlsx.SetCellValue(sheetName, "F"+rows, userRecord.FocusRepoCount)
  95. xlsx.SetCellValue(sheetName, "G"+rows, userRecord.StarRepoCount)
  96. xlsx.SetCellValue(sheetName, "H"+rows, userRecord.LoginCount)
  97. xlsx.SetCellValue(sheetName, "I"+rows, userRecord.WatchedCount)
  98. xlsx.SetCellValue(sheetName, "J"+rows, userRecord.CommitCodeSize)
  99. xlsx.SetCellValue(sheetName, "K"+rows, userRecord.SolveIssueCount)
  100. xlsx.SetCellValue(sheetName, "L"+rows, userRecord.EncyclopediasCount)
  101. xlsx.SetCellValue(sheetName, "M"+rows, userRecord.CreateRepoCount)
  102. xlsx.SetCellValue(sheetName, "N"+rows, userRecord.OpenIIndex)
  103. xlsx.SetCellValue(sheetName, "O"+rows, userRecord.RegistDate.Format("2006-01-02"))
  104. xlsx.SetCellValue(sheetName, "P"+rows, time.Unix(userRecord.CountDate, 0).Format("2006-01-02"))
  105. }
  106. //设置默认打开的表单
  107. xlsx.SetActiveSheet(index)
  108. filename := sheetName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  109. if len(userName) > 0 {
  110. filename = sheetName + "_" + userName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  111. }
  112. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  113. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  114. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  115. log.Info("writer exel error." + err.Error())
  116. }
  117. } else {
  118. ctx.JSON(http.StatusOK, mapInterface)
  119. }
  120. }
  121. func TimingCountDataByDateAndReCount(date string, isReCount bool) {
  122. t, _ := time.Parse("2006-01-02", date)
  123. startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  124. endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
  125. //query wiki data
  126. log.Info("start to time count data")
  127. wikiMap := make(map[string]int)
  128. repoList, err := models.GetAllRepositories()
  129. if err != nil {
  130. log.Error("query repo error.")
  131. return
  132. }
  133. log.Info("start to query wiki data")
  134. for _, repoRecord := range repoList {
  135. wikiPath := models.WikiPath(repoRecord.OwnerName, repoRecord.Name)
  136. time, err := git.GetLatestCommitTime(wikiPath)
  137. if err == nil {
  138. log.Info("last commit time:" + time.Format("2006-01-02 15:04:05") + " wikiPath=" + wikiPath)
  139. if time.After(startTime) {
  140. wikiRepo, _, err := FindWikiRepoCommitByWikiPath(wikiPath)
  141. if err != nil {
  142. log.Error("wiki not exist. wikiPath=" + wikiPath)
  143. } else {
  144. log.Info("wiki exist, wikiPath=" + wikiPath)
  145. list, err := wikiRepo.GetCommitByPathAndDays(wikiPath, 1)
  146. if err != nil {
  147. log.Info("err,err=v%", err)
  148. } else {
  149. for logEntry := list.Front(); logEntry != nil; logEntry = logEntry.Next() {
  150. commit := logEntry.Value.(*git.Commit)
  151. log.Info("commit msg=" + commit.CommitMessage + " time=" + commit.Committer.When.Format("2006-01-02 15:04:05") + " user=" + commit.Committer.Name)
  152. if _, ok := wikiMap[commit.Committer.Name]; !ok {
  153. wikiMap[commit.Committer.Name] = 1
  154. } else {
  155. wikiMap[commit.Committer.Name] += 1
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. //other user info data
  164. models.CounDataByDateAndReCount(wikiMap, startTime, endTime, isReCount)
  165. }
  166. func TimingCountDataByDate(date string) {
  167. TimingCountDataByDateAndReCount(date, true)
  168. }
  169. func TimingCountData() {
  170. log.Info("start to time count data")
  171. currentTimeNow := time.Now()
  172. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  173. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  174. TimingCountDataByDateAndReCount(startTime, false)
  175. }