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

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