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

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