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

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