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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/services/mailer"
  13. "github.com/360EntSecGroup-Skylar/excelize/v2"
  14. )
  15. const (
  16. PAGE_SIZE = 2000
  17. )
  18. func queryUserDataPage(ctx *context.Context, tableName string, queryObj interface{}) {
  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. if IsReturnFile {
  30. //writer exec file.
  31. xlsx := excelize.NewFile()
  32. sheetName := ctx.Tr("user.static.sheetname")
  33. index := xlsx.NewSheet(sheetName)
  34. xlsx.DeleteSheet("Sheet1")
  35. dataHeader := map[string]string{
  36. "A1": ctx.Tr("user.static.id"),
  37. "B1": ctx.Tr("user.static.name"),
  38. "C1": ctx.Tr("user.static.codemergecount"),
  39. "D1": ctx.Tr("user.static.commitcount"),
  40. "E1": ctx.Tr("user.static.issuecount"),
  41. "F1": ctx.Tr("user.static.commentcount"),
  42. "G1": ctx.Tr("user.static.focusrepocount"),
  43. "H1": ctx.Tr("user.static.starrepocount"),
  44. "I1": ctx.Tr("user.static.logincount"),
  45. "J1": ctx.Tr("user.static.watchedcount"),
  46. "K1": ctx.Tr("user.static.commitcodesize"),
  47. "L1": ctx.Tr("user.static.solveissuecount"),
  48. "M1": ctx.Tr("user.static.encyclopediascount"),
  49. "N1": ctx.Tr("user.static.createrepocount"),
  50. "O1": ctx.Tr("user.static.openiindex"),
  51. "P1": ctx.Tr("user.static.registdate"),
  52. "Q1": ctx.Tr("user.static.countdate"),
  53. }
  54. for k, v := range dataHeader {
  55. //设置单元格的值
  56. xlsx.SetCellValue(sheetName, k, v)
  57. }
  58. _, count := models.QueryUserStaticDataByTableName(1, 1, tableName, queryObj, userName)
  59. var indexTotal int64
  60. indexTotal = 0
  61. row := 1
  62. for {
  63. re, _ := models.QueryUserStaticDataByTableName(int(indexTotal), PAGE_SIZE, tableName, queryObj, "")
  64. log.Info("return count=" + fmt.Sprint(count))
  65. for _, userRecord := range re {
  66. row++
  67. rows := fmt.Sprint(row)
  68. xlsx.SetCellValue(sheetName, "A"+rows, userRecord.ID)
  69. xlsx.SetCellValue(sheetName, "B"+rows, userRecord.Name)
  70. xlsx.SetCellValue(sheetName, "C"+rows, userRecord.CodeMergeCount)
  71. xlsx.SetCellValue(sheetName, "D"+rows, userRecord.CommitCount)
  72. xlsx.SetCellValue(sheetName, "E"+rows, userRecord.IssueCount)
  73. xlsx.SetCellValue(sheetName, "F"+rows, userRecord.CommentCount)
  74. xlsx.SetCellValue(sheetName, "G"+rows, userRecord.FocusRepoCount)
  75. xlsx.SetCellValue(sheetName, "H"+rows, userRecord.StarRepoCount)
  76. xlsx.SetCellValue(sheetName, "I"+rows, userRecord.LoginCount)
  77. xlsx.SetCellValue(sheetName, "J"+rows, userRecord.WatchedCount)
  78. xlsx.SetCellValue(sheetName, "K"+rows, userRecord.CommitCodeSize)
  79. xlsx.SetCellValue(sheetName, "L"+rows, userRecord.SolveIssueCount)
  80. xlsx.SetCellValue(sheetName, "M"+rows, userRecord.EncyclopediasCount)
  81. xlsx.SetCellValue(sheetName, "N"+rows, userRecord.CreateRepoCount)
  82. xlsx.SetCellValue(sheetName, "O"+rows, fmt.Sprintf("%.2f", userRecord.OpenIIndex))
  83. formatTime := userRecord.RegistDate.Format("2006-01-02 15:04:05")
  84. xlsx.SetCellValue(sheetName, "P"+rows, formatTime[0:len(formatTime)-3])
  85. formatTime = userRecord.DataDate
  86. xlsx.SetCellValue(sheetName, "Q"+rows, formatTime)
  87. }
  88. indexTotal += PAGE_SIZE
  89. if indexTotal >= count {
  90. break
  91. }
  92. }
  93. //设置默认打开的表单
  94. xlsx.SetActiveSheet(index)
  95. filename := sheetName + "_" + ctx.Tr("user.static."+tableName) + ".xlsx"
  96. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  97. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  98. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  99. log.Info("writer exel error." + err.Error())
  100. }
  101. } else {
  102. re, count := models.QueryUserStaticDataByTableName((page-1)*pageSize, pageSize, tableName, queryObj, userName)
  103. mapInterface := make(map[string]interface{})
  104. mapInterface["data"] = re
  105. mapInterface["count"] = count
  106. ctx.JSON(http.StatusOK, mapInterface)
  107. }
  108. }
  109. func QueryUserStaticCurrentMonth(ctx *context.Context) {
  110. queryUserDataPage(ctx, "public.user_business_analysis_current_month", new(models.UserBusinessAnalysisCurrentMonth))
  111. }
  112. func QueryUserStaticCurrentWeek(ctx *context.Context) {
  113. queryUserDataPage(ctx, "public.user_business_analysis_current_week", new(models.UserBusinessAnalysisCurrentWeek))
  114. }
  115. func QueryUserStaticCurrentYear(ctx *context.Context) {
  116. queryUserDataPage(ctx, "public.user_business_analysis_current_year", new(models.UserBusinessAnalysisCurrentYear))
  117. }
  118. func QueryUserStaticLast30Day(ctx *context.Context) {
  119. queryUserDataPage(ctx, "public.user_business_analysis_last30_day", new(models.UserBusinessAnalysisLast30Day))
  120. }
  121. func QueryUserStaticLastMonth(ctx *context.Context) {
  122. queryUserDataPage(ctx, "public.user_business_analysis_last_month", new(models.UserBusinessAnalysisLastMonth))
  123. }
  124. func QueryUserStaticYesterday(ctx *context.Context) {
  125. queryUserDataPage(ctx, "public.user_business_analysis_yesterday", new(models.UserBusinessAnalysisYesterday))
  126. }
  127. func QueryUserStaticAll(ctx *context.Context) {
  128. queryUserDataPage(ctx, "public.user_business_analysis_all", new(models.UserBusinessAnalysisAll))
  129. }
  130. func QueryUserStaticDataPage(ctx *context.Context) {
  131. startDate := ctx.Query("startDate")
  132. endDate := ctx.Query("endDate")
  133. page := ctx.QueryInt("page")
  134. if page <= 0 {
  135. page = 1
  136. }
  137. pageSize := ctx.QueryInt("pageSize")
  138. if pageSize <= 0 {
  139. pageSize = setting.UI.IssuePagingNum
  140. }
  141. userName := ctx.Query("userName")
  142. IsReturnFile := ctx.QueryBool("IsReturnFile")
  143. log.Info("startDate=" + startDate + " endDate=" + endDate + " userName=" + userName + " page=" + fmt.Sprint(page))
  144. var startTime time.Time
  145. var endTime time.Time
  146. var isAll bool
  147. if startDate == "all" {
  148. isAll = true
  149. startTime = time.Now()
  150. endTime = time.Now()
  151. } else {
  152. startTime, _ = time.ParseInLocation("2006-01-02", startDate, time.Local)
  153. startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 12, 0, 0, 0, startTime.Location())
  154. settingStartTime, _ := time.Parse("2006-01-02", setting.RadarMap.RecordBeginTime)
  155. if startTime.Unix() < settingStartTime.Unix() {
  156. startTime = settingStartTime
  157. startDate = settingStartTime.Format("2006-01-02")
  158. }
  159. endTime, _ = time.ParseInLocation("2006-01-02", endDate, time.Local)
  160. endTime = endTime.AddDate(0, 0, 1)
  161. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 23, 59, 59, 0, startTime.Location())
  162. isAll = false
  163. log.Info("startTime=" + fmt.Sprint(startTime.Unix()) + " endDate=" + fmt.Sprint(endTime.Unix()))
  164. }
  165. if IsReturnFile {
  166. page = -1
  167. pageSize = -1
  168. }
  169. pageOpts := &models.UserBusinessAnalysisQueryOptions{
  170. ListOptions: models.ListOptions{
  171. Page: page,
  172. PageSize: pageSize,
  173. },
  174. UserName: userName,
  175. StartTime: startTime.Unix(),
  176. EndTime: endTime.Unix(),
  177. IsAll: isAll,
  178. }
  179. if IsReturnFile {
  180. re, count := models.QueryUserStaticDataAll(pageOpts)
  181. log.Info("return count=" + fmt.Sprint(count))
  182. //writer exec file.
  183. xlsx := excelize.NewFile()
  184. sheetName := ctx.Tr("user.static.sheetname")
  185. index := xlsx.NewSheet(sheetName)
  186. xlsx.DeleteSheet("Sheet1")
  187. dataHeader := map[string]string{
  188. "A1": ctx.Tr("user.static.id"),
  189. "B1": ctx.Tr("user.static.name"),
  190. "C1": ctx.Tr("user.static.codemergecount"),
  191. "D1": ctx.Tr("user.static.commitcount"),
  192. "E1": ctx.Tr("user.static.issuecount"),
  193. "F1": ctx.Tr("user.static.commentcount"),
  194. "G1": ctx.Tr("user.static.focusrepocount"),
  195. "H1": ctx.Tr("user.static.starrepocount"),
  196. "I1": ctx.Tr("user.static.logincount"),
  197. "J1": ctx.Tr("user.static.watchedcount"),
  198. "K1": ctx.Tr("user.static.commitcodesize"),
  199. "L1": ctx.Tr("user.static.solveissuecount"),
  200. "M1": ctx.Tr("user.static.encyclopediascount"),
  201. "N1": ctx.Tr("user.static.createrepocount"),
  202. "O1": ctx.Tr("user.static.openiindex"),
  203. "P1": ctx.Tr("user.static.registdate"),
  204. "Q1": ctx.Tr("user.static.countdate"),
  205. }
  206. for k, v := range dataHeader {
  207. //设置单元格的值
  208. xlsx.SetCellValue(sheetName, k, v)
  209. }
  210. for i, userRecord := range re {
  211. rows := fmt.Sprint(i + 2)
  212. xlsx.SetCellValue(sheetName, "A"+rows, userRecord.ID)
  213. xlsx.SetCellValue(sheetName, "B"+rows, userRecord.Name)
  214. xlsx.SetCellValue(sheetName, "C"+rows, userRecord.CodeMergeCount)
  215. xlsx.SetCellValue(sheetName, "D"+rows, userRecord.CommitCount)
  216. xlsx.SetCellValue(sheetName, "E"+rows, userRecord.IssueCount)
  217. xlsx.SetCellValue(sheetName, "F"+rows, userRecord.CommentCount)
  218. xlsx.SetCellValue(sheetName, "G"+rows, userRecord.FocusRepoCount)
  219. xlsx.SetCellValue(sheetName, "H"+rows, userRecord.StarRepoCount)
  220. xlsx.SetCellValue(sheetName, "I"+rows, userRecord.LoginCount)
  221. xlsx.SetCellValue(sheetName, "J"+rows, userRecord.WatchedCount)
  222. xlsx.SetCellValue(sheetName, "K"+rows, userRecord.CommitCodeSize)
  223. xlsx.SetCellValue(sheetName, "L"+rows, userRecord.SolveIssueCount)
  224. xlsx.SetCellValue(sheetName, "M"+rows, userRecord.EncyclopediasCount)
  225. xlsx.SetCellValue(sheetName, "N"+rows, userRecord.CreateRepoCount)
  226. xlsx.SetCellValue(sheetName, "O"+rows, fmt.Sprintf("%.2f", userRecord.OpenIIndex))
  227. formatTime := userRecord.RegistDate.Format("2006-01-02 15:04:05")
  228. xlsx.SetCellValue(sheetName, "P"+rows, formatTime[0:len(formatTime)-3])
  229. formatTime = userRecord.DataDate
  230. xlsx.SetCellValue(sheetName, "Q"+rows, formatTime+" 00:01")
  231. }
  232. //设置默认打开的表单
  233. xlsx.SetActiveSheet(index)
  234. filename := sheetName + "_" + ctx.Tr("user.static.all") + ".xlsx"
  235. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  236. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  237. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  238. log.Info("writer exel error." + err.Error())
  239. }
  240. } else {
  241. mapInterface := make(map[string]interface{})
  242. re, count := models.QueryUserStaticDataPage(pageOpts)
  243. mapInterface["data"] = re
  244. mapInterface["count"] = count
  245. ctx.JSON(http.StatusOK, mapInterface)
  246. }
  247. }
  248. func TimingCountDataByDateAndReCount(date string, isReCount bool) {
  249. t, _ := time.Parse("2006-01-02", date)
  250. startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  251. endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
  252. //query wiki data
  253. log.Info("start to time count data")
  254. wikiMap := make(map[string]int)
  255. warnEmailMessage := "用户统计信息入库失败,请尽快定位。"
  256. repoList, err := models.GetAllRepositories()
  257. if err != nil {
  258. log.Error("query repo error." + err.Error())
  259. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  260. return
  261. }
  262. log.Info("start to query wiki data")
  263. for _, repoRecord := range repoList {
  264. wikiPath := models.WikiPath(repoRecord.OwnerName, repoRecord.Name)
  265. time, err := git.GetLatestCommitTime(wikiPath)
  266. if err == nil {
  267. log.Info("last commit time:" + time.Format("2006-01-02 15:04:05") + " wikiPath=" + wikiPath)
  268. if time.After(startTime) {
  269. wikiRepo, _, err := FindWikiRepoCommitByWikiPath(wikiPath)
  270. if err != nil {
  271. log.Error("wiki not exist. wikiPath=" + wikiPath)
  272. } else {
  273. log.Info("wiki exist, wikiPath=" + wikiPath)
  274. list, err := wikiRepo.GetCommitByPathAndDays(wikiPath, 1)
  275. if err != nil {
  276. log.Info("err,err=v%", err)
  277. } else {
  278. for logEntry := list.Front(); logEntry != nil; logEntry = logEntry.Next() {
  279. commit := logEntry.Value.(*git.Commit)
  280. log.Info("commit msg=" + commit.CommitMessage + " time=" + commit.Committer.When.Format("2006-01-02 15:04:05") + " user=" + commit.Committer.Name)
  281. if _, ok := wikiMap[commit.Committer.Name]; !ok {
  282. wikiMap[commit.Committer.Name] = 1
  283. } else {
  284. wikiMap[commit.Committer.Name] += 1
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. //other user info data
  293. err = models.CounDataByDateAndReCount(wikiMap, startTime, endTime, isReCount)
  294. if err != nil {
  295. log.Error("count user info error." + err.Error())
  296. mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage)
  297. }
  298. log.Info("start to count all user info data")
  299. //models.RefreshUserStaticAllTabel(wikiMap)
  300. log.Info("end to count all user info data")
  301. }
  302. func TimingCountDataByDate(date string) {
  303. TimingCountDataByDateAndReCount(date, true)
  304. }
  305. func TimingCountData() {
  306. log.Info("start to time count data")
  307. currentTimeNow := time.Now()
  308. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  309. startTime := currentTimeNow.AddDate(0, 0, -1).Format("2006-01-02")
  310. TimingCountDataByDateAndReCount(startTime, false)
  311. }