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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.ParseInLocation("2006-01-02", startDate, time.Local)
  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.ParseInLocation("2006-01-02", endDate, time.Local)
  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. sheetName := ctx.Tr("user.static.sheetname")
  81. index := xlsx.NewSheet(sheetName)
  82. xlsx.DeleteSheet("Sheet1")
  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. formatTime := userRecord.RegistDate.Format("2006-01-02 15:04:05")
  124. xlsx.SetCellValue(sheetName, "P"+rows, formatTime[0:len(formatTime)-3])
  125. formatTime = time.Unix(userRecord.CountDate, 0).Format("2006-01-02 15:04:05")
  126. xlsx.SetCellValue(sheetName, "Q"+rows, formatTime[0:len(formatTime)-3])
  127. }
  128. //设置默认打开的表单
  129. xlsx.SetActiveSheet(index)
  130. var filename string
  131. nowTime := time.Now()
  132. nowZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  133. if endTime.Unix() >= nowZeroTime.Unix() {
  134. endDate = nowZeroTime.AddDate(0, 0, -1).Format("2006-01-02")
  135. }
  136. if isAll {
  137. filename = sheetName + "_" + ctx.Tr("user.static.all") + ".xlsx"
  138. } else {
  139. filename = sheetName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  140. }
  141. if len(userName) > 0 {
  142. filename = sheetName + "_" + userName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx"
  143. }
  144. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  145. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  146. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  147. log.Info("writer exel error." + err.Error())
  148. }
  149. } else {
  150. ctx.JSON(http.StatusOK, mapInterface)
  151. }
  152. }
  153. func TimingCountDataByDateAndReCount(date string, isReCount bool) {
  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. }
  204. func TimingCountDataByDate(date string) {
  205. TimingCountDataByDateAndReCount(date, true)
  206. }
  207. func TimingCountData() {
  208. log.Info("start to time count data")
  209. currentTimeNow := time.Now()
  210. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  211. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  212. TimingCountDataByDateAndReCount(startTime, false)
  213. }