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

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