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_business_analysis.go 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/modules/timeutil"
  7. )
  8. type UserBusinessAnalysis struct {
  9. ID int64 `xorm:"pk"`
  10. CountDate int64 `xorm:"pk"`
  11. //action :ActionMergePullRequest // 11
  12. CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
  13. //action :ActionCommitRepo // 5
  14. CommitCount int `xorm:"NOT NULL DEFAULT 0"`
  15. //action :ActionCommentIssue // 10
  16. IssueCount int `xorm:"NOT NULL DEFAULT 0"`
  17. //comment table current date
  18. CommentCount int `xorm:"NOT NULL DEFAULT 0"`
  19. //watch table current date
  20. FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  21. //star table current date
  22. StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  23. //follow table
  24. WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
  25. // user table
  26. GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
  27. //
  28. CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
  29. //attachement table
  30. CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
  31. //0
  32. CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
  33. //issue, issueassignees
  34. SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
  35. //baike
  36. EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
  37. //user
  38. RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
  39. //user
  40. Email string `xorm:"NOT NULL"`
  41. //user
  42. Name string `xorm:"NOT NULL"`
  43. }
  44. func CountData(wikiCountMap map[string]int) {
  45. log.Info("start to count data")
  46. sess := x.NewSession()
  47. defer sess.Close()
  48. sess.Select("`user`.*").Table("user")
  49. userList := make([]*User, 0)
  50. sess.Find(&userList)
  51. currentTimeNow := time.Now()
  52. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  53. yesterday := currentTimeNow.AddDate(0, 0, -1)
  54. startTime := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location())
  55. start_unix := startTime.Unix()
  56. log.Info("DB query time:" + startTime.Format("2006-01-02 15:04:05"))
  57. endTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  58. end_unix := endTime.Unix()
  59. CountDate := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  60. //codeMergeCountMap := queryAction(start_unix,end_unix,11)
  61. CodeMergeCountMap := queryAction(start_unix, end_unix, 11)
  62. CommitCountMap := queryAction(start_unix, end_unix, 5)
  63. IssueCountMap := queryAction(start_unix, end_unix, 10)
  64. CommentCountMap := queryComment(start_unix, end_unix)
  65. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  66. StarRepoCountMap := queryStar(start_unix, end_unix)
  67. WatchedCountMap := queryFollow(start_unix, end_unix)
  68. CommitCodeSizeMap, err := GetAllUserKPIStats()
  69. if err != nil {
  70. log.Info("query commit code errr.")
  71. }
  72. CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
  73. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  74. for i, userRecord := range userList {
  75. var dateRecord UserBusinessAnalysis
  76. dateRecord.ID = userRecord.ID
  77. log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  78. dateRecord.CountDate = CountDate.Unix()
  79. dateRecord.Email = userRecord.Email
  80. dateRecord.RegistDate = userRecord.CreatedUnix
  81. dateRecord.Name = userRecord.Name
  82. dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  83. if _, ok := CodeMergeCountMap[dateRecord.ID]; !ok {
  84. dateRecord.CodeMergeCount = 0
  85. } else {
  86. dateRecord.CodeMergeCount = CodeMergeCountMap[dateRecord.ID]
  87. }
  88. if _, ok := CommitCountMap[dateRecord.ID]; !ok {
  89. dateRecord.CommitCount = 0
  90. } else {
  91. dateRecord.CommitCount = CommitCountMap[dateRecord.ID]
  92. }
  93. if _, ok := IssueCountMap[dateRecord.ID]; !ok {
  94. dateRecord.IssueCount = 0
  95. } else {
  96. dateRecord.IssueCount = IssueCountMap[dateRecord.ID]
  97. }
  98. if _, ok := CommentCountMap[dateRecord.ID]; !ok {
  99. dateRecord.CommentCount = 0
  100. } else {
  101. dateRecord.CommentCount = CommentCountMap[dateRecord.ID]
  102. }
  103. if _, ok := FocusRepoCountMap[dateRecord.ID]; !ok {
  104. dateRecord.FocusRepoCount = 0
  105. } else {
  106. dateRecord.FocusRepoCount = FocusRepoCountMap[dateRecord.ID]
  107. }
  108. if _, ok := StarRepoCountMap[dateRecord.ID]; !ok {
  109. dateRecord.StarRepoCount = 0
  110. } else {
  111. dateRecord.StarRepoCount = StarRepoCountMap[dateRecord.ID]
  112. }
  113. if _, ok := WatchedCountMap[dateRecord.ID]; !ok {
  114. dateRecord.WatchedCount = 0
  115. } else {
  116. dateRecord.WatchedCount = WatchedCountMap[dateRecord.ID]
  117. }
  118. if _, ok := CommitCodeSizeMap[dateRecord.Email]; !ok {
  119. dateRecord.CommitCodeSize = 0
  120. } else {
  121. dateRecord.CommitCodeSize = int(CommitCodeSizeMap[dateRecord.Email].CommitLines)
  122. }
  123. if _, ok := CommitDatasetSizeMap[dateRecord.ID]; !ok {
  124. dateRecord.CommitDatasetSize = 0
  125. } else {
  126. dateRecord.CommitDatasetSize = CommitDatasetSizeMap[dateRecord.ID]
  127. }
  128. if _, ok := SolveIssueCountMap[dateRecord.ID]; !ok {
  129. dateRecord.SolveIssueCount = 0
  130. } else {
  131. dateRecord.SolveIssueCount = SolveIssueCountMap[dateRecord.ID]
  132. }
  133. if _, ok := wikiCountMap[dateRecord.Name]; !ok {
  134. dateRecord.EncyclopediasCount = 0
  135. } else {
  136. dateRecord.EncyclopediasCount = wikiCountMap[dateRecord.Name]
  137. }
  138. dateRecord.CommitModelCount = 0
  139. statictisSess := xStatistic.NewSession()
  140. defer statictisSess.Close()
  141. statictisSess.Insert(&dateRecord)
  142. }
  143. }
  144. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  145. //select issue_assignees.* from issue_assignees,issue where issue.is_closed=true and issue.id=issue_assignees.issue_id
  146. sess := x.NewSession()
  147. defer sess.Close()
  148. sess.Select("issue_assignees.*").Table("issue_assignees").
  149. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  150. Where("issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix))
  151. issueAssigneesList := make([]*IssueAssignees, 0)
  152. sess.Find(&issueAssigneesList)
  153. resultMap := make(map[int64]int)
  154. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  155. for _, issueAssigneesRecord := range issueAssigneesList {
  156. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  157. resultMap[issueAssigneesRecord.AssigneeID] = 1
  158. } else {
  159. resultMap[issueAssigneesRecord.AssigneeID] += 1
  160. }
  161. }
  162. return resultMap
  163. }
  164. func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  165. sess := x.NewSession()
  166. defer sess.Close()
  167. sess.Select("id,user_id,op_type,act_user_id").Table("action").Where("op_type=" + fmt.Sprint(actionType) + " and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  168. actionList := make([]*Action, 0)
  169. sess.Find(&actionList)
  170. resultMap := make(map[int64]int)
  171. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  172. for _, actionRecord := range actionList {
  173. if _, ok := resultMap[actionRecord.UserID]; !ok {
  174. resultMap[actionRecord.UserID] = 1
  175. } else {
  176. resultMap[actionRecord.UserID] += 1
  177. }
  178. }
  179. return resultMap
  180. }
  181. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  182. sess := x.NewSession()
  183. defer sess.Close()
  184. sess.Select("id,type,poster_id").Table("comment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  185. commentList := make([]*Comment, 0)
  186. sess.Find(&commentList)
  187. resultMap := make(map[int64]int)
  188. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  189. for _, commentRecord := range commentList {
  190. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  191. resultMap[commentRecord.PosterID] = 1
  192. } else {
  193. resultMap[commentRecord.PosterID] += 1
  194. }
  195. }
  196. return resultMap
  197. }
  198. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  199. sess := x.NewSession()
  200. defer sess.Close()
  201. sess.Select("id,user_id,repo_id").Table("watch").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  202. watchList := make([]*Watch, 0)
  203. sess.Find(&watchList)
  204. resultMap := make(map[int64]int)
  205. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  206. for _, watchRecord := range watchList {
  207. if _, ok := resultMap[watchRecord.UserID]; !ok {
  208. resultMap[watchRecord.UserID] = 1
  209. } else {
  210. resultMap[watchRecord.UserID] += 1
  211. }
  212. }
  213. return resultMap
  214. }
  215. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  216. sess := x.NewSession()
  217. defer sess.Close()
  218. sess.Select("id,uid,repo_id").Table("star").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  219. starList := make([]*Star, 0)
  220. sess.Find(&starList)
  221. resultMap := make(map[int64]int)
  222. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  223. for _, starRecord := range starList {
  224. if _, ok := resultMap[starRecord.UID]; !ok {
  225. resultMap[starRecord.UID] = 1
  226. } else {
  227. resultMap[starRecord.UID] += 1
  228. }
  229. }
  230. return resultMap
  231. }
  232. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  233. sess := x.NewSession()
  234. defer sess.Close()
  235. sess.Select("id,user_id,follow_id").Table("follow").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  236. followList := make([]*Follow, 0)
  237. sess.Find(&followList)
  238. resultMap := make(map[int64]int)
  239. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  240. for _, followRecord := range followList {
  241. if _, ok := resultMap[followRecord.UserID]; !ok {
  242. resultMap[followRecord.UserID] = 1
  243. } else {
  244. resultMap[followRecord.UserID] += 1
  245. }
  246. }
  247. return resultMap
  248. }
  249. func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int {
  250. sess := x.NewSession()
  251. defer sess.Close()
  252. sess.Select("id,uploader_id,size").Table("attachment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  253. attachmentList := make([]*Attachment, 0)
  254. sess.Find(&attachmentList)
  255. resultMap := make(map[int64]int)
  256. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  257. for _, attachRecord := range attachmentList {
  258. if _, ok := resultMap[attachRecord.UploaderID]; !ok {
  259. resultMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  260. } else {
  261. resultMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  262. }
  263. }
  264. return resultMap
  265. }
  266. func subMonth(t1, t2 time.Time) (month int) {
  267. y1 := t1.Year()
  268. y2 := t2.Year()
  269. m1 := int(t1.Month())
  270. m2 := int(t2.Month())
  271. d1 := t1.Day()
  272. d2 := t2.Day()
  273. yearInterval := y1 - y2
  274. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  275. if m1 < m2 || m1 == m2 && d1 < d2 {
  276. yearInterval--
  277. }
  278. // 获取月数差值
  279. monthInterval := (m1 + 12) - m2
  280. if d1 < d2 {
  281. monthInterval--
  282. }
  283. monthInterval %= 12
  284. month = yearInterval*12 + monthInterval
  285. return month
  286. }
  287. func QueryAllRepo() []*Repository {
  288. sess := x.NewSession()
  289. defer sess.Close()
  290. sess.Select("*").Table("repository")
  291. repositoryList := make([]*Repository, 0)
  292. sess.Find(&repositoryList)
  293. return repositoryList
  294. }