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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 :ActionCommentIssue // 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:"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. 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) {
  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, 10)
  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. statictisSess.Insert(&dateRecord)
  327. }
  328. }
  329. func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
  330. CounDataByDateAndReCount(wikiCountMap, startTime, endTime, false)
  331. }
  332. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  333. //select issue_assignees.* from issue_assignees,issue where issue.is_closed=true and issue.id=issue_assignees.issue_id
  334. sess := x.NewSession()
  335. defer sess.Close()
  336. sess.Select("issue_assignees.*").Table("issue_assignees").
  337. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  338. Where("issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix))
  339. issueAssigneesList := make([]*IssueAssignees, 0)
  340. sess.Find(&issueAssigneesList)
  341. resultMap := make(map[int64]int)
  342. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  343. for _, issueAssigneesRecord := range issueAssigneesList {
  344. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  345. resultMap[issueAssigneesRecord.AssigneeID] = 1
  346. } else {
  347. resultMap[issueAssigneesRecord.AssigneeID] += 1
  348. }
  349. }
  350. return resultMap
  351. }
  352. func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
  353. sess := x.NewSession()
  354. defer sess.Close()
  355. sess.Select("issue.*").Table("issue").
  356. Join("inner", "pull_request", "issue.id=pull_request.issue_id").
  357. Where("pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix))
  358. issueList := make([]*Issue, 0)
  359. sess.Find(&issueList)
  360. resultMap := make(map[int64]int)
  361. log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
  362. for _, issueRecord := range issueList {
  363. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  364. resultMap[issueRecord.PosterID] = 1
  365. } else {
  366. resultMap[issueRecord.PosterID] += 1
  367. }
  368. }
  369. return resultMap
  370. }
  371. func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  372. sess := x.NewSession()
  373. defer sess.Close()
  374. 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))
  375. actionList := make([]*Action, 0)
  376. sess.Find(&actionList)
  377. resultMap := make(map[int64]int)
  378. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  379. for _, actionRecord := range actionList {
  380. if _, ok := resultMap[actionRecord.UserID]; !ok {
  381. resultMap[actionRecord.UserID] = 1
  382. } else {
  383. resultMap[actionRecord.UserID] += 1
  384. }
  385. }
  386. return resultMap
  387. }
  388. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  389. sess := x.NewSession()
  390. defer sess.Close()
  391. sess.Select("id,type,poster_id").Table("comment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  392. commentList := make([]*Comment, 0)
  393. sess.Find(&commentList)
  394. resultMap := make(map[int64]int)
  395. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  396. for _, commentRecord := range commentList {
  397. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  398. resultMap[commentRecord.PosterID] = 1
  399. } else {
  400. resultMap[commentRecord.PosterID] += 1
  401. }
  402. }
  403. return resultMap
  404. }
  405. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  406. sess := x.NewSession()
  407. defer sess.Close()
  408. sess.Select("id,user_id,repo_id").Table("watch").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  409. watchList := make([]*Watch, 0)
  410. sess.Find(&watchList)
  411. resultMap := make(map[int64]int)
  412. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  413. for _, watchRecord := range watchList {
  414. if _, ok := resultMap[watchRecord.UserID]; !ok {
  415. resultMap[watchRecord.UserID] = 1
  416. } else {
  417. resultMap[watchRecord.UserID] += 1
  418. }
  419. }
  420. return resultMap
  421. }
  422. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  423. sess := x.NewSession()
  424. defer sess.Close()
  425. sess.Select("id,uid,repo_id").Table("star").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  426. starList := make([]*Star, 0)
  427. sess.Find(&starList)
  428. resultMap := make(map[int64]int)
  429. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  430. for _, starRecord := range starList {
  431. if _, ok := resultMap[starRecord.UID]; !ok {
  432. resultMap[starRecord.UID] = 1
  433. } else {
  434. resultMap[starRecord.UID] += 1
  435. }
  436. }
  437. return resultMap
  438. }
  439. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  440. sess := x.NewSession()
  441. defer sess.Close()
  442. sess.Select("id,user_id,follow_id").Table("follow").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  443. followList := make([]*Follow, 0)
  444. sess.Find(&followList)
  445. resultMap := make(map[int64]int)
  446. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  447. for _, followRecord := range followList {
  448. if _, ok := resultMap[followRecord.FollowID]; !ok {
  449. resultMap[followRecord.FollowID] = 1
  450. } else {
  451. resultMap[followRecord.FollowID] += 1
  452. }
  453. }
  454. return resultMap
  455. }
  456. func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int {
  457. sess := x.NewSession()
  458. defer sess.Close()
  459. sess.Select("id,uploader_id,size").Table("attachment").Where(" created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  460. attachmentList := make([]*Attachment, 0)
  461. sess.Find(&attachmentList)
  462. resultMap := make(map[int64]int)
  463. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  464. for _, attachRecord := range attachmentList {
  465. if _, ok := resultMap[attachRecord.UploaderID]; !ok {
  466. resultMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  467. } else {
  468. resultMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  469. }
  470. }
  471. return resultMap
  472. }
  473. func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
  474. sess := x.NewSession()
  475. defer sess.Close()
  476. 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))
  477. repoList := make([]*Repository, 0)
  478. sess.Find(&repoList)
  479. resultMap := make(map[int64]int)
  480. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  481. for _, repoRecord := range repoList {
  482. if _, ok := resultMap[repoRecord.OwnerID]; !ok {
  483. resultMap[repoRecord.OwnerID] = 1
  484. } else {
  485. resultMap[repoRecord.OwnerID] += 1
  486. }
  487. }
  488. return resultMap
  489. }
  490. func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
  491. statictisSess := xStatistic.NewSession()
  492. defer statictisSess.Close()
  493. statictisSess.Select("repo_id,radar_total").Table("repo_statistic").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  494. repoStatisticList := make([]*RepoStatistic, 0)
  495. statictisSess.Find(&repoStatisticList)
  496. repoOpenIIndexMap := make(map[int64]float64)
  497. log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
  498. for _, repoRecord := range repoStatisticList {
  499. if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
  500. repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
  501. }
  502. }
  503. sess := x.NewSession()
  504. defer sess.Close()
  505. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
  506. repoList := make([]*Repository, 0)
  507. sess.Find(&repoList)
  508. userMap := make(map[int64]float64)
  509. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  510. for _, repoRecord := range repoList {
  511. if _, ok := userMap[repoRecord.OwnerID]; !ok {
  512. if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
  513. userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
  514. }
  515. }
  516. }
  517. //query collaboration
  518. sess.Select("repo_id,user_id,mode").Table("collaboration")
  519. collaborationList := make([]*Collaboration, 0)
  520. sess.Find(&collaborationList)
  521. log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
  522. for _, collaborationRecord := range collaborationList {
  523. if _, ok := userMap[collaborationRecord.UserID]; !ok {
  524. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  525. userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
  526. }
  527. } else {
  528. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  529. userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
  530. }
  531. }
  532. }
  533. userMapJson, _ := json.Marshal(userMap)
  534. log.Info("userMapJson=" + string(userMapJson))
  535. return userMap
  536. }
  537. func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
  538. statictisSess := xStatistic.NewSession()
  539. defer statictisSess.Close()
  540. statictisSess.Select("id,u_id").Table("user_login_log").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix))
  541. userLoginLogList := make([]*UserLoginLog, 0)
  542. statictisSess.Find(&userLoginLogList)
  543. resultMap := make(map[int64]int)
  544. log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
  545. for _, loginRecord := range userLoginLogList {
  546. if _, ok := resultMap[loginRecord.UId]; !ok {
  547. resultMap[loginRecord.UId] = 1
  548. } else {
  549. resultMap[loginRecord.UId] += 1
  550. }
  551. }
  552. return resultMap
  553. }
  554. func subMonth(t1, t2 time.Time) (month int) {
  555. y1 := t1.Year()
  556. y2 := t2.Year()
  557. m1 := int(t1.Month())
  558. m2 := int(t2.Month())
  559. d1 := t1.Day()
  560. d2 := t2.Day()
  561. yearInterval := y1 - y2
  562. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  563. if m1 < m2 || m1 == m2 && d1 < d2 {
  564. yearInterval--
  565. }
  566. // 获取月数差值
  567. monthInterval := (m1 + 12) - m2
  568. if d1 < d2 {
  569. monthInterval--
  570. }
  571. monthInterval %= 12
  572. month = yearInterval*12 + monthInterval
  573. return month
  574. }