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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
  69. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  70. for i, userRecord := range userList {
  71. var dateRecord UserBusinessAnalysis
  72. dateRecord.ID = userRecord.ID
  73. log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  74. dateRecord.CountDate = CountDate.Unix()
  75. dateRecord.Email = userRecord.Email
  76. dateRecord.RegistDate = userRecord.CreatedUnix
  77. dateRecord.Name = userRecord.Name
  78. dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  79. if _, ok := CodeMergeCountMap[dateRecord.ID]; !ok {
  80. dateRecord.CodeMergeCount = 0
  81. } else {
  82. dateRecord.CodeMergeCount = CodeMergeCountMap[dateRecord.ID]
  83. }
  84. if _, ok := CommitCountMap[dateRecord.ID]; !ok {
  85. dateRecord.CommitCount = 0
  86. } else {
  87. dateRecord.CommitCount = CommitCountMap[dateRecord.ID]
  88. }
  89. if _, ok := IssueCountMap[dateRecord.ID]; !ok {
  90. dateRecord.IssueCount = 0
  91. } else {
  92. dateRecord.IssueCount = IssueCountMap[dateRecord.ID]
  93. }
  94. if _, ok := CommentCountMap[dateRecord.ID]; !ok {
  95. dateRecord.CommentCount = 0
  96. } else {
  97. dateRecord.CommentCount = CommentCountMap[dateRecord.ID]
  98. }
  99. if _, ok := FocusRepoCountMap[dateRecord.ID]; !ok {
  100. dateRecord.FocusRepoCount = 0
  101. } else {
  102. dateRecord.FocusRepoCount = FocusRepoCountMap[dateRecord.ID]
  103. }
  104. if _, ok := StarRepoCountMap[dateRecord.ID]; !ok {
  105. dateRecord.StarRepoCount = 0
  106. } else {
  107. dateRecord.StarRepoCount = StarRepoCountMap[dateRecord.ID]
  108. }
  109. if _, ok := WatchedCountMap[dateRecord.ID]; !ok {
  110. dateRecord.WatchedCount = 0
  111. } else {
  112. dateRecord.WatchedCount = WatchedCountMap[dateRecord.ID]
  113. }
  114. if _, ok := CommitDatasetSizeMap[dateRecord.ID]; !ok {
  115. dateRecord.CommitDatasetSize = 0
  116. } else {
  117. dateRecord.CommitDatasetSize = CommitDatasetSizeMap[dateRecord.ID]
  118. }
  119. if _, ok := SolveIssueCountMap[dateRecord.ID]; !ok {
  120. dateRecord.SolveIssueCount = 0
  121. } else {
  122. dateRecord.SolveIssueCount = SolveIssueCountMap[dateRecord.ID]
  123. }
  124. if _, ok := wikiCountMap[dateRecord.Name]; !ok {
  125. dateRecord.EncyclopediasCount = 0
  126. } else {
  127. dateRecord.EncyclopediasCount = wikiCountMap[dateRecord.Name]
  128. }
  129. dateRecord.CommitModelCount = 0
  130. sess.Insert(&dateRecord)
  131. }
  132. }
  133. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  134. //select issue_assignees.* from issue_assignees,issue where issue.is_closed=true and issue.id=issue_assignees.issue_id
  135. sess := x.NewSession()
  136. defer sess.Close()
  137. sess.Select("issue_assignees.*").Table("issue_assignees").
  138. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  139. Where("issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix))
  140. issueAssigneesList := make([]*IssueAssignees, 0)
  141. sess.Find(&issueAssigneesList)
  142. resultMap := make(map[int64]int)
  143. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  144. for _, issueAssigneesRecord := range issueAssigneesList {
  145. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  146. resultMap[issueAssigneesRecord.AssigneeID] = 1
  147. } else {
  148. resultMap[issueAssigneesRecord.AssigneeID] += 1
  149. }
  150. }
  151. return resultMap
  152. }
  153. func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  154. sess := x.NewSession()
  155. defer sess.Close()
  156. 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))
  157. actionList := make([]*Action, 0)
  158. sess.Find(&actionList)
  159. resultMap := make(map[int64]int)
  160. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  161. for _, actionRecord := range actionList {
  162. if _, ok := resultMap[actionRecord.UserID]; !ok {
  163. resultMap[actionRecord.UserID] = 1
  164. } else {
  165. resultMap[actionRecord.UserID] += 1
  166. }
  167. }
  168. return resultMap
  169. }
  170. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  171. sess := x.NewSession()
  172. defer sess.Close()
  173. sess.Select("id,type,poster_id").Table("comment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  174. commentList := make([]*Comment, 0)
  175. sess.Find(&commentList)
  176. resultMap := make(map[int64]int)
  177. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  178. for _, commentRecord := range commentList {
  179. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  180. resultMap[commentRecord.PosterID] = 1
  181. } else {
  182. resultMap[commentRecord.PosterID] += 1
  183. }
  184. }
  185. return resultMap
  186. }
  187. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  188. sess := x.NewSession()
  189. defer sess.Close()
  190. sess.Select("id,user_id,repo_id").Table("watch").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  191. watchList := make([]*Watch, 0)
  192. sess.Find(&watchList)
  193. resultMap := make(map[int64]int)
  194. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  195. for _, watchRecord := range watchList {
  196. if _, ok := resultMap[watchRecord.UserID]; !ok {
  197. resultMap[watchRecord.UserID] = 1
  198. } else {
  199. resultMap[watchRecord.UserID] += 1
  200. }
  201. }
  202. return resultMap
  203. }
  204. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  205. sess := x.NewSession()
  206. defer sess.Close()
  207. sess.Select("id,uid,repo_id").Table("star").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  208. starList := make([]*Star, 0)
  209. sess.Find(&starList)
  210. resultMap := make(map[int64]int)
  211. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  212. for _, starRecord := range starList {
  213. if _, ok := resultMap[starRecord.UID]; !ok {
  214. resultMap[starRecord.UID] = 1
  215. } else {
  216. resultMap[starRecord.UID] += 1
  217. }
  218. }
  219. return resultMap
  220. }
  221. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  222. sess := x.NewSession()
  223. defer sess.Close()
  224. sess.Select("id,user_id,follow_id").Table("follow").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  225. followList := make([]*Follow, 0)
  226. sess.Find(&followList)
  227. resultMap := make(map[int64]int)
  228. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  229. for _, followRecord := range followList {
  230. if _, ok := resultMap[followRecord.UserID]; !ok {
  231. resultMap[followRecord.UserID] = 1
  232. } else {
  233. resultMap[followRecord.UserID] += 1
  234. }
  235. }
  236. return resultMap
  237. }
  238. func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int {
  239. sess := x.NewSession()
  240. defer sess.Close()
  241. sess.Select("id,uploader_id,size").Table("attachment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  242. attachmentList := make([]*Attachment, 0)
  243. sess.Find(&attachmentList)
  244. resultMap := make(map[int64]int)
  245. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  246. for _, attachRecord := range attachmentList {
  247. if _, ok := resultMap[attachRecord.UploaderID]; !ok {
  248. resultMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  249. } else {
  250. resultMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  251. }
  252. }
  253. return resultMap
  254. }
  255. func subMonth(t1, t2 time.Time) (month int) {
  256. y1 := t1.Year()
  257. y2 := t2.Year()
  258. m1 := int(t1.Month())
  259. m2 := int(t2.Month())
  260. d1 := t1.Day()
  261. d2 := t2.Day()
  262. yearInterval := y1 - y2
  263. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  264. if m1 < m2 || m1 == m2 && d1 < d2 {
  265. yearInterval--
  266. }
  267. // 获取月数差值
  268. monthInterval := (m1 + 12) - m2
  269. if d1 < d2 {
  270. monthInterval--
  271. }
  272. monthInterval %= 12
  273. month = yearInterval*12 + monthInterval
  274. return month
  275. }
  276. func QueryAllRepo() []*Repository {
  277. sess := x.NewSession()
  278. defer sess.Close()
  279. sess.Select("*").Table("repository")
  280. repositoryList := make([]*Repository, 0)
  281. sess.Find(&repositoryList)
  282. return repositoryList
  283. }