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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. package models
  2. import (
  3. "fmt"
  4. "sort"
  5. "strconv"
  6. "time"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/builder"
  11. )
  12. const (
  13. Page_SIZE = 2000
  14. )
  15. type UserBusinessAnalysisAll struct {
  16. ID int64 `xorm:"pk"`
  17. CountDate int64 `xorm:"pk"`
  18. //action :ActionMergePullRequest // 11
  19. CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
  20. //action :ActionCommitRepo // 5
  21. CommitCount int `xorm:"NOT NULL DEFAULT 0"`
  22. //action :ActionCreateIssue // 10
  23. IssueCount int `xorm:"NOT NULL DEFAULT 0"`
  24. //comment table current date
  25. CommentCount int `xorm:"NOT NULL DEFAULT 0"`
  26. //watch table current date
  27. FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  28. //star table current date
  29. StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  30. //follow table
  31. WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
  32. // user table
  33. GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
  34. //
  35. CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
  36. //attachement table
  37. CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
  38. //0
  39. CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
  40. //issue, issueassignees
  41. SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
  42. //baike
  43. EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
  44. //user
  45. RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
  46. //repo
  47. CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  48. //login count, from elk
  49. LoginCount int `xorm:"NOT NULL DEFAULT 0"`
  50. //openi index
  51. OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  52. //user
  53. Email string `xorm:"NOT NULL"`
  54. //user
  55. Name string `xorm:"NOT NULL"`
  56. DataDate string `xorm:"NULL"`
  57. }
  58. type UserBusinessAnalysis struct {
  59. ID int64 `xorm:"pk"`
  60. CountDate int64 `xorm:"pk"`
  61. //action :ActionMergePullRequest // 11
  62. CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
  63. //action :ActionCommitRepo // 5
  64. CommitCount int `xorm:"NOT NULL DEFAULT 0"`
  65. //action :ActionCreateIssue // 6
  66. IssueCount int `xorm:"NOT NULL DEFAULT 0"`
  67. //comment table current date
  68. CommentCount int `xorm:"NOT NULL DEFAULT 0"`
  69. //watch table current date
  70. FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  71. //star table current date
  72. StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  73. //follow table
  74. WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
  75. // user table
  76. GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
  77. //
  78. CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
  79. //attachement table
  80. CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
  81. //0
  82. CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
  83. //issue, issueassignees
  84. SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
  85. //baike
  86. EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
  87. //user
  88. RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
  89. //repo
  90. CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  91. //login count, from elk
  92. LoginCount int `xorm:"NOT NULL DEFAULT 0"`
  93. //openi index
  94. OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  95. //user
  96. Email string `xorm:"NOT NULL"`
  97. //user
  98. Name string `xorm:"NOT NULL"`
  99. DataDate string `xorm:"NULL"`
  100. }
  101. type UserBusinessAnalysisQueryOptions struct {
  102. ListOptions
  103. UserName string
  104. SortType string
  105. StartTime int64
  106. EndTime int64
  107. IsAll bool
  108. }
  109. type UserBusinessAnalysisList []*UserBusinessAnalysis
  110. func (ulist UserBusinessAnalysisList) Swap(i, j int) { ulist[i], ulist[j] = ulist[j], ulist[i] }
  111. func (ulist UserBusinessAnalysisList) Len() int { return len(ulist) }
  112. func (ulist UserBusinessAnalysisList) Less(i, j int) bool {
  113. return ulist[i].ID > ulist[j].ID
  114. }
  115. type UserBusinessAnalysisAllList []*UserBusinessAnalysisAll
  116. func (ulist UserBusinessAnalysisAllList) Swap(i, j int) { ulist[i], ulist[j] = ulist[j], ulist[i] }
  117. func (ulist UserBusinessAnalysisAllList) Len() int { return len(ulist) }
  118. func (ulist UserBusinessAnalysisAllList) Less(i, j int) bool {
  119. return ulist[i].ID > ulist[j].ID
  120. }
  121. func getLastCountDate() int64 {
  122. statictisSess := xStatistic.NewSession()
  123. defer statictisSess.Close()
  124. statictisSess.Limit(1, 0)
  125. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  126. if err := statictisSess.Table("user_business_analysis").OrderBy("count_date desc").Limit(1, 0).
  127. Find(&userBusinessAnalysisList); err == nil {
  128. for _, userRecord := range userBusinessAnalysisList {
  129. return userRecord.CountDate - 10000
  130. }
  131. } else {
  132. log.Info("query error." + err.Error())
  133. }
  134. currentTimeNow := time.Now()
  135. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  136. return pageStartTime.Unix()
  137. }
  138. func QueryUserStaticDataAll(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysisAll, int64) {
  139. log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll))
  140. statictisSess := xStatistic.NewSession()
  141. defer statictisSess.Close()
  142. allCount, err := statictisSess.Count(new(UserBusinessAnalysisAll))
  143. if err != nil {
  144. log.Info("query error." + err.Error())
  145. return nil, 0
  146. }
  147. log.Info("query return total:" + fmt.Sprint(allCount))
  148. if allCount == 0 {
  149. CommitCodeSizeMap, err := GetAllUserKPIStats()
  150. if err != nil {
  151. log.Info("query commit code errr.")
  152. } else {
  153. log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
  154. }
  155. RefreshUserStaticAllTabel(make(map[string]int), CommitCodeSizeMap)
  156. }
  157. pageSize := 1000
  158. totalPage := int(allCount) / pageSize
  159. userBusinessAnalysisReturnList := UserBusinessAnalysisAllList{}
  160. for i := 0; i <= int(totalPage); i++ {
  161. userBusinessAnalysisAllList := make([]*UserBusinessAnalysisAll, 0)
  162. if err := statictisSess.Table("user_business_analysis_all").OrderBy("id desc").Limit(pageSize, i*pageSize).
  163. Find(&userBusinessAnalysisAllList); err != nil {
  164. return nil, 0
  165. }
  166. log.Info("query " + fmt.Sprint(i+1) + " result size=" + fmt.Sprint(len(userBusinessAnalysisAllList)))
  167. for _, userRecord := range userBusinessAnalysisAllList {
  168. userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, userRecord)
  169. }
  170. }
  171. sort.Sort(userBusinessAnalysisReturnList)
  172. log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
  173. return userBusinessAnalysisReturnList, allCount
  174. }
  175. func QueryUserStaticDataPage(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysis, int64) {
  176. log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll))
  177. statictisSess := xStatistic.NewSession()
  178. defer statictisSess.Close()
  179. currentTimeNow := time.Now()
  180. pageStartTime := getLastCountDate()
  181. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()).Unix()
  182. var cond = builder.NewCond()
  183. if len(opts.UserName) > 0 {
  184. cond = cond.And(
  185. builder.Like{"name", opts.UserName},
  186. )
  187. }
  188. cond = cond.And(
  189. builder.Gte{"count_date": pageStartTime},
  190. )
  191. cond = cond.And(
  192. builder.Lte{"count_date": pageEndTime},
  193. )
  194. count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
  195. if err != nil {
  196. log.Info("query error." + err.Error())
  197. return nil, 0
  198. }
  199. if opts.Page >= 0 && opts.PageSize > 0 {
  200. var start int
  201. if opts.Page == 0 {
  202. start = 0
  203. } else {
  204. start = (opts.Page - 1) * opts.PageSize
  205. }
  206. statictisSess.Limit(opts.PageSize, start)
  207. }
  208. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  209. if err := statictisSess.Table("user_business_analysis").Where(cond).OrderBy("id desc").
  210. Find(&userBusinessAnalysisList); err != nil {
  211. return nil, 0
  212. }
  213. resultMap := make(map[int64]*UserBusinessAnalysis)
  214. if len(userBusinessAnalysisList) > 0 {
  215. var newAndCond = builder.NewCond()
  216. var newOrCond = builder.NewCond()
  217. for _, userRecord := range userBusinessAnalysisList {
  218. newOrCond = newOrCond.Or(
  219. builder.Eq{"id": userRecord.ID},
  220. )
  221. }
  222. newAndCond = newAndCond.And(
  223. newOrCond,
  224. )
  225. if !opts.IsAll {
  226. newAndCond = newAndCond.And(
  227. builder.Gte{"count_date": opts.StartTime},
  228. )
  229. newAndCond = newAndCond.And(
  230. builder.Lte{"count_date": opts.EndTime},
  231. )
  232. }
  233. allCount, err := statictisSess.Where(newAndCond).Count(new(UserBusinessAnalysis))
  234. if err != nil {
  235. log.Info("query error." + err.Error())
  236. return nil, 0
  237. }
  238. pageSize := 1000
  239. totalPage := int(allCount) / pageSize
  240. for i := 0; i <= int(totalPage); i++ {
  241. userBusinessAnalysisList = make([]*UserBusinessAnalysis, 0)
  242. if err := statictisSess.Table("user_business_analysis").Where(newAndCond).OrderBy("count_date desc").Limit(pageSize, i*pageSize).
  243. Find(&userBusinessAnalysisList); err != nil {
  244. return nil, 0
  245. }
  246. log.Info("query result size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  247. for _, userRecord := range userBusinessAnalysisList {
  248. if _, ok := resultMap[userRecord.ID]; !ok {
  249. resultMap[userRecord.ID] = userRecord
  250. } else {
  251. resultMap[userRecord.ID].CodeMergeCount += userRecord.CodeMergeCount
  252. resultMap[userRecord.ID].CommitCount += userRecord.CommitCount
  253. resultMap[userRecord.ID].IssueCount += userRecord.IssueCount
  254. resultMap[userRecord.ID].CommentCount += userRecord.CommentCount
  255. resultMap[userRecord.ID].FocusRepoCount += userRecord.FocusRepoCount
  256. resultMap[userRecord.ID].StarRepoCount += userRecord.StarRepoCount
  257. resultMap[userRecord.ID].WatchedCount += userRecord.WatchedCount
  258. resultMap[userRecord.ID].CommitCodeSize += userRecord.CommitCodeSize
  259. resultMap[userRecord.ID].CommitDatasetSize += userRecord.CommitDatasetSize
  260. resultMap[userRecord.ID].CommitModelCount += userRecord.CommitModelCount
  261. resultMap[userRecord.ID].SolveIssueCount += userRecord.SolveIssueCount
  262. resultMap[userRecord.ID].EncyclopediasCount += userRecord.EncyclopediasCount
  263. resultMap[userRecord.ID].CreateRepoCount += userRecord.CreateRepoCount
  264. resultMap[userRecord.ID].LoginCount += userRecord.LoginCount
  265. }
  266. }
  267. }
  268. }
  269. userBusinessAnalysisReturnList := UserBusinessAnalysisList{}
  270. for _, v := range resultMap {
  271. userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, v)
  272. }
  273. sort.Sort(userBusinessAnalysisReturnList)
  274. log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
  275. return userBusinessAnalysisReturnList, count
  276. }
  277. func RefreshUserStaticAllTabel(wikiCountMap map[string]int, CommitCodeSizeMap map[string]*git.UserKPIStats) {
  278. sess := x.NewSession()
  279. defer sess.Close()
  280. statictisSess := xStatistic.NewSession()
  281. defer statictisSess.Close()
  282. log.Info("truncate all data from table: user_business_analysis_all")
  283. statictisSess.Exec("TRUNCATE TABLE user_business_analysis_all")
  284. currentTimeNow := time.Now()
  285. startTime := currentTimeNow.AddDate(0, 0, -1)
  286. pageStartTime := time.Date(2021, 11, 5, 0, 0, 0, 0, currentTimeNow.Location())
  287. log.Info("pageStartTime:" + pageStartTime.Format("2006-01-02 15:04:05"))
  288. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  289. log.Info("pageEndTime time:" + pageEndTime.Format("2006-01-02 15:04:05"))
  290. start_unix := pageStartTime.Unix()
  291. end_unix := pageEndTime.Unix()
  292. CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
  293. CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
  294. IssueCountMap := queryAction(start_unix, end_unix, 6)
  295. CommentCountMap := queryComment(start_unix, end_unix)
  296. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  297. StarRepoCountMap := queryStar(start_unix, end_unix)
  298. WatchedCountMap := queryFollow(start_unix, end_unix)
  299. CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
  300. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  301. CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
  302. LoginCountMap := queryLoginCount(start_unix, end_unix)
  303. OpenIIndexMap := queryUserRepoOpenIIndex(startTime.Unix(), end_unix)
  304. DataDate := currentTimeNow.Format("2006-01-02")
  305. cond := "type != 1 and is_active=true"
  306. count, err := sess.Where(cond).Count(new(User))
  307. if err != nil {
  308. log.Info("query user error. return.")
  309. return
  310. }
  311. var indexTotal int64
  312. indexTotal = 0
  313. for {
  314. sess.Select("`user`.*").Table("user").Where(cond).Limit(Page_SIZE, int(indexTotal))
  315. userList := make([]*User, 0)
  316. sess.Find(&userList)
  317. for i, userRecord := range userList {
  318. log.Info("insert all static, i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  319. var dateRecordAll UserBusinessAnalysisAll
  320. dateRecordAll.ID = userRecord.ID
  321. dateRecordAll.Email = userRecord.Email
  322. dateRecordAll.RegistDate = userRecord.CreatedUnix
  323. dateRecordAll.Name = userRecord.Name
  324. dateRecordAll.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  325. dateRecordAll.DataDate = DataDate
  326. if _, ok := CodeMergeCountMap[dateRecordAll.ID]; !ok {
  327. dateRecordAll.CodeMergeCount = 0
  328. } else {
  329. dateRecordAll.CodeMergeCount = CodeMergeCountMap[dateRecordAll.ID]
  330. }
  331. if _, ok := CommitCountMap[dateRecordAll.ID]; !ok {
  332. dateRecordAll.CommitCount = 0
  333. } else {
  334. dateRecordAll.CommitCount = CommitCountMap[dateRecordAll.ID]
  335. }
  336. if _, ok := IssueCountMap[dateRecordAll.ID]; !ok {
  337. dateRecordAll.IssueCount = 0
  338. } else {
  339. dateRecordAll.IssueCount = IssueCountMap[dateRecordAll.ID]
  340. }
  341. if _, ok := CommentCountMap[dateRecordAll.ID]; !ok {
  342. dateRecordAll.CommentCount = 0
  343. } else {
  344. dateRecordAll.CommentCount = CommentCountMap[dateRecordAll.ID]
  345. }
  346. if _, ok := FocusRepoCountMap[dateRecordAll.ID]; !ok {
  347. dateRecordAll.FocusRepoCount = 0
  348. } else {
  349. dateRecordAll.FocusRepoCount = FocusRepoCountMap[dateRecordAll.ID]
  350. }
  351. if _, ok := StarRepoCountMap[dateRecordAll.ID]; !ok {
  352. dateRecordAll.StarRepoCount = 0
  353. } else {
  354. dateRecordAll.StarRepoCount = StarRepoCountMap[dateRecordAll.ID]
  355. }
  356. if _, ok := WatchedCountMap[dateRecordAll.ID]; !ok {
  357. dateRecordAll.WatchedCount = 0
  358. } else {
  359. dateRecordAll.WatchedCount = WatchedCountMap[dateRecordAll.ID]
  360. }
  361. if _, ok := CommitCodeSizeMap[dateRecordAll.Email]; !ok {
  362. dateRecordAll.CommitCodeSize = 0
  363. } else {
  364. dateRecordAll.CommitCodeSize = int(CommitCodeSizeMap[dateRecordAll.Email].CommitLines)
  365. }
  366. if _, ok := CommitDatasetSizeMap[dateRecordAll.ID]; !ok {
  367. dateRecordAll.CommitDatasetSize = 0
  368. } else {
  369. dateRecordAll.CommitDatasetSize = CommitDatasetSizeMap[dateRecordAll.ID]
  370. }
  371. if _, ok := SolveIssueCountMap[dateRecordAll.ID]; !ok {
  372. dateRecordAll.SolveIssueCount = 0
  373. } else {
  374. dateRecordAll.SolveIssueCount = SolveIssueCountMap[dateRecordAll.ID]
  375. }
  376. if _, ok := wikiCountMap[dateRecordAll.Name]; !ok {
  377. dateRecordAll.EncyclopediasCount = 0
  378. } else {
  379. dateRecordAll.EncyclopediasCount = wikiCountMap[dateRecordAll.Name]
  380. }
  381. if _, ok := CreateRepoCountMap[dateRecordAll.ID]; !ok {
  382. dateRecordAll.CreateRepoCount = 0
  383. } else {
  384. dateRecordAll.CreateRepoCount = CreateRepoCountMap[dateRecordAll.ID]
  385. }
  386. if _, ok := LoginCountMap[dateRecordAll.ID]; !ok {
  387. dateRecordAll.LoginCount = 0
  388. } else {
  389. dateRecordAll.LoginCount = LoginCountMap[dateRecordAll.ID]
  390. }
  391. if _, ok := OpenIIndexMap[dateRecordAll.ID]; !ok {
  392. dateRecordAll.OpenIIndex = 0
  393. } else {
  394. dateRecordAll.OpenIIndex = OpenIIndexMap[dateRecordAll.ID]
  395. }
  396. dateRecordAll.CommitModelCount = 0
  397. _, err = statictisSess.Insert(&dateRecordAll)
  398. if err != nil {
  399. log.Info("insert all data failed." + err.Error())
  400. }
  401. }
  402. indexTotal += Page_SIZE
  403. if indexTotal >= count {
  404. break
  405. }
  406. }
  407. log.Info("refresh all data finished.")
  408. }
  409. func CounDataByDateAndReCount(wikiCountMap map[string]int, startTime time.Time, endTime time.Time, isReCount bool) error {
  410. log.Info("start to count other user info data")
  411. sess := x.NewSession()
  412. defer sess.Close()
  413. currentTimeNow := time.Now()
  414. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  415. start_unix := startTime.Unix()
  416. log.Info("DB query time:" + startTime.Format("2006-01-02 15:04:05"))
  417. end_unix := endTime.Unix()
  418. CountDate := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  419. if isReCount {
  420. CountDate = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  421. }
  422. DataDate := startTime.Format("2006-01-02")
  423. CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
  424. CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
  425. IssueCountMap := queryAction(start_unix, end_unix, 6)
  426. CommentCountMap := queryComment(start_unix, end_unix)
  427. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  428. StarRepoCountMap := queryStar(start_unix, end_unix)
  429. WatchedCountMap := queryFollow(start_unix, end_unix)
  430. CommitCodeSizeMap, err := GetAllUserKPIStats()
  431. if err != nil {
  432. log.Info("query commit code errr.")
  433. } else {
  434. log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
  435. }
  436. CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
  437. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  438. CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
  439. LoginCountMap := queryLoginCount(start_unix, end_unix)
  440. OpenIIndexMap := queryUserRepoOpenIIndex(start_unix, end_unix)
  441. statictisSess := xStatistic.NewSession()
  442. defer statictisSess.Close()
  443. cond := "type != 1 and is_active=true"
  444. count, err := sess.Where(cond).Count(new(User))
  445. if err != nil {
  446. log.Info("query user error. return.")
  447. return err
  448. }
  449. var indexTotal int64
  450. indexTotal = 0
  451. for {
  452. sess.Select("`user`.*").Table("user").Where(cond).Limit(Page_SIZE, int(indexTotal))
  453. userList := make([]*User, 0)
  454. sess.Find(&userList)
  455. for i, userRecord := range userList {
  456. var dateRecord UserBusinessAnalysis
  457. dateRecord.ID = userRecord.ID
  458. log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  459. dateRecord.CountDate = CountDate.Unix()
  460. statictisSess.Delete(&dateRecord)
  461. dateRecord.Email = userRecord.Email
  462. dateRecord.RegistDate = userRecord.CreatedUnix
  463. dateRecord.Name = userRecord.Name
  464. dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  465. dateRecord.DataDate = DataDate
  466. if _, ok := CodeMergeCountMap[dateRecord.ID]; !ok {
  467. dateRecord.CodeMergeCount = 0
  468. } else {
  469. dateRecord.CodeMergeCount = CodeMergeCountMap[dateRecord.ID]
  470. }
  471. if _, ok := CommitCountMap[dateRecord.ID]; !ok {
  472. dateRecord.CommitCount = 0
  473. } else {
  474. dateRecord.CommitCount = CommitCountMap[dateRecord.ID]
  475. }
  476. if _, ok := IssueCountMap[dateRecord.ID]; !ok {
  477. dateRecord.IssueCount = 0
  478. } else {
  479. dateRecord.IssueCount = IssueCountMap[dateRecord.ID]
  480. }
  481. if _, ok := CommentCountMap[dateRecord.ID]; !ok {
  482. dateRecord.CommentCount = 0
  483. } else {
  484. dateRecord.CommentCount = CommentCountMap[dateRecord.ID]
  485. }
  486. if _, ok := FocusRepoCountMap[dateRecord.ID]; !ok {
  487. dateRecord.FocusRepoCount = 0
  488. } else {
  489. dateRecord.FocusRepoCount = FocusRepoCountMap[dateRecord.ID]
  490. }
  491. if _, ok := StarRepoCountMap[dateRecord.ID]; !ok {
  492. dateRecord.StarRepoCount = 0
  493. } else {
  494. dateRecord.StarRepoCount = StarRepoCountMap[dateRecord.ID]
  495. }
  496. if _, ok := WatchedCountMap[dateRecord.ID]; !ok {
  497. dateRecord.WatchedCount = 0
  498. } else {
  499. dateRecord.WatchedCount = WatchedCountMap[dateRecord.ID]
  500. }
  501. if _, ok := CommitCodeSizeMap[dateRecord.Email]; !ok {
  502. dateRecord.CommitCodeSize = 0
  503. } else {
  504. dateRecord.CommitCodeSize = int(CommitCodeSizeMap[dateRecord.Email].CommitLines)
  505. }
  506. if _, ok := CommitDatasetSizeMap[dateRecord.ID]; !ok {
  507. dateRecord.CommitDatasetSize = 0
  508. } else {
  509. dateRecord.CommitDatasetSize = CommitDatasetSizeMap[dateRecord.ID]
  510. }
  511. if _, ok := SolveIssueCountMap[dateRecord.ID]; !ok {
  512. dateRecord.SolveIssueCount = 0
  513. } else {
  514. dateRecord.SolveIssueCount = SolveIssueCountMap[dateRecord.ID]
  515. }
  516. if _, ok := wikiCountMap[dateRecord.Name]; !ok {
  517. dateRecord.EncyclopediasCount = 0
  518. } else {
  519. dateRecord.EncyclopediasCount = wikiCountMap[dateRecord.Name]
  520. }
  521. if _, ok := CreateRepoCountMap[dateRecord.ID]; !ok {
  522. dateRecord.CreateRepoCount = 0
  523. } else {
  524. dateRecord.CreateRepoCount = CreateRepoCountMap[dateRecord.ID]
  525. }
  526. if _, ok := LoginCountMap[dateRecord.ID]; !ok {
  527. dateRecord.LoginCount = 0
  528. } else {
  529. dateRecord.LoginCount = LoginCountMap[dateRecord.ID]
  530. }
  531. if _, ok := OpenIIndexMap[dateRecord.ID]; !ok {
  532. dateRecord.OpenIIndex = 0
  533. } else {
  534. dateRecord.OpenIIndex = OpenIIndexMap[dateRecord.ID]
  535. }
  536. dateRecord.CommitModelCount = 0
  537. _, err = statictisSess.Insert(&dateRecord)
  538. if err != nil {
  539. log.Info("insert daterecord failed." + err.Error())
  540. return err
  541. }
  542. }
  543. indexTotal += Page_SIZE
  544. if indexTotal >= count {
  545. break
  546. }
  547. }
  548. RefreshUserStaticAllTabel(wikiCountMap, CommitCodeSizeMap)
  549. return nil
  550. }
  551. func getInt(str string) int {
  552. re, err := strconv.ParseInt(str, 10, 32)
  553. if err != nil {
  554. return 0
  555. }
  556. return int(re)
  557. }
  558. func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
  559. CounDataByDateAndReCount(wikiCountMap, startTime, endTime, false)
  560. }
  561. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  562. sess := x.NewSession()
  563. defer sess.Close()
  564. resultMap := make(map[int64]int)
  565. cond := "issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix)
  566. count, err := sess.Table("issue_assignees").Join("inner", "issue", "issue.id=issue_assignees.issue_id").Where(cond).Count(new(IssueAssignees))
  567. if err != nil {
  568. log.Info("query issue error. return.")
  569. return resultMap
  570. }
  571. var indexTotal int64
  572. indexTotal = 0
  573. for {
  574. issueAssigneesList := make([]*IssueAssignees, 0)
  575. sess.Select("issue_assignees.*").Table("issue_assignees").
  576. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  577. Where(cond).Limit(Page_SIZE, int(indexTotal))
  578. sess.Find(&issueAssigneesList)
  579. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  580. for _, issueAssigneesRecord := range issueAssigneesList {
  581. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  582. resultMap[issueAssigneesRecord.AssigneeID] = 1
  583. } else {
  584. resultMap[issueAssigneesRecord.AssigneeID] += 1
  585. }
  586. }
  587. indexTotal += Page_SIZE
  588. if indexTotal >= count {
  589. break
  590. }
  591. }
  592. return resultMap
  593. }
  594. func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
  595. sess := x.NewSession()
  596. defer sess.Close()
  597. resultMap := make(map[int64]int)
  598. cond := "pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix)
  599. count, err := sess.Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Count(new(Issue))
  600. if err != nil {
  601. log.Info("query issue error. return.")
  602. return resultMap
  603. }
  604. var indexTotal int64
  605. indexTotal = 0
  606. for {
  607. issueList := make([]*Issue, 0)
  608. sess.Select("issue.*").Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Limit(Page_SIZE, int(indexTotal))
  609. sess.Find(&issueList)
  610. log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
  611. for _, issueRecord := range issueList {
  612. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  613. resultMap[issueRecord.PosterID] = 1
  614. } else {
  615. resultMap[issueRecord.PosterID] += 1
  616. }
  617. }
  618. indexTotal += Page_SIZE
  619. if indexTotal >= count {
  620. break
  621. }
  622. }
  623. return resultMap
  624. }
  625. func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  626. sess := x.NewSession()
  627. defer sess.Close()
  628. resultMap := make(map[int64]int)
  629. cond := "user_id=act_user_id and op_type=" + fmt.Sprint(actionType) + " and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  630. count, err := sess.Where(cond).Count(new(Action))
  631. if err != nil {
  632. log.Info("query action error. return.")
  633. return resultMap
  634. }
  635. var indexTotal int64
  636. indexTotal = 0
  637. for {
  638. sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).Limit(Page_SIZE, int(indexTotal))
  639. actionList := make([]*Action, 0)
  640. sess.Find(&actionList)
  641. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  642. for _, actionRecord := range actionList {
  643. if _, ok := resultMap[actionRecord.UserID]; !ok {
  644. resultMap[actionRecord.UserID] = 1
  645. } else {
  646. resultMap[actionRecord.UserID] += 1
  647. }
  648. }
  649. indexTotal += Page_SIZE
  650. if indexTotal >= count {
  651. break
  652. }
  653. }
  654. return resultMap
  655. }
  656. func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  657. sess := x.NewSession()
  658. defer sess.Close()
  659. resultMap := make(map[int64]int)
  660. cond := "op_type=" + fmt.Sprint(actionType) + " and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  661. count, err := sess.Where(cond).Count(new(Action))
  662. if err != nil {
  663. log.Info("query Action error. return.")
  664. return resultMap
  665. }
  666. var indexTotal int64
  667. indexTotal = 0
  668. for {
  669. sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).Limit(Page_SIZE, int(indexTotal))
  670. actionList := make([]*Action, 0)
  671. sess.Find(&actionList)
  672. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  673. for _, actionRecord := range actionList {
  674. if _, ok := resultMap[actionRecord.UserID]; !ok {
  675. resultMap[actionRecord.UserID] = 1
  676. } else {
  677. resultMap[actionRecord.UserID] += 1
  678. }
  679. }
  680. indexTotal += Page_SIZE
  681. if indexTotal >= count {
  682. break
  683. }
  684. }
  685. return resultMap
  686. }
  687. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  688. sess := x.NewSession()
  689. defer sess.Close()
  690. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  691. resultMap := make(map[int64]int)
  692. count, err := sess.Where(cond).Count(new(Comment))
  693. if err != nil {
  694. log.Info("query Comment error. return.")
  695. return resultMap
  696. }
  697. var indexTotal int64
  698. indexTotal = 0
  699. for {
  700. sess.Select("id,type,poster_id").Table("comment").Where(cond).Limit(Page_SIZE, int(indexTotal))
  701. commentList := make([]*Comment, 0)
  702. sess.Find(&commentList)
  703. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  704. for _, commentRecord := range commentList {
  705. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  706. resultMap[commentRecord.PosterID] = 1
  707. } else {
  708. resultMap[commentRecord.PosterID] += 1
  709. }
  710. }
  711. indexTotal += Page_SIZE
  712. if indexTotal >= count {
  713. break
  714. }
  715. }
  716. return resultMap
  717. }
  718. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  719. sess := x.NewSession()
  720. defer sess.Close()
  721. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  722. resultMap := make(map[int64]int)
  723. count, err := sess.Where(cond).Count(new(Watch))
  724. if err != nil {
  725. log.Info("query issue error. return.")
  726. return resultMap
  727. }
  728. var indexTotal int64
  729. indexTotal = 0
  730. for {
  731. watchList := make([]*Watch, 0)
  732. sess.Select("id,user_id,repo_id").Table("watch").Where(cond).Limit(Page_SIZE, int(indexTotal))
  733. sess.Find(&watchList)
  734. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  735. for _, watchRecord := range watchList {
  736. if _, ok := resultMap[watchRecord.UserID]; !ok {
  737. resultMap[watchRecord.UserID] = 1
  738. } else {
  739. resultMap[watchRecord.UserID] += 1
  740. }
  741. }
  742. indexTotal += Page_SIZE
  743. if indexTotal >= count {
  744. break
  745. }
  746. }
  747. return resultMap
  748. }
  749. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  750. sess := x.NewSession()
  751. defer sess.Close()
  752. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  753. resultMap := make(map[int64]int)
  754. count, err := sess.Where(cond).Count(new(Star))
  755. if err != nil {
  756. log.Info("query star error. return.")
  757. return resultMap
  758. }
  759. var indexTotal int64
  760. indexTotal = 0
  761. for {
  762. sess.Select("id,uid,repo_id").Table("star").Where(cond).Limit(Page_SIZE, int(indexTotal))
  763. starList := make([]*Star, 0)
  764. sess.Find(&starList)
  765. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  766. for _, starRecord := range starList {
  767. if _, ok := resultMap[starRecord.UID]; !ok {
  768. resultMap[starRecord.UID] = 1
  769. } else {
  770. resultMap[starRecord.UID] += 1
  771. }
  772. }
  773. indexTotal += Page_SIZE
  774. if indexTotal >= count {
  775. break
  776. }
  777. }
  778. return resultMap
  779. }
  780. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  781. sess := x.NewSession()
  782. defer sess.Close()
  783. resultMap := make(map[int64]int)
  784. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  785. count, err := sess.Where(cond).Count(new(Follow))
  786. if err != nil {
  787. log.Info("query follow error. return.")
  788. return resultMap
  789. }
  790. var indexTotal int64
  791. indexTotal = 0
  792. for {
  793. sess.Select("id,user_id,follow_id").Table("follow").Where(cond).Limit(Page_SIZE, int(indexTotal))
  794. followList := make([]*Follow, 0)
  795. sess.Find(&followList)
  796. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  797. for _, followRecord := range followList {
  798. if _, ok := resultMap[followRecord.FollowID]; !ok {
  799. resultMap[followRecord.FollowID] = 1
  800. } else {
  801. resultMap[followRecord.FollowID] += 1
  802. }
  803. }
  804. indexTotal += Page_SIZE
  805. if indexTotal >= count {
  806. break
  807. }
  808. }
  809. return resultMap
  810. }
  811. func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int {
  812. sess := x.NewSession()
  813. defer sess.Close()
  814. resultMap := make(map[int64]int)
  815. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  816. count, err := sess.Where(cond).Count(new(Attachment))
  817. if err != nil {
  818. log.Info("query attachment error. return.")
  819. return resultMap
  820. }
  821. var indexTotal int64
  822. indexTotal = 0
  823. for {
  824. sess.Select("id,uploader_id,size").Table("attachment").Where(cond).Limit(Page_SIZE, int(indexTotal))
  825. attachmentList := make([]*Attachment, 0)
  826. sess.Find(&attachmentList)
  827. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  828. for _, attachRecord := range attachmentList {
  829. if _, ok := resultMap[attachRecord.UploaderID]; !ok {
  830. resultMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  831. } else {
  832. resultMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  833. }
  834. }
  835. indexTotal += Page_SIZE
  836. if indexTotal >= count {
  837. break
  838. }
  839. }
  840. return resultMap
  841. }
  842. func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
  843. sess := x.NewSession()
  844. defer sess.Close()
  845. resultMap := make(map[int64]int)
  846. cond := "is_fork=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  847. count, err := sess.Where(cond).Count(new(Repository))
  848. if err != nil {
  849. log.Info("query Repository error. return.")
  850. return resultMap
  851. }
  852. var indexTotal int64
  853. indexTotal = 0
  854. for {
  855. sess.Select("id,owner_id,name").Table("repository").Where(cond).Limit(Page_SIZE, int(indexTotal))
  856. repoList := make([]*Repository, 0)
  857. sess.Find(&repoList)
  858. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  859. for _, repoRecord := range repoList {
  860. if _, ok := resultMap[repoRecord.OwnerID]; !ok {
  861. resultMap[repoRecord.OwnerID] = 1
  862. } else {
  863. resultMap[repoRecord.OwnerID] += 1
  864. }
  865. }
  866. indexTotal += Page_SIZE
  867. if indexTotal >= count {
  868. break
  869. }
  870. }
  871. return resultMap
  872. }
  873. func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
  874. statictisSess := xStatistic.NewSession()
  875. defer statictisSess.Close()
  876. statictisSess.Select("id,repo_id,radar_total").Table("repo_statistic").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)).OrderBy("id desc")
  877. repoStatisticList := make([]*RepoStatistic, 0)
  878. statictisSess.Find(&repoStatisticList)
  879. repoOpenIIndexMap := make(map[int64]float64)
  880. log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
  881. for _, repoRecord := range repoStatisticList {
  882. if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
  883. repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
  884. }
  885. }
  886. sess := x.NewSession()
  887. defer sess.Close()
  888. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
  889. repoList := make([]*Repository, 0)
  890. sess.Find(&repoList)
  891. userMap := make(map[int64]float64)
  892. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  893. for _, repoRecord := range repoList {
  894. if _, ok := userMap[repoRecord.OwnerID]; !ok {
  895. if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
  896. userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
  897. }
  898. }
  899. }
  900. //query collaboration
  901. sess.Select("repo_id,user_id,mode").Table("collaboration")
  902. collaborationList := make([]*Collaboration, 0)
  903. sess.Find(&collaborationList)
  904. log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
  905. for _, collaborationRecord := range collaborationList {
  906. if _, ok := userMap[collaborationRecord.UserID]; !ok {
  907. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  908. userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
  909. }
  910. } else {
  911. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  912. userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
  913. }
  914. }
  915. }
  916. log.Info("user openi index size=" + fmt.Sprint(len(userMap)))
  917. return userMap
  918. }
  919. func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
  920. statictisSess := xStatistic.NewSession()
  921. defer statictisSess.Close()
  922. resultMap := make(map[int64]int)
  923. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  924. count, err := statictisSess.Where(cond).Count(new(UserLoginLog))
  925. if err != nil {
  926. log.Info("query UserLoginLog error. return.")
  927. return resultMap
  928. }
  929. var indexTotal int64
  930. indexTotal = 0
  931. for {
  932. statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).Limit(Page_SIZE, int(indexTotal))
  933. userLoginLogList := make([]*UserLoginLog, 0)
  934. statictisSess.Find(&userLoginLogList)
  935. log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
  936. for _, loginRecord := range userLoginLogList {
  937. if _, ok := resultMap[loginRecord.UId]; !ok {
  938. resultMap[loginRecord.UId] = 1
  939. } else {
  940. resultMap[loginRecord.UId] += 1
  941. }
  942. }
  943. indexTotal += Page_SIZE
  944. if indexTotal >= count {
  945. break
  946. }
  947. }
  948. log.Info("user login size=" + fmt.Sprint(len(resultMap)))
  949. return resultMap
  950. }
  951. func subMonth(t1, t2 time.Time) (month int) {
  952. y1 := t1.Year()
  953. y2 := t2.Year()
  954. m1 := int(t1.Month())
  955. m2 := int(t2.Month())
  956. d1 := t1.Day()
  957. d2 := t2.Day()
  958. yearInterval := y1 - y2
  959. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  960. if m1 < m2 || m1 == m2 && d1 < d2 {
  961. yearInterval--
  962. }
  963. // 获取月数差值
  964. monthInterval := (m1 + 12) - m2
  965. if d1 < d2 {
  966. monthInterval--
  967. }
  968. monthInterval %= 12
  969. month = yearInterval*12 + monthInterval
  970. if month == 0 {
  971. month = 1
  972. }
  973. return month
  974. }