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

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