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

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