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.4 kB

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