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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 QueryUserStaticDataPage(ctx *context.Context) {
  17. startDate := ctx.Query("startDate")
  18. endDate := ctx.Query("endDate")
  19. page := ctx.QueryInt("page")
  20. if page <= 0 {
  21. page = 1
  22. }
  23. pageSize := ctx.QueryInt("pageSize")
  24. if pageSize <= 0 {
  25. pageSize = setting.UI.IssuePagingNum
  26. }
  27. userName := ctx.Query("userName")
  28. IsReturnFile := ctx.QueryBool("IsReturnFile")
  29. log.Info("startDate=" + startDate + " endDate=" + endDate + " userName=" + userName + " page=" + fmt.Sprint(page))
  30. var startTime time.Time
  31. var endTime time.Time
  32. var isAll bool
  33. if startDate == "all" {
  34. isAll = true
  35. startTime = time.Now()
  36. endTime = time.Now()
  37. } else {
  38. startTime, _ = time.ParseInLocation("2006-01-02", startDate, time.Local)
  39. startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 12, 0, 0, 0, startTime.Location())
  40. settingStartTime, _ := time.Parse("2006-01-02", setting.RadarMap.RecordBeginTime)
  41. if startTime.Unix() < settingStartTime.Unix() {
  42. startTime = settingStartTime
  43. startDate = settingStartTime.Format("2006-01-02")
  44. }
  45. endTime, _ = time.ParseInLocation("2006-01-02", endDate, time.Local)
  46. endTime = endTime.AddDate(0, 0, 1)
  47. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 23, 59, 59, 0, startTime.Location())
  48. isAll = false
  49. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  50. }
  51. if IsReturnFile {
  52. page = -1
  53. pageSize = -1
  54. }
  55. pageOpts := &models.UserBusinessAnalysisQueryOptions{
  56. ListOptions: models.ListOptions{
  57. Page: page,
  58. PageSize: pageSize,
  59. },
  60. UserName: userName,
  61. StartTime: startTime.Unix(),
  62. EndTime: endTime.Unix(),
  63. IsAll: isAll,
  64. }
  65. if IsReturnFile {
  66. re, count := models.QueryUserStaticDataAll(pageOpts)
  67. log.Info("return count=" + fmt.Sprint(count))
  68. //writer exec file.
  69. xlsx := excelize.NewFile()
  70. sheetName := ctx.Tr("user.static.sheetname")
  71. index := xlsx.NewSheet(sheetName)
  72. xlsx.DeleteSheet("Sheet1")
  73. dataHeader := map[string]string{
  74. "A1": ctx.Tr("user.static.id"),
  75. "B1": ctx.Tr("user.static.name"),
  76. "C1": ctx.Tr("user.static.codemergecount"),
  77. "D1": ctx.Tr("user.static.commitcount"),
  78. "E1": ctx.Tr("user.static.issuecount"),
  79. "F1": ctx.Tr("user.static.commentcount"),
  80. "G1": ctx.Tr("user.static.focusrepocount"),
  81. "H1": ctx.Tr("user.static.starrepocount"),
  82. "I1": ctx.Tr("user.static.logincount"),
  83. "J1": ctx.Tr("user.static.watchedcount"),
  84. "K1": ctx.Tr("user.static.commitcodesize"),
  85. "L1": ctx.Tr("user.static.solveissuecount"),
  86. "M1": ctx.Tr("user.static.encyclopediascount"),
  87. "N1": ctx.Tr("user.static.createrepocount"),
  88. "O1": ctx.Tr("user.static.openiindex"),
  89. "P1": ctx.Tr("user.static.registdate"),
  90. "Q1": ctx.Tr("user.static.countdate"),
  91. }
  92. for k, v := range dataHeader {
  93. //设置单元格的值
  94. xlsx.SetCellValue(sheetName, k, v)
  95. }
  96. for i, userRecord := range re {
  97. rows := fmt.Sprint(i + 2)
  98. xlsx.SetCellValue(sheetName, "A"+rows, userRecord.ID)
  99. xlsx.SetCellValue(sheetName, "B"+rows, userRecord.Name)
  100. xlsx.SetCellValue(sheetName, "C"+rows, userRecord.CodeMergeCount)
  101. xlsx.SetCellValue(sheetName, "D"+rows, userRecord.CommitCount)
  102. xlsx.SetCellValue(sheetName, "E"+rows, userRecord.IssueCount)
  103. xlsx.SetCellValue(sheetName, "F"+rows, userRecord.CommentCount)
  104. xlsx.SetCellValue(sheetName, "G"+rows, userRecord.FocusRepoCount)
  105. xlsx.SetCellValue(sheetName, "H"+rows, userRecord.StarRepoCount)
  106. xlsx.SetCellValue(sheetName, "I"+rows, userRecord.LoginCount)
  107. xlsx.SetCellValue(sheetName, "J"+rows, userRecord.WatchedCount)
  108. xlsx.SetCellValue(sheetName, "K"+rows, userRecord.CommitCodeSize)
  109. xlsx.SetCellValue(sheetName, "L"+rows, userRecord.SolveIssueCount)
  110. xlsx.SetCellValue(sheetName, "M"+rows, userRecord.EncyclopediasCount)
  111. xlsx.SetCellValue(sheetName, "N"+rows, userRecord.CreateRepoCount)
  112. xlsx.SetCellValue(sheetName, "O"+rows, fmt.Sprintf("%.2f", userRecord.OpenIIndex))
  113. formatTime := userRecord.RegistDate.Format("2006-01-02 15:04:05")
  114. xlsx.SetCellValue(sheetName, "P"+rows, formatTime[0:len(formatTime)-3])
  115. formatTime = userRecord.DataDate
  116. xlsx.SetCellValue(sheetName, "Q"+rows, formatTime+" 00:01")
  117. }
  118. //设置默认打开的表单
  119. xlsx.SetActiveSheet(index)
  120. var filename string
  121. nowTime := time.Now()
  122. nowZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  123. if endTime.Unix() >= nowZeroTime.Unix() {
  124. endDate = nowZeroTime.AddDate(0, 0, -1).Format("2006-01-02")
  125. }
  126. if isAll {
  127. filename = sheetName + "_" + ctx.Tr("user.static.all") + ".xlsx"
  128. } else {
  129. filename = sheetName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  130. }
  131. if len(userName) > 0 {
  132. filename = sheetName + "_" + userName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  133. }
  134. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  135. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  136. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  137. log.Info("writer exel error." + err.Error())
  138. }
  139. } else {
  140. mapInterface := make(map[string]interface{})
  141. re, count := models.QueryUserStaticDataPage(pageOpts)
  142. mapInterface["data"] = re
  143. mapInterface["count"] = count
  144. ctx.JSON(http.StatusOK, mapInterface)
  145. }
  146. }
  147. func TimingCountDataByDateAndReCount(date string, isReCount bool) {
  148. if date == "refreshAll" {
  149. models.RefreshUserStaticAllTabel()
  150. return
  151. }
  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. if isReCount {
  202. models.RefreshUserStaticAllTabel()
  203. }
  204. }
  205. func TimingCountDataByDate(date string) {
  206. TimingCountDataByDateAndReCount(date, true)
  207. }
  208. func TimingCountData() {
  209. log.Info("start to time count data")
  210. currentTimeNow := time.Now()
  211. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  212. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  213. TimingCountDataByDateAndReCount(startTime, false)
  214. }