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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package models
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. "time"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/builder"
  11. )
  12. type UserBusinessAnalysis struct {
  13. ID int64 `xorm:"pk"`
  14. CountDate int64 `xorm:"pk"`
  15. //action :ActionMergePullRequest // 11
  16. CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
  17. //action :ActionCommitRepo // 5
  18. CommitCount int `xorm:"NOT NULL DEFAULT 0"`
  19. //action :ActionCommentIssue // 10
  20. IssueCount int `xorm:"NOT NULL DEFAULT 0"`
  21. //comment table current date
  22. CommentCount int `xorm:"NOT NULL DEFAULT 0"`
  23. //watch table current date
  24. FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  25. //star table current date
  26. StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  27. //follow table
  28. WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
  29. // user table
  30. GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
  31. //
  32. CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
  33. //attachement table
  34. CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
  35. //0
  36. CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
  37. //issue, issueassignees
  38. SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
  39. //baike
  40. EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
  41. //user
  42. RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
  43. //repo
  44. CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  45. //login count, from elk
  46. LoginCount int `xorm:"NOT NULL DEFAULT 0"`
  47. //openi index
  48. OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  49. //user
  50. Email string `xorm:"NOT NULL"`
  51. //user
  52. Name string `xorm:"NOT NULL"`
  53. }
  54. type UserBusinessAnalysisQueryOptions struct {
  55. ListOptions
  56. UserName string
  57. SortType string
  58. StartTime int64
  59. EndTime int64
  60. }
  61. type UserBusinessAnalysisList []*UserBusinessAnalysis
  62. func (ulist UserBusinessAnalysisList) Swap(i, j int) { ulist[i], ulist[j] = ulist[j], ulist[i] }
  63. func (ulist UserBusinessAnalysisList) Len() int { return len(ulist) }
  64. func (ulist UserBusinessAnalysisList) Less(i, j int) bool {
  65. return ulist[i].CommitCount > ulist[j].CommitCount
  66. }
  67. func QueryUserStaticData(startTime int64, endTime int64) []*UserBusinessAnalysis {
  68. log.Info("query startTime =" + fmt.Sprint(startTime) + " endTime=" + fmt.Sprint(endTime))
  69. statictisSess := xStatistic.NewSession()
  70. defer statictisSess.Close()
  71. statictisSess.Select("*").Table("user_business_analysis").Where(" count_date>=" + fmt.Sprint(startTime) + " and count_date<=" + fmt.Sprint(endTime)).OrderBy("count_date desc")
  72. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  73. statictisSess.Find(&userBusinessAnalysisList)
  74. resultMap := make(map[int64]*UserBusinessAnalysis)
  75. log.Info("query result size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  76. for _, userRecord := range userBusinessAnalysisList {
  77. if _, ok := resultMap[userRecord.ID]; !ok {
  78. resultMap[userRecord.ID] = userRecord
  79. } else {
  80. resultMap[userRecord.ID].CodeMergeCount += userRecord.CodeMergeCount
  81. resultMap[userRecord.ID].CommitCount += userRecord.CommitCount
  82. resultMap[userRecord.ID].IssueCount += userRecord.IssueCount
  83. resultMap[userRecord.ID].CommentCount += userRecord.CommentCount
  84. resultMap[userRecord.ID].FocusRepoCount += userRecord.FocusRepoCount
  85. resultMap[userRecord.ID].StarRepoCount += userRecord.StarRepoCount
  86. resultMap[userRecord.ID].WatchedCount += userRecord.WatchedCount
  87. resultMap[userRecord.ID].CommitCodeSize += userRecord.CommitCodeSize
  88. resultMap[userRecord.ID].CommitDatasetSize += userRecord.CommitDatasetSize
  89. resultMap[userRecord.ID].CommitModelCount += userRecord.CommitModelCount
  90. resultMap[userRecord.ID].SolveIssueCount += userRecord.SolveIssueCount
  91. resultMap[userRecord.ID].EncyclopediasCount += userRecord.EncyclopediasCount
  92. resultMap[userRecord.ID].CreateRepoCount += userRecord.CreateRepoCount
  93. resultMap[userRecord.ID].LoginCount += userRecord.LoginCount
  94. }
  95. }
  96. userBusinessAnalysisReturnList := UserBusinessAnalysisList{}
  97. for _, v := range resultMap {
  98. userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, v)
  99. }
  100. sort.Sort(userBusinessAnalysisReturnList)
  101. log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
  102. return userBusinessAnalysisReturnList
  103. }
  104. func QueryUserStaticDataPage(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysis, int64) {
  105. log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime))
  106. statictisSess := xStatistic.NewSession()
  107. defer statictisSess.Close()
  108. currentTimeNow := time.Now()
  109. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  110. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  111. var cond = builder.NewCond()
  112. if len(opts.UserName) > 0 {
  113. cond = cond.And(
  114. builder.Eq{"name": opts.UserName},
  115. )
  116. }
  117. cond = cond.And(
  118. builder.Gte{"count_date": fmt.Sprint(pageStartTime)},
  119. )
  120. cond = cond.And(
  121. builder.Lte{"count_date": fmt.Sprint(pageEndTime)},
  122. )
  123. count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
  124. if err != nil {
  125. log.Info("query error." + err.Error())
  126. return nil, 0
  127. }
  128. if opts.Page >= 0 && opts.PageSize > 0 {
  129. var start int
  130. if opts.Page == 0 {
  131. start = 0
  132. } else {
  133. start = (opts.Page - 1) * opts.PageSize
  134. }
  135. statictisSess.Limit(opts.PageSize, start)
  136. }
  137. statictisSess.OrderBy("count_date desc")
  138. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0, setting.UI.IssuePagingNum)
  139. if err := statictisSess.Table("user_business_analysis").Where(cond).
  140. Find(&userBusinessAnalysisList); err != nil {
  141. return nil, 0
  142. }
  143. resultMap := make(map[int64]*UserBusinessAnalysis)
  144. var newAndCond = builder.NewCond()
  145. var newOrCond = builder.NewCond()
  146. for _, userRecord := range userBusinessAnalysisList {
  147. newOrCond.Or(
  148. builder.Eq{"id": userRecord.ID},
  149. )
  150. }
  151. newAndCond = newAndCond.And(
  152. newOrCond,
  153. )
  154. newAndCond = newAndCond.And(
  155. builder.Gte{"count_date": fmt.Sprint(opts.StartTime)},
  156. )
  157. newAndCond = newAndCond.And(
  158. builder.Lte{"count_date": fmt.Sprint(opts.EndTime)},
  159. )
  160. userBusinessAnalysisList = make([]*UserBusinessAnalysis, 0)
  161. if err := statictisSess.Table("user_business_analysis").Where(newAndCond).
  162. Find(&userBusinessAnalysisList); err != nil {
  163. return nil, 0
  164. }
  165. log.Info("query result size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  166. for _, userRecord := range userBusinessAnalysisList {
  167. if _, ok := resultMap[userRecord.ID]; !ok {
  168. resultMap[userRecord.ID] = userRecord
  169. } else {
  170. resultMap[userRecord.ID].CodeMergeCount += userRecord.CodeMergeCount
  171. resultMap[userRecord.ID].CommitCount += userRecord.CommitCount
  172. resultMap[userRecord.ID].IssueCount += userRecord.IssueCount
  173. resultMap[userRecord.ID].CommentCount += userRecord.CommentCount
  174. resultMap[userRecord.ID].FocusRepoCount += userRecord.FocusRepoCount
  175. resultMap[userRecord.ID].StarRepoCount += userRecord.StarRepoCount
  176. resultMap[userRecord.ID].WatchedCount += userRecord.WatchedCount
  177. resultMap[userRecord.ID].CommitCodeSize += userRecord.CommitCodeSize
  178. resultMap[userRecord.ID].CommitDatasetSize += userRecord.CommitDatasetSize
  179. resultMap[userRecord.ID].CommitModelCount += userRecord.CommitModelCount
  180. resultMap[userRecord.ID].SolveIssueCount += userRecord.SolveIssueCount
  181. resultMap[userRecord.ID].EncyclopediasCount += userRecord.EncyclopediasCount
  182. resultMap[userRecord.ID].CreateRepoCount += userRecord.CreateRepoCount
  183. resultMap[userRecord.ID].LoginCount += userRecord.LoginCount
  184. }
  185. }
  186. userBusinessAnalysisReturnList := UserBusinessAnalysisList{}
  187. for _, v := range resultMap {
  188. userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, v)
  189. }
  190. sort.Sort(userBusinessAnalysisReturnList)
  191. log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
  192. return userBusinessAnalysisReturnList, count
  193. }
  194. func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
  195. log.Info("start to count other user info data")
  196. sess := x.NewSession()
  197. defer sess.Close()
  198. sess.Select("`user`.*").Table("user").Where("type != 1 and is_active=true")
  199. userList := make([]*User, 0)
  200. sess.Find(&userList)
  201. currentTimeNow := time.Now()
  202. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  203. //yesterday := currentTimeNow.AddDate(0, 0, -1)
  204. //startTime := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location())
  205. start_unix := startTime.Unix()
  206. log.Info("DB query time:" + startTime.Format("2006-01-02 15:04:05"))
  207. //endTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  208. end_unix := endTime.Unix()
  209. CountDate := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  210. CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
  211. CommitCountMap := queryAction(start_unix, end_unix, 5)
  212. IssueCountMap := queryAction(start_unix, end_unix, 10)
  213. CommentCountMap := queryComment(start_unix, end_unix)
  214. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  215. StarRepoCountMap := queryStar(start_unix, end_unix)
  216. WatchedCountMap := queryFollow(start_unix, end_unix)
  217. CommitCodeSizeMap, err := GetAllUserKPIStats()
  218. if err != nil {
  219. log.Info("query commit code errr.")
  220. } else {
  221. log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
  222. }
  223. CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
  224. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  225. CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
  226. LoginCountMap := queryLoginCount(start_unix, end_unix)
  227. OpenIIndexMap := queryUserRepoOpenIIndex(start_unix, end_unix)
  228. statictisSess := xStatistic.NewSession()
  229. defer statictisSess.Close()
  230. for i, userRecord := range userList {
  231. var dateRecord UserBusinessAnalysis
  232. dateRecord.ID = userRecord.ID
  233. log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  234. dateRecord.CountDate = CountDate.Unix()
  235. statictisSess.Delete(&dateRecord)
  236. dateRecord.Email = userRecord.Email
  237. dateRecord.RegistDate = userRecord.CreatedUnix
  238. dateRecord.Name = userRecord.Name
  239. dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  240. if _, ok := CodeMergeCountMap[dateRecord.ID]; !ok {
  241. dateRecord.CodeMergeCount = 0
  242. } else {
  243. dateRecord.CodeMergeCount = CodeMergeCountMap[dateRecord.ID]
  244. }
  245. if _, ok := CommitCountMap[dateRecord.ID]; !ok {
  246. dateRecord.CommitCount = 0
  247. } else {
  248. dateRecord.CommitCount = CommitCountMap[dateRecord.ID]
  249. }
  250. if _, ok := IssueCountMap[dateRecord.ID]; !ok {
  251. dateRecord.IssueCount = 0
  252. } else {
  253. dateRecord.IssueCount = IssueCountMap[dateRecord.ID]
  254. }
  255. if _, ok := CommentCountMap[dateRecord.ID]; !ok {
  256. dateRecord.CommentCount = 0
  257. } else {
  258. dateRecord.CommentCount = CommentCountMap[dateRecord.ID]
  259. }
  260. if _, ok := FocusRepoCountMap[dateRecord.ID]; !ok {
  261. dateRecord.FocusRepoCount = 0
  262. } else {
  263. dateRecord.FocusRepoCount = FocusRepoCountMap[dateRecord.ID]
  264. }
  265. if _, ok := StarRepoCountMap[dateRecord.ID]; !ok {
  266. dateRecord.StarRepoCount = 0
  267. } else {
  268. dateRecord.StarRepoCount = StarRepoCountMap[dateRecord.ID]
  269. }
  270. if _, ok := WatchedCountMap[dateRecord.ID]; !ok {
  271. dateRecord.WatchedCount = 0
  272. } else {
  273. dateRecord.WatchedCount = WatchedCountMap[dateRecord.ID]
  274. }
  275. if _, ok := CommitCodeSizeMap[dateRecord.Email]; !ok {
  276. dateRecord.CommitCodeSize = 0
  277. } else {
  278. dateRecord.CommitCodeSize = int(CommitCodeSizeMap[dateRecord.Email].CommitLines)
  279. }
  280. if _, ok := CommitDatasetSizeMap[dateRecord.ID]; !ok {
  281. dateRecord.CommitDatasetSize = 0
  282. } else {
  283. dateRecord.CommitDatasetSize = CommitDatasetSizeMap[dateRecord.ID]
  284. }
  285. if _, ok := SolveIssueCountMap[dateRecord.ID]; !ok {
  286. dateRecord.SolveIssueCount = 0
  287. } else {
  288. dateRecord.SolveIssueCount = SolveIssueCountMap[dateRecord.ID]
  289. }
  290. if _, ok := wikiCountMap[dateRecord.Name]; !ok {
  291. dateRecord.EncyclopediasCount = 0
  292. } else {
  293. dateRecord.EncyclopediasCount = wikiCountMap[dateRecord.Name]
  294. }
  295. if _, ok := CreateRepoCountMap[dateRecord.ID]; !ok {
  296. dateRecord.CreateRepoCount = 0
  297. } else {
  298. dateRecord.CreateRepoCount = CreateRepoCountMap[dateRecord.ID]
  299. }
  300. if _, ok := LoginCountMap[dateRecord.ID]; !ok {
  301. dateRecord.LoginCount = 0
  302. } else {
  303. dateRecord.LoginCount = LoginCountMap[dateRecord.ID]
  304. }
  305. if _, ok := OpenIIndexMap[dateRecord.ID]; !ok {
  306. dateRecord.OpenIIndex = 0
  307. } else {
  308. dateRecord.OpenIIndex = OpenIIndexMap[dateRecord.ID]
  309. }
  310. dateRecord.CommitModelCount = 0
  311. statictisSess.Insert(&dateRecord)
  312. }
  313. }
  314. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  315. //select issue_assignees.* from issue_assignees,issue where issue.is_closed=true and issue.id=issue_assignees.issue_id
  316. sess := x.NewSession()
  317. defer sess.Close()
  318. sess.Select("issue_assignees.*").Table("issue_assignees").
  319. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  320. Where("issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix))
  321. issueAssigneesList := make([]*IssueAssignees, 0)
  322. sess.Find(&issueAssigneesList)
  323. resultMap := make(map[int64]int)
  324. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  325. for _, issueAssigneesRecord := range issueAssigneesList {
  326. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  327. resultMap[issueAssigneesRecord.AssigneeID] = 1
  328. } else {
  329. resultMap[issueAssigneesRecord.AssigneeID] += 1
  330. }
  331. }
  332. return resultMap
  333. }
  334. func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
  335. sess := x.NewSession()
  336. defer sess.Close()
  337. sess.Select("issue.*").Table("issue").
  338. Join("inner", "pull_request", "issue.id=pull_request.issue_id").
  339. Where("pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix))
  340. issueList := make([]*Issue, 0)
  341. sess.Find(&issueList)
  342. resultMap := make(map[int64]int)
  343. log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
  344. for _, issueRecord := range issueList {
  345. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  346. resultMap[issueRecord.PosterID] = 1
  347. } else {
  348. resultMap[issueRecord.PosterID] += 1
  349. }
  350. }
  351. return resultMap
  352. }
  353. func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  354. sess := x.NewSession()
  355. defer sess.Close()
  356. 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))
  357. actionList := make([]*Action, 0)
  358. sess.Find(&actionList)
  359. resultMap := make(map[int64]int)
  360. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  361. for _, actionRecord := range actionList {
  362. if _, ok := resultMap[actionRecord.UserID]; !ok {
  363. resultMap[actionRecord.UserID] = 1
  364. } else {
  365. resultMap[actionRecord.UserID] += 1
  366. }
  367. }
  368. return resultMap
  369. }
  370. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  371. sess := x.NewSession()
  372. defer sess.Close()
  373. sess.Select("id,type,poster_id").Table("comment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  374. commentList := make([]*Comment, 0)
  375. sess.Find(&commentList)
  376. resultMap := make(map[int64]int)
  377. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  378. for _, commentRecord := range commentList {
  379. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  380. resultMap[commentRecord.PosterID] = 1
  381. } else {
  382. resultMap[commentRecord.PosterID] += 1
  383. }
  384. }
  385. return resultMap
  386. }
  387. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  388. sess := x.NewSession()
  389. defer sess.Close()
  390. sess.Select("id,user_id,repo_id").Table("watch").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  391. watchList := make([]*Watch, 0)
  392. sess.Find(&watchList)
  393. resultMap := make(map[int64]int)
  394. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  395. for _, watchRecord := range watchList {
  396. if _, ok := resultMap[watchRecord.UserID]; !ok {
  397. resultMap[watchRecord.UserID] = 1
  398. } else {
  399. resultMap[watchRecord.UserID] += 1
  400. }
  401. }
  402. return resultMap
  403. }
  404. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  405. sess := x.NewSession()
  406. defer sess.Close()
  407. sess.Select("id,uid,repo_id").Table("star").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  408. starList := make([]*Star, 0)
  409. sess.Find(&starList)
  410. resultMap := make(map[int64]int)
  411. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  412. for _, starRecord := range starList {
  413. if _, ok := resultMap[starRecord.UID]; !ok {
  414. resultMap[starRecord.UID] = 1
  415. } else {
  416. resultMap[starRecord.UID] += 1
  417. }
  418. }
  419. return resultMap
  420. }
  421. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  422. sess := x.NewSession()
  423. defer sess.Close()
  424. sess.Select("id,user_id,follow_id").Table("follow").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  425. followList := make([]*Follow, 0)
  426. sess.Find(&followList)
  427. resultMap := make(map[int64]int)
  428. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  429. for _, followRecord := range followList {
  430. if _, ok := resultMap[followRecord.UserID]; !ok {
  431. resultMap[followRecord.UserID] = 1
  432. } else {
  433. resultMap[followRecord.UserID] += 1
  434. }
  435. }
  436. return resultMap
  437. }
  438. func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int {
  439. sess := x.NewSession()
  440. defer sess.Close()
  441. sess.Select("id,uploader_id,size").Table("attachment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  442. attachmentList := make([]*Attachment, 0)
  443. sess.Find(&attachmentList)
  444. resultMap := make(map[int64]int)
  445. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  446. for _, attachRecord := range attachmentList {
  447. if _, ok := resultMap[attachRecord.UploaderID]; !ok {
  448. resultMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  449. } else {
  450. resultMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  451. }
  452. }
  453. return resultMap
  454. }
  455. func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
  456. sess := x.NewSession()
  457. defer sess.Close()
  458. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  459. repoList := make([]*Repository, 0)
  460. sess.Find(&repoList)
  461. resultMap := make(map[int64]int)
  462. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  463. for _, repoRecord := range repoList {
  464. if _, ok := resultMap[repoRecord.OwnerID]; !ok {
  465. resultMap[repoRecord.OwnerID] = 1
  466. } else {
  467. resultMap[repoRecord.OwnerID] += 1
  468. }
  469. }
  470. return resultMap
  471. }
  472. func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
  473. statictisSess := xStatistic.NewSession()
  474. defer statictisSess.Close()
  475. statictisSess.Select("repo_id,radar_total").Table("repo_statistic").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  476. repoStatisticList := make([]*RepoStatistic, 0)
  477. statictisSess.Find(&repoStatisticList)
  478. repoOpenIIndexMap := make(map[int64]float64)
  479. log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
  480. for _, repoRecord := range repoStatisticList {
  481. if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
  482. repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
  483. }
  484. }
  485. sess := x.NewSession()
  486. defer sess.Close()
  487. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
  488. repoList := make([]*Repository, 0)
  489. sess.Find(&repoList)
  490. userMap := make(map[int64]float64)
  491. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  492. for _, repoRecord := range repoList {
  493. if _, ok := userMap[repoRecord.OwnerID]; !ok {
  494. if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
  495. userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
  496. }
  497. }
  498. }
  499. //query collaboration
  500. sess.Select("repo_id,user_id,mode").Table("collaboration")
  501. collaborationList := make([]*Collaboration, 0)
  502. sess.Find(&collaborationList)
  503. log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
  504. for _, collaborationRecord := range collaborationList {
  505. if _, ok := userMap[collaborationRecord.UserID]; !ok {
  506. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  507. userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
  508. }
  509. } else {
  510. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  511. userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
  512. }
  513. }
  514. }
  515. userMapJson, _ := json.Marshal(userMap)
  516. log.Info("userMapJson=" + string(userMapJson))
  517. return userMap
  518. }
  519. func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
  520. statictisSess := xStatistic.NewSession()
  521. defer statictisSess.Close()
  522. statictisSess.Select("id,u_id").Table("user_login_log").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  523. userLoginLogList := make([]*UserLoginLog, 0)
  524. statictisSess.Find(&userLoginLogList)
  525. resultMap := make(map[int64]int)
  526. log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
  527. for _, loginRecord := range userLoginLogList {
  528. if _, ok := resultMap[loginRecord.UId]; !ok {
  529. resultMap[loginRecord.UId] = 1
  530. } else {
  531. resultMap[loginRecord.UId] += 1
  532. }
  533. }
  534. return resultMap
  535. }
  536. func subMonth(t1, t2 time.Time) (month int) {
  537. y1 := t1.Year()
  538. y2 := t2.Year()
  539. m1 := int(t1.Month())
  540. m2 := int(t2.Month())
  541. d1 := t1.Day()
  542. d2 := t2.Day()
  543. yearInterval := y1 - y2
  544. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  545. if m1 < m2 || m1 == m2 && d1 < d2 {
  546. yearInterval--
  547. }
  548. // 获取月数差值
  549. monthInterval := (m1 + 12) - m2
  550. if d1 < d2 {
  551. monthInterval--
  552. }
  553. monthInterval %= 12
  554. month = yearInterval*12 + monthInterval
  555. return month
  556. }