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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.commitcount"),
  70. "E1": ctx.Tr("user.static.issuecount"),
  71. "F1": ctx.Tr("user.static.commentcount"),
  72. "G1": ctx.Tr("user.static.focusrepocount"),
  73. "H1": ctx.Tr("user.static.starrepocount"),
  74. "I1": ctx.Tr("user.static.logincount"),
  75. "J1": ctx.Tr("user.static.watchedcount"),
  76. "K1": ctx.Tr("user.static.commitcodesize"),
  77. "L1": ctx.Tr("user.static.solveissuecount"),
  78. "M1": ctx.Tr("user.static.encyclopediascount"),
  79. "N1": ctx.Tr("user.static.createrepocount"),
  80. "O1": ctx.Tr("user.static.openiindex"),
  81. "P1": ctx.Tr("user.static.registdate"),
  82. "Q1": ctx.Tr("user.static.countdate"),
  83. }
  84. for k, v := range dataHeader {
  85. //设置单元格的值
  86. xlsx.SetCellValue(sheetName, k, v)
  87. }
  88. for i, userRecord := range re {
  89. rows := fmt.Sprint(i + 2)
  90. xlsx.SetCellValue(sheetName, "A"+rows, userRecord.ID)
  91. xlsx.SetCellValue(sheetName, "B"+rows, userRecord.Name)
  92. xlsx.SetCellValue(sheetName, "C"+rows, userRecord.CodeMergeCount)
  93. xlsx.SetCellValue(sheetName, "D"+rows, userRecord.CommitCount)
  94. xlsx.SetCellValue(sheetName, "E"+rows, userRecord.IssueCount)
  95. xlsx.SetCellValue(sheetName, "F"+rows, userRecord.CommentCount)
  96. xlsx.SetCellValue(sheetName, "G"+rows, userRecord.FocusRepoCount)
  97. xlsx.SetCellValue(sheetName, "H"+rows, userRecord.StarRepoCount)
  98. xlsx.SetCellValue(sheetName, "I"+rows, userRecord.LoginCount)
  99. xlsx.SetCellValue(sheetName, "J"+rows, userRecord.WatchedCount)
  100. xlsx.SetCellValue(sheetName, "K"+rows, userRecord.CommitCodeSize)
  101. xlsx.SetCellValue(sheetName, "L"+rows, userRecord.SolveIssueCount)
  102. xlsx.SetCellValue(sheetName, "M"+rows, userRecord.EncyclopediasCount)
  103. xlsx.SetCellValue(sheetName, "N"+rows, userRecord.CreateRepoCount)
  104. xlsx.SetCellValue(sheetName, "O"+rows, userRecord.OpenIIndex)
  105. xlsx.SetCellValue(sheetName, "P"+rows, userRecord.RegistDate.Format("2006-01-02"))
  106. xlsx.SetCellValue(sheetName, "Q"+rows, time.Unix(userRecord.CountDate, 0).Format("2006-01-02"))
  107. }
  108. //设置默认打开的表单
  109. xlsx.SetActiveSheet(index)
  110. filename := sheetName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  111. if len(userName) > 0 {
  112. filename = sheetName + "_" + userName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  113. }
  114. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  115. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  116. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  117. log.Info("writer exel error." + err.Error())
  118. }
  119. } else {
  120. ctx.JSON(http.StatusOK, mapInterface)
  121. }
  122. }
  123. func TimingCountDataByDateAndReCount(date string, isReCount bool) {
  124. t, _ := time.Parse("2006-01-02", date)
  125. startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  126. endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
  127. //query wiki data
  128. log.Info("start to time count data")
  129. wikiMap := make(map[string]int)
  130. repoList, err := models.GetAllRepositories()
  131. if err != nil {
  132. log.Error("query repo error.")
  133. return
  134. }
  135. log.Info("start to query wiki data")
  136. for _, repoRecord := range repoList {
  137. wikiPath := models.WikiPath(repoRecord.OwnerName, repoRecord.Name)
  138. time, err := git.GetLatestCommitTime(wikiPath)
  139. if err == nil {
  140. log.Info("last commit time:" + time.Format("2006-01-02 15:04:05") + " wikiPath=" + wikiPath)
  141. if time.After(startTime) {
  142. wikiRepo, _, err := FindWikiRepoCommitByWikiPath(wikiPath)
  143. if err != nil {
  144. log.Error("wiki not exist. wikiPath=" + wikiPath)
  145. } else {
  146. log.Info("wiki exist, wikiPath=" + wikiPath)
  147. list, err := wikiRepo.GetCommitByPathAndDays(wikiPath, 1)
  148. if err != nil {
  149. log.Info("err,err=v%", err)
  150. } else {
  151. for logEntry := list.Front(); logEntry != nil; logEntry = logEntry.Next() {
  152. commit := logEntry.Value.(*git.Commit)
  153. log.Info("commit msg=" + commit.CommitMessage + " time=" + commit.Committer.When.Format("2006-01-02 15:04:05") + " user=" + commit.Committer.Name)
  154. if _, ok := wikiMap[commit.Committer.Name]; !ok {
  155. wikiMap[commit.Committer.Name] = 1
  156. } else {
  157. wikiMap[commit.Committer.Name] += 1
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. //other user info data
  166. models.CounDataByDateAndReCount(wikiMap, startTime, endTime, isReCount)
  167. }
  168. func TimingCountDataByDate(date string) {
  169. TimingCountDataByDateAndReCount(date, true)
  170. }
  171. func TimingCountData() {
  172. log.Info("start to time count data")
  173. currentTimeNow := time.Now()
  174. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  175. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  176. TimingCountDataByDateAndReCount(startTime, false)
  177. }