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

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