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

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