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

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