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

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