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

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