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

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