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

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