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

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