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

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