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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. package models
  2. import (
  3. "fmt"
  4. "sort"
  5. "strconv"
  6. "time"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "xorm.io/builder"
  10. "xorm.io/xorm"
  11. )
  12. const (
  13. PAGE_SIZE = 2000
  14. BATCH_INSERT_SIZE = 50
  15. )
  16. type UserBusinessAnalysisAll struct {
  17. ID int64 `xorm:"pk"`
  18. CountDate int64 `xorm:"pk"`
  19. //action :ActionMergePullRequest // 11
  20. CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
  21. //action :ActionCommitRepo // 5
  22. CommitCount int `xorm:"NOT NULL DEFAULT 0"`
  23. //action :ActionCreateIssue // 10
  24. IssueCount int `xorm:"NOT NULL DEFAULT 0"`
  25. //comment table current date
  26. CommentCount int `xorm:"NOT NULL DEFAULT 0"`
  27. //watch table current date
  28. FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  29. //star table current date
  30. StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  31. //follow table
  32. WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
  33. // user table
  34. GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
  35. //
  36. CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
  37. //attachement table
  38. CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
  39. //0
  40. CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
  41. //issue, issueassignees
  42. SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
  43. //baike
  44. EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
  45. //user
  46. RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
  47. //repo
  48. CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  49. //login count, from elk
  50. LoginCount int `xorm:"NOT NULL DEFAULT 0"`
  51. //openi index
  52. OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  53. //user
  54. Email string `xorm:"NOT NULL"`
  55. //user
  56. Name string `xorm:"NOT NULL"`
  57. DataDate string `xorm:"NULL"`
  58. //cloudbraintask
  59. CloudBrainTaskNum int `xorm:"NOT NULL DEFAULT 0"`
  60. GpuDebugJob int `xorm:"NOT NULL DEFAULT 0"`
  61. NpuDebugJob int `xorm:"NOT NULL DEFAULT 0"`
  62. GpuTrainJob int `xorm:"NOT NULL DEFAULT 0"`
  63. NpuTrainJob int `xorm:"NOT NULL DEFAULT 0"`
  64. NpuInferenceJob int `xorm:"NOT NULL DEFAULT 0"`
  65. GpuBenchMarkJob int `xorm:"NOT NULL DEFAULT 0"`
  66. CloudBrainRunTime int `xorm:"NOT NULL DEFAULT 0"`
  67. CommitDatasetNum int `xorm:"NOT NULL DEFAULT 0"`
  68. UserIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  69. UserLocation string `xorm:"NULL"`
  70. }
  71. type UserBusinessAnalysis struct {
  72. ID int64 `xorm:"pk"`
  73. CountDate int64 `xorm:"pk"`
  74. //action :ActionMergePullRequest // 11
  75. CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
  76. //action :ActionCommitRepo // 5
  77. CommitCount int `xorm:"NOT NULL DEFAULT 0"`
  78. //action :ActionCreateIssue // 6
  79. IssueCount int `xorm:"NOT NULL DEFAULT 0"`
  80. //comment table current date
  81. CommentCount int `xorm:"NOT NULL DEFAULT 0"`
  82. //watch table current date
  83. FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  84. //star table current date
  85. StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  86. //follow table
  87. WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
  88. // user table
  89. GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
  90. //
  91. CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
  92. //attachement table
  93. CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
  94. //0
  95. CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
  96. //issue, issueassignees
  97. SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
  98. //baike
  99. EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
  100. //user
  101. RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
  102. //repo
  103. CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
  104. //login count, from elk
  105. LoginCount int `xorm:"NOT NULL DEFAULT 0"`
  106. //openi index
  107. OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  108. //user
  109. Email string `xorm:"NOT NULL"`
  110. //user
  111. Name string `xorm:"NOT NULL"`
  112. DataDate string `xorm:"NULL"`
  113. CloudBrainTaskNum int `xorm:"NOT NULL DEFAULT 0"`
  114. GpuDebugJob int `xorm:"NOT NULL DEFAULT 0"`
  115. NpuDebugJob int `xorm:"NOT NULL DEFAULT 0"`
  116. GpuTrainJob int `xorm:"NOT NULL DEFAULT 0"`
  117. NpuTrainJob int `xorm:"NOT NULL DEFAULT 0"`
  118. NpuInferenceJob int `xorm:"NOT NULL DEFAULT 0"`
  119. GpuBenchMarkJob int `xorm:"NOT NULL DEFAULT 0"`
  120. CloudBrainRunTime int `xorm:"NOT NULL DEFAULT 0"`
  121. CommitDatasetNum int `xorm:"NOT NULL DEFAULT 0"`
  122. UserIndex float64 `xorm:"NOT NULL DEFAULT 0"`
  123. UserLocation string `xorm:"NULL"`
  124. }
  125. type UserBusinessAnalysisQueryOptions struct {
  126. ListOptions
  127. UserName string
  128. SortType string
  129. StartTime int64
  130. EndTime int64
  131. IsAll bool
  132. }
  133. type UserBusinessAnalysisList []*UserBusinessAnalysis
  134. func (ulist UserBusinessAnalysisList) Swap(i, j int) { ulist[i], ulist[j] = ulist[j], ulist[i] }
  135. func (ulist UserBusinessAnalysisList) Len() int { return len(ulist) }
  136. func (ulist UserBusinessAnalysisList) Less(i, j int) bool {
  137. return ulist[i].ID > ulist[j].ID
  138. }
  139. func getLastCountDate() int64 {
  140. statictisSess := xStatistic.NewSession()
  141. defer statictisSess.Close()
  142. statictisSess.Limit(1, 0)
  143. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  144. if err := statictisSess.Table("user_business_analysis").OrderBy("count_date desc").Limit(1, 0).
  145. Find(&userBusinessAnalysisList); err == nil {
  146. for _, userRecord := range userBusinessAnalysisList {
  147. return userRecord.CountDate - 10000
  148. }
  149. } else {
  150. log.Info("query error." + err.Error())
  151. }
  152. currentTimeNow := time.Now()
  153. pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  154. return pageStartTime.Unix()
  155. }
  156. func QueryMetrics(start int64, end int64) ([]*UserMetrics, int64) {
  157. statictisSess := xStatistic.NewSession()
  158. defer statictisSess.Close()
  159. userMetricsList := make([]*UserMetrics, 0)
  160. if err := statictisSess.Table(new(UserMetrics)).Where("count_date >" + fmt.Sprint(start) + " and count_date<" + fmt.Sprint(end)).OrderBy("count_date desc").
  161. Find(&userMetricsList); err != nil {
  162. return nil, 0
  163. }
  164. return userMetricsList, int64(len(userMetricsList))
  165. }
  166. func QueryRankList(key string, tableName string, limit int) ([]*UserBusinessAnalysisAll, int64) {
  167. statictisSess := xStatistic.NewSession()
  168. defer statictisSess.Close()
  169. userBusinessAnalysisAllList := make([]*UserBusinessAnalysisAll, 0)
  170. if err := statictisSess.Table(tableName).OrderBy(key+" desc,id desc").Limit(limit, 0).
  171. Find(&userBusinessAnalysisAllList); err != nil {
  172. return nil, 0
  173. }
  174. return userBusinessAnalysisAllList, int64(len(userBusinessAnalysisAllList))
  175. }
  176. func QueryUserStaticDataByTableName(start int, pageSize int, tableName string, queryObj interface{}, userName string) ([]*UserBusinessAnalysisAll, int64) {
  177. statictisSess := xStatistic.NewSession()
  178. defer statictisSess.Close()
  179. var cond = builder.NewCond()
  180. if len(userName) > 0 {
  181. cond = cond.And(
  182. builder.Like{"name", userName},
  183. )
  184. }
  185. allCount, err := statictisSess.Where(cond).Count(queryObj)
  186. if err != nil {
  187. log.Info("query error." + err.Error())
  188. return nil, 0
  189. }
  190. log.Info("query return total:" + fmt.Sprint(allCount))
  191. userBusinessAnalysisAllList := make([]*UserBusinessAnalysisAll, 0)
  192. if err := statictisSess.Table(tableName).Where(cond).OrderBy("user_index desc,id desc").Limit(pageSize, start).
  193. Find(&userBusinessAnalysisAllList); err != nil {
  194. return nil, 0
  195. }
  196. return userBusinessAnalysisAllList, allCount
  197. }
  198. func QueryUserStaticDataAll(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysisAll, int64) {
  199. log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll))
  200. statictisSess := xStatistic.NewSession()
  201. defer statictisSess.Close()
  202. allCount, err := statictisSess.Count(new(UserBusinessAnalysisAll))
  203. if err != nil {
  204. log.Info("query error." + err.Error())
  205. return nil, 0
  206. }
  207. log.Info("query return total:" + fmt.Sprint(allCount))
  208. pageSize := PAGE_SIZE
  209. totalPage := int(allCount) / pageSize
  210. userBusinessAnalysisReturnList := make([]*UserBusinessAnalysisAll, 0)
  211. for i := 0; i <= int(totalPage); i++ {
  212. userBusinessAnalysisAllList := make([]*UserBusinessAnalysisAll, 0)
  213. if err := statictisSess.Table("user_business_analysis_all").OrderBy("id desc").Limit(pageSize, i*pageSize).
  214. Find(&userBusinessAnalysisAllList); err != nil {
  215. return nil, 0
  216. }
  217. log.Info("query " + fmt.Sprint(i+1) + " result size=" + fmt.Sprint(len(userBusinessAnalysisAllList)))
  218. for _, userRecord := range userBusinessAnalysisAllList {
  219. userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, userRecord)
  220. }
  221. }
  222. log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
  223. return userBusinessAnalysisReturnList, allCount
  224. }
  225. func QueryUserStaticDataPage(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysis, int64) {
  226. log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll))
  227. statictisSess := xStatistic.NewSession()
  228. defer statictisSess.Close()
  229. currentTimeNow := time.Now()
  230. pageStartTime := getLastCountDate()
  231. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()).Unix()
  232. var cond = builder.NewCond()
  233. if len(opts.UserName) > 0 {
  234. cond = cond.And(
  235. builder.Like{"name", opts.UserName},
  236. )
  237. }
  238. cond = cond.And(
  239. builder.Gte{"count_date": pageStartTime},
  240. )
  241. cond = cond.And(
  242. builder.Lte{"count_date": pageEndTime},
  243. )
  244. count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
  245. if err != nil {
  246. log.Info("query error." + err.Error())
  247. return nil, 0
  248. }
  249. if opts.Page >= 0 && opts.PageSize > 0 {
  250. var start int
  251. if opts.Page == 0 {
  252. start = 0
  253. } else {
  254. start = (opts.Page - 1) * opts.PageSize
  255. }
  256. statictisSess.Limit(opts.PageSize, start)
  257. }
  258. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  259. if err := statictisSess.Table("user_business_analysis").Where(cond).OrderBy("id desc").
  260. Find(&userBusinessAnalysisList); err != nil {
  261. return nil, 0
  262. }
  263. resultMap := make(map[int64]*UserBusinessAnalysis)
  264. if len(userBusinessAnalysisList) > 0 {
  265. var newAndCond = builder.NewCond()
  266. var newOrCond = builder.NewCond()
  267. for _, userRecord := range userBusinessAnalysisList {
  268. newOrCond = newOrCond.Or(
  269. builder.Eq{"id": userRecord.ID},
  270. )
  271. }
  272. newAndCond = newAndCond.And(
  273. newOrCond,
  274. )
  275. if !opts.IsAll {
  276. newAndCond = newAndCond.And(
  277. builder.Gte{"count_date": opts.StartTime},
  278. )
  279. newAndCond = newAndCond.And(
  280. builder.Lte{"count_date": opts.EndTime},
  281. )
  282. }
  283. allCount, err := statictisSess.Where(newAndCond).Count(new(UserBusinessAnalysis))
  284. if err != nil {
  285. log.Info("query error." + err.Error())
  286. return nil, 0
  287. }
  288. pageSize := 1000
  289. totalPage := int(allCount) / pageSize
  290. for i := 0; i <= int(totalPage); i++ {
  291. userBusinessAnalysisList = make([]*UserBusinessAnalysis, 0)
  292. if err := statictisSess.Table("user_business_analysis").Where(newAndCond).OrderBy("count_date desc").Limit(pageSize, i*pageSize).
  293. Find(&userBusinessAnalysisList); err != nil {
  294. return nil, 0
  295. }
  296. log.Info("query result size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  297. for _, userRecord := range userBusinessAnalysisList {
  298. if _, ok := resultMap[userRecord.ID]; !ok {
  299. resultMap[userRecord.ID] = userRecord
  300. } else {
  301. resultMap[userRecord.ID].CodeMergeCount += userRecord.CodeMergeCount
  302. resultMap[userRecord.ID].CommitCount += userRecord.CommitCount
  303. resultMap[userRecord.ID].IssueCount += userRecord.IssueCount
  304. resultMap[userRecord.ID].CommentCount += userRecord.CommentCount
  305. resultMap[userRecord.ID].FocusRepoCount += userRecord.FocusRepoCount
  306. resultMap[userRecord.ID].StarRepoCount += userRecord.StarRepoCount
  307. resultMap[userRecord.ID].WatchedCount += userRecord.WatchedCount
  308. resultMap[userRecord.ID].CommitCodeSize += userRecord.CommitCodeSize
  309. resultMap[userRecord.ID].CommitDatasetSize += userRecord.CommitDatasetSize
  310. resultMap[userRecord.ID].CommitDatasetNum += userRecord.CommitDatasetNum
  311. resultMap[userRecord.ID].CommitModelCount += userRecord.CommitModelCount
  312. resultMap[userRecord.ID].SolveIssueCount += userRecord.SolveIssueCount
  313. resultMap[userRecord.ID].EncyclopediasCount += userRecord.EncyclopediasCount
  314. resultMap[userRecord.ID].CreateRepoCount += userRecord.CreateRepoCount
  315. resultMap[userRecord.ID].LoginCount += userRecord.LoginCount
  316. }
  317. }
  318. }
  319. }
  320. userBusinessAnalysisReturnList := UserBusinessAnalysisList{}
  321. for _, v := range resultMap {
  322. userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, v)
  323. }
  324. sort.Sort(userBusinessAnalysisReturnList)
  325. log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
  326. return userBusinessAnalysisReturnList, count
  327. }
  328. func refreshUserStaticTable(wikiCountMap map[string]int, tableName string, pageStartTime time.Time, pageEndTime time.Time, userMetrics map[string]int) {
  329. sess := x.NewSession()
  330. defer sess.Close()
  331. statictisSess := xStatistic.NewSession()
  332. defer statictisSess.Close()
  333. log.Info("truncate all data from table: " + tableName)
  334. statictisSess.Exec("TRUNCATE TABLE " + tableName)
  335. log.Info("pageStartTime:" + pageStartTime.Format("2006-01-02 15:04:05"))
  336. log.Info("pageEndTime time:" + pageEndTime.Format("2006-01-02 15:04:05"))
  337. start_unix := pageStartTime.Unix()
  338. end_unix := pageEndTime.Unix()
  339. currentTimeNow := time.Now()
  340. startTime := currentTimeNow.AddDate(0, 0, -1)
  341. CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
  342. CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
  343. IssueCountMap := queryCreateIssue(start_unix, end_unix)
  344. CommentCountMap := queryComment(start_unix, end_unix)
  345. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  346. StarRepoCountMap := queryStar(start_unix, end_unix)
  347. WatchedCountMap := queryFollow(start_unix, end_unix)
  348. CommitCodeSizeMap := queryCommitCodeSize(start_unix, end_unix)
  349. CommitDatasetSizeMap, CommitDatasetNumMap := queryDatasetSize(start_unix, end_unix)
  350. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  351. CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
  352. LoginCountMap := queryLoginCount(start_unix, end_unix)
  353. OpenIIndexMap := queryUserRepoOpenIIndex(startTime.Unix(), end_unix)
  354. CloudBrainTaskMap, CloudBrainTaskItemMap := queryCloudBrainTask(start_unix, end_unix)
  355. AiModelManageMap := queryUserModel(start_unix, end_unix)
  356. DataDate := currentTimeNow.Format("2006-01-02") + " 00:01"
  357. cond := "type != 1 and is_active=true"
  358. count, err := sess.Where(cond).Count(new(User))
  359. if err != nil {
  360. log.Info("query user error. return.")
  361. return
  362. }
  363. ParaWeight := getParaWeight()
  364. var indexTotal int64
  365. indexTotal = 0
  366. insertCount := 0
  367. userIndexMap := make(map[int64]float64, 0)
  368. maxUserIndex := 0.0
  369. minUserIndex := 100000000.0
  370. dateRecordBatch := make([]UserBusinessAnalysisAll, 0)
  371. for {
  372. sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  373. userList := make([]*User, 0)
  374. sess.Find(&userList)
  375. for _, userRecord := range userList {
  376. var dateRecordAll UserBusinessAnalysisAll
  377. dateRecordAll.ID = userRecord.ID
  378. dateRecordAll.Email = userRecord.Email
  379. dateRecordAll.RegistDate = userRecord.CreatedUnix
  380. dateRecordAll.Name = userRecord.Name
  381. dateRecordAll.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  382. dateRecordAll.DataDate = DataDate
  383. dateRecordAll.UserLocation = userRecord.Location
  384. dateRecordAll.CodeMergeCount = getMapValue(dateRecordAll.ID, CodeMergeCountMap)
  385. dateRecordAll.CommitCount = getMapValue(dateRecordAll.ID, CommitCountMap)
  386. dateRecordAll.IssueCount = getMapValue(dateRecordAll.ID, IssueCountMap)
  387. dateRecordAll.CommentCount = getMapValue(dateRecordAll.ID, CommentCountMap)
  388. dateRecordAll.FocusRepoCount = getMapValue(dateRecordAll.ID, FocusRepoCountMap)
  389. dateRecordAll.StarRepoCount = getMapValue(dateRecordAll.ID, StarRepoCountMap)
  390. dateRecordAll.WatchedCount = getMapValue(dateRecordAll.ID, WatchedCountMap)
  391. dateRecordAll.CommitCodeSize = getMapValue(dateRecordAll.ID, CommitCodeSizeMap)
  392. dateRecordAll.CommitDatasetSize = getMapValue(dateRecordAll.ID, CommitDatasetSizeMap)
  393. dateRecordAll.CommitDatasetNum = getMapValue(dateRecordAll.ID, CommitDatasetNumMap)
  394. dateRecordAll.SolveIssueCount = getMapValue(dateRecordAll.ID, SolveIssueCountMap)
  395. dateRecordAll.EncyclopediasCount = getMapKeyStringValue(dateRecordAll.Name, wikiCountMap)
  396. dateRecordAll.CreateRepoCount = getMapValue(dateRecordAll.ID, CreateRepoCountMap)
  397. dateRecordAll.LoginCount = getMapValue(dateRecordAll.ID, LoginCountMap)
  398. if _, ok := OpenIIndexMap[dateRecordAll.ID]; !ok {
  399. dateRecordAll.OpenIIndex = 0
  400. } else {
  401. dateRecordAll.OpenIIndex = OpenIIndexMap[dateRecordAll.ID]
  402. }
  403. dateRecordAll.CloudBrainTaskNum = getMapValue(dateRecordAll.ID, CloudBrainTaskMap)
  404. dateRecordAll.GpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_GpuDebugJob", CloudBrainTaskItemMap)
  405. dateRecordAll.NpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_NpuDebugJob", CloudBrainTaskItemMap)
  406. dateRecordAll.GpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_GpuTrainJob", CloudBrainTaskItemMap)
  407. dateRecordAll.NpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_NpuTrainJob", CloudBrainTaskItemMap)
  408. dateRecordAll.NpuInferenceJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_NpuInferenceJob", CloudBrainTaskItemMap)
  409. dateRecordAll.GpuBenchMarkJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_GpuBenchMarkJob", CloudBrainTaskItemMap)
  410. dateRecordAll.CommitModelCount = getMapValue(dateRecordAll.ID, AiModelManageMap)
  411. dateRecordAll.UserIndex = getUserIndexFromAnalysisAll(dateRecordAll, ParaWeight)
  412. userIndexMap[dateRecordAll.ID] = dateRecordAll.UserIndex
  413. if maxUserIndex < dateRecordAll.UserIndex {
  414. maxUserIndex = dateRecordAll.UserIndex
  415. }
  416. if minUserIndex > dateRecordAll.UserIndex {
  417. minUserIndex = dateRecordAll.UserIndex
  418. }
  419. dateRecordBatch = append(dateRecordBatch, dateRecordAll)
  420. if len(dateRecordBatch) >= BATCH_INSERT_SIZE {
  421. insertTable(dateRecordBatch, tableName, statictisSess)
  422. insertCount += BATCH_INSERT_SIZE
  423. if err != nil {
  424. log.Info("insert all data failed." + err.Error())
  425. }
  426. dateRecordBatch = make([]UserBusinessAnalysisAll, 0)
  427. }
  428. if tableName == "user_business_analysis_all" {
  429. if dateRecordAll.UserIndex > 0 || dateRecordAll.LoginCount > 0 {
  430. userMetrics["TotalHasActivityUser"] = getMapKeyStringValue("TotalHasActivityUser", userMetrics) + 1
  431. }
  432. }
  433. }
  434. indexTotal += PAGE_SIZE
  435. if indexTotal >= count {
  436. break
  437. }
  438. }
  439. if len(dateRecordBatch) > 0 {
  440. insertTable(dateRecordBatch, tableName, statictisSess)
  441. insertCount += len(dateRecordBatch)
  442. if err != nil {
  443. log.Info("insert all data failed." + err.Error())
  444. }
  445. }
  446. //normalization
  447. for k, v := range userIndexMap {
  448. tmpResult := (v - minUserIndex) / (maxUserIndex - minUserIndex)
  449. if tmpResult > 0.99 {
  450. tmpResult = 0.99
  451. }
  452. updateUserIndex(tableName, statictisSess, k, tmpResult)
  453. }
  454. log.Info("refresh data finished.tableName=" + tableName + " total record:" + fmt.Sprint(insertCount))
  455. }
  456. func updateUserIndex(tableName string, statictisSess *xorm.Session, userId int64, userIndex float64) {
  457. updateSql := "UPDATE public." + tableName + " set user_index=" + fmt.Sprint(userIndex*100) + " where id=" + fmt.Sprint(userId)
  458. statictisSess.Exec(updateSql)
  459. }
  460. func insertTable(dateRecords []UserBusinessAnalysisAll, tableName string, statictisSess *xorm.Session) {
  461. insertBatchSql := "INSERT INTO public." + tableName +
  462. "(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, " +
  463. "commit_model_count, solve_issue_count, encyclopedias_count, regist_date, create_repo_count, login_count, open_i_index, email, name, data_date,cloud_brain_task_num,gpu_debug_job,npu_debug_job,gpu_train_job,npu_train_job,npu_inference_job,gpu_bench_mark_job,cloud_brain_run_time,commit_dataset_num,user_index,user_location) " +
  464. "VALUES"
  465. for i, record := range dateRecords {
  466. insertBatchSql += "(" + fmt.Sprint(record.ID) + ", " + fmt.Sprint(record.CountDate) + ", " + fmt.Sprint(record.CodeMergeCount) + ", " + fmt.Sprint(record.CommitCount) +
  467. ", " + fmt.Sprint(record.IssueCount) + ", " + fmt.Sprint(record.CommentCount) + ", " + fmt.Sprint(record.FocusRepoCount) + ", " + fmt.Sprint(record.StarRepoCount) +
  468. ", " + fmt.Sprint(record.WatchedCount) + ", " + fmt.Sprint(record.GiteaAgeMonth) + ", " + fmt.Sprint(record.CommitCodeSize) + ", " + fmt.Sprint(record.CommitDatasetSize) +
  469. ", " + fmt.Sprint(record.CommitModelCount) + ", " + fmt.Sprint(record.SolveIssueCount) + ", " + fmt.Sprint(record.EncyclopediasCount) + ", " + fmt.Sprint(record.RegistDate) +
  470. ", " + fmt.Sprint(record.CreateRepoCount) + ", " + fmt.Sprint(record.LoginCount) + ", " + fmt.Sprint(record.OpenIIndex) + ", '" + record.Email + "', '" + record.Name + "', '" + record.DataDate + "'," + fmt.Sprint(record.CloudBrainTaskNum) + "," + fmt.Sprint(record.GpuDebugJob) + "," + fmt.Sprint(record.NpuDebugJob) + "," + fmt.Sprint(record.GpuTrainJob) + "," + fmt.Sprint(record.NpuTrainJob) + "," + fmt.Sprint(record.NpuInferenceJob) + "," + fmt.Sprint(record.GpuBenchMarkJob) + "," + fmt.Sprint(record.CloudBrainRunTime) + "," + fmt.Sprint(record.CommitDatasetNum) + "," + fmt.Sprint(record.UserIndex) + ",'" + record.UserLocation + "')"
  471. if i < (len(dateRecords) - 1) {
  472. insertBatchSql += ","
  473. }
  474. }
  475. statictisSess.Exec(insertBatchSql)
  476. }
  477. func RefreshUserStaticAllTabel(wikiCountMap map[string]int, userMetrics map[string]int) {
  478. currentTimeNow := time.Now()
  479. pageStartTime := time.Date(2021, 11, 5, 0, 0, 0, 0, currentTimeNow.Location())
  480. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  481. refreshUserStaticTable(wikiCountMap, "user_business_analysis_all", pageStartTime, pageEndTime, userMetrics)
  482. log.Info("refresh all data finished.")
  483. pageStartTime = time.Date(currentTimeNow.Year(), 1, 1, 0, 0, 0, 0, currentTimeNow.Location())
  484. refreshUserStaticTable(wikiCountMap, "user_business_analysis_current_year", pageStartTime, pageEndTime, userMetrics)
  485. thisMonth := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
  486. refreshUserStaticTable(wikiCountMap, "user_business_analysis_current_month", thisMonth, pageEndTime, userMetrics)
  487. offset := int(time.Monday - currentTimeNow.Weekday())
  488. if offset > 0 {
  489. offset = -6
  490. }
  491. pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  492. refreshUserStaticTable(wikiCountMap, "user_business_analysis_current_week", pageStartTime, pageEndTime, userMetrics)
  493. pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -30)
  494. refreshUserStaticTable(wikiCountMap, "user_business_analysis_last30_day", pageStartTime, pageEndTime, userMetrics)
  495. pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  496. pageEndTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
  497. refreshUserStaticTable(wikiCountMap, "user_business_analysis_yesterday", pageStartTime, pageEndTime, userMetrics)
  498. pageStartTime = thisMonth.AddDate(0, -1, 0)
  499. pageEndTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
  500. refreshUserStaticTable(wikiCountMap, "user_business_analysis_last_month", pageStartTime, pageEndTime, userMetrics)
  501. }
  502. func CounDataByDateAndReCount(wikiCountMap map[string]int, startTime time.Time, endTime time.Time, isReCount bool) error {
  503. log.Info("start to count other user info data")
  504. sess := x.NewSession()
  505. defer sess.Close()
  506. currentTimeNow := time.Now()
  507. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  508. start_unix := startTime.Unix()
  509. log.Info("DB query time:" + startTime.Format("2006-01-02 15:04:05"))
  510. end_unix := endTime.Unix()
  511. CountDate := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  512. if isReCount {
  513. CountDate = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  514. }
  515. DataDate := startTime.Format("2006-01-02")
  516. CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
  517. CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
  518. IssueCountMap := queryCreateIssue(start_unix, end_unix)
  519. CommentCountMap := queryComment(start_unix, end_unix)
  520. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  521. StarRepoCountMap := queryStar(start_unix, end_unix)
  522. WatchedCountMap := queryFollow(start_unix, end_unix)
  523. CommitCodeSizeMap, err := GetAllUserKPIStats()
  524. if err != nil {
  525. log.Info("query commit code errr.")
  526. } else {
  527. log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
  528. }
  529. CommitDatasetSizeMap, CommitDatasetNumMap := queryDatasetSize(start_unix, end_unix)
  530. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  531. CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
  532. LoginCountMap := queryLoginCount(start_unix, end_unix)
  533. OpenIIndexMap := queryUserRepoOpenIIndex(start_unix, end_unix)
  534. CloudBrainTaskMap, CloudBrainTaskItemMap := queryCloudBrainTask(start_unix, end_unix)
  535. AiModelManageMap := queryUserModel(start_unix, end_unix)
  536. statictisSess := xStatistic.NewSession()
  537. defer statictisSess.Close()
  538. cond := "type != 1 and is_active=true"
  539. count, err := sess.Where(cond).Count(new(User))
  540. if err != nil {
  541. log.Info("query user error. return.")
  542. return err
  543. }
  544. ParaWeight := getParaWeight()
  545. userMetrics := make(map[string]int)
  546. var indexTotal int64
  547. indexTotal = 0
  548. for {
  549. sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  550. userList := make([]*User, 0)
  551. sess.Find(&userList)
  552. for i, userRecord := range userList {
  553. var dateRecord UserBusinessAnalysis
  554. dateRecord.ID = userRecord.ID
  555. log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  556. dateRecord.CountDate = CountDate.Unix()
  557. statictisSess.Delete(&dateRecord)
  558. dateRecord.Email = userRecord.Email
  559. dateRecord.RegistDate = userRecord.CreatedUnix
  560. dateRecord.Name = userRecord.Name
  561. dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  562. dateRecord.DataDate = DataDate
  563. dateRecord.CodeMergeCount = getMapValue(dateRecord.ID, CodeMergeCountMap)
  564. dateRecord.CommitCount = getMapValue(dateRecord.ID, CommitCountMap)
  565. dateRecord.IssueCount = getMapValue(dateRecord.ID, IssueCountMap)
  566. dateRecord.CommentCount = getMapValue(dateRecord.ID, CommentCountMap)
  567. dateRecord.FocusRepoCount = getMapValue(dateRecord.ID, FocusRepoCountMap)
  568. dateRecord.StarRepoCount = getMapValue(dateRecord.ID, StarRepoCountMap)
  569. dateRecord.WatchedCount = getMapValue(dateRecord.ID, WatchedCountMap)
  570. if _, ok := CommitCodeSizeMap[dateRecord.Email]; !ok {
  571. dateRecord.CommitCodeSize = 0
  572. } else {
  573. dateRecord.CommitCodeSize = int(CommitCodeSizeMap[dateRecord.Email].CommitLines)
  574. }
  575. dateRecord.CommitDatasetSize = getMapValue(dateRecord.ID, CommitDatasetSizeMap)
  576. dateRecord.CommitDatasetNum = getMapValue(dateRecord.ID, CommitDatasetNumMap)
  577. dateRecord.SolveIssueCount = getMapValue(dateRecord.ID, SolveIssueCountMap)
  578. dateRecord.EncyclopediasCount = getMapKeyStringValue(dateRecord.Name, wikiCountMap)
  579. dateRecord.CreateRepoCount = getMapValue(dateRecord.ID, CreateRepoCountMap)
  580. dateRecord.LoginCount = getMapValue(dateRecord.ID, LoginCountMap)
  581. if _, ok := OpenIIndexMap[dateRecord.ID]; !ok {
  582. dateRecord.OpenIIndex = 0
  583. } else {
  584. dateRecord.OpenIIndex = OpenIIndexMap[dateRecord.ID]
  585. }
  586. dateRecord.CloudBrainTaskNum = getMapValue(dateRecord.ID, CloudBrainTaskMap)
  587. dateRecord.GpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_GpuDebugJob", CloudBrainTaskItemMap)
  588. dateRecord.NpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_NpuDebugJob", CloudBrainTaskItemMap)
  589. dateRecord.GpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_GpuTrainJob", CloudBrainTaskItemMap)
  590. dateRecord.NpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_NpuTrainJob", CloudBrainTaskItemMap)
  591. dateRecord.NpuInferenceJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_NpuInferenceJob", CloudBrainTaskItemMap)
  592. dateRecord.GpuBenchMarkJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_GpuBenchMarkJob", CloudBrainTaskItemMap)
  593. dateRecord.CloudBrainRunTime = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_CloudBrainRunTime", CloudBrainTaskItemMap)
  594. dateRecord.CommitModelCount = getMapValue(dateRecord.ID, AiModelManageMap)
  595. dateRecord.UserIndex = getUserIndex(dateRecord, ParaWeight)
  596. setUserMetrics(userMetrics, userRecord, start_unix, end_unix, dateRecord)
  597. _, err = statictisSess.Insert(&dateRecord)
  598. if err != nil {
  599. log.Info("insert daterecord failed." + err.Error())
  600. return err
  601. }
  602. }
  603. indexTotal += PAGE_SIZE
  604. if indexTotal >= count {
  605. break
  606. }
  607. }
  608. RefreshUserStaticAllTabel(wikiCountMap, userMetrics)
  609. //insert userMetrics table
  610. var useMetrics UserMetrics
  611. useMetrics.CountDate = CountDate.Unix()
  612. statictisSess.Delete(&useMetrics)
  613. useMetrics.ActivateRegistUser = getMapKeyStringValue("ActivateRegistUser", userMetrics)
  614. useMetrics.HasActivityUser = getMapKeyStringValue("HasActivityUser", userMetrics)
  615. useMetrics.NotActivateRegistUser = getMapKeyStringValue("NotActivateRegistUser", userMetrics)
  616. useMetrics.TotalActivateRegistUser = getMapKeyStringValue("TotalActivateRegistUser", userMetrics)
  617. useMetrics.TotalHasActivityUser = getMapKeyStringValue("TotalHasActivityUser", userMetrics)
  618. statictisSess.Insert(&useMetrics)
  619. return nil
  620. }
  621. func setUserMetrics(userMetrics map[string]int, user *User, start_time int64, end_time int64, dateRecord UserBusinessAnalysis) {
  622. //ActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  623. //NotActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  624. //HasActivityUser int `xorm:"NOT NULL DEFAULT 0"`
  625. //TotalActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  626. //TotalHasActivityUser
  627. regist_time := user.CreatedUnix.AsTime().Unix()
  628. if regist_time >= start_time && regist_time <= end_time {
  629. if user.IsActive {
  630. userMetrics["ActivateRegistUser"] = getMapKeyStringValue("ActivateRegistUser", userMetrics) + 1
  631. } else {
  632. userMetrics["NotActivateRegistUser"] = getMapKeyStringValue("NotActivateRegistUser", userMetrics) + 1
  633. }
  634. }
  635. if user.IsActive {
  636. userMetrics["TotalActivateRegistUser"] = getMapKeyStringValue("TotalActivateRegistUser", userMetrics) + 1
  637. }
  638. if dateRecord.UserIndex > 0 || dateRecord.LoginCount > 0 {
  639. userMetrics["HasActivityUser"] = getMapKeyStringValue("HasActivityUser", userMetrics) + 1
  640. }
  641. }
  642. func getParaWeight() map[string]float64 {
  643. result := make(map[string]float64)
  644. statictisSess := xStatistic.NewSession()
  645. defer statictisSess.Close()
  646. statictisSess.Select("*").Table(new(UserAnalysisPara))
  647. paraList := make([]*UserAnalysisPara, 0)
  648. statictisSess.Find(&paraList)
  649. for _, paraRecord := range paraList {
  650. result[paraRecord.Key] = paraRecord.Value
  651. }
  652. return result
  653. }
  654. func getUserIndexFromAnalysisAll(dateRecord UserBusinessAnalysisAll, ParaWeight map[string]float64) float64 {
  655. var result float64
  656. // PR数 0.20
  657. // commit数 0.20
  658. // 提出任务数 0.20
  659. // 评论数 0.20
  660. // 关注项目数 0.10
  661. // 点赞项目数 0.10
  662. // 登录次数 0.10
  663. result = float64(dateRecord.CodeMergeCount) * getParaWeightValue("CodeMergeCount", ParaWeight, 0.2)
  664. result += float64(dateRecord.CommitCount) * getParaWeightValue("CommitCount", ParaWeight, 0.2)
  665. log.Info("1 result=" + fmt.Sprint(result))
  666. result += float64(dateRecord.IssueCount) * getParaWeightValue("IssueCount", ParaWeight, 0.2)
  667. result += float64(dateRecord.CommentCount) * getParaWeightValue("CommentCount", ParaWeight, 0.2)
  668. result += float64(dateRecord.FocusRepoCount) * getParaWeightValue("FocusRepoCount", ParaWeight, 0.1)
  669. result += float64(dateRecord.StarRepoCount) * getParaWeightValue("StarRepoCount", ParaWeight, 0.1)
  670. result += float64(dateRecord.LoginCount) * getParaWeightValue("LoginCount", ParaWeight, 0.1)
  671. result += float64(dateRecord.WatchedCount) * getParaWeightValue("WatchedCount", ParaWeight, 0.3)
  672. result += float64(dateRecord.CommitCodeSize) * getParaWeightValue("CommitCodeSize", ParaWeight, 0.1)
  673. result += float64(dateRecord.SolveIssueCount) * getParaWeightValue("SolveIssueCount", ParaWeight, 0.2)
  674. result += float64(dateRecord.EncyclopediasCount) * getParaWeightValue("EncyclopediasCount", ParaWeight, 0.1)
  675. result += float64(dateRecord.CreateRepoCount) * getParaWeightValue("CreateRepoCount", ParaWeight, 0.05)
  676. result += float64(dateRecord.CloudBrainTaskNum) * getParaWeightValue("CloudBrainTaskNum", ParaWeight, 0.3)
  677. result += float64(dateRecord.CommitModelCount) * getParaWeightValue("CommitModelCount", ParaWeight, 0.2)
  678. result += dateRecord.OpenIIndex * getParaWeightValue("OpenIIndex", ParaWeight, 0.1)
  679. return result
  680. }
  681. func getUserIndex(dateRecord UserBusinessAnalysis, ParaWeight map[string]float64) float64 {
  682. var result float64
  683. // PR数 0.20
  684. // commit数 0.20
  685. // 提出任务数 0.20
  686. // 评论数 0.20
  687. // 关注项目数 0.10
  688. // 点赞项目数 0.10
  689. // 登录次数 0.10
  690. result = float64(dateRecord.CodeMergeCount) * getParaWeightValue("CodeMergeCount", ParaWeight, 0.2)
  691. result += float64(dateRecord.CommitCount) * getParaWeightValue("CommitCount", ParaWeight, 0.2)
  692. log.Info("2 result=" + fmt.Sprint(result))
  693. result += float64(dateRecord.IssueCount) * getParaWeightValue("IssueCount", ParaWeight, 0.2)
  694. result += float64(dateRecord.CommentCount) * getParaWeightValue("CommentCount", ParaWeight, 0.2)
  695. result += float64(dateRecord.FocusRepoCount) * getParaWeightValue("FocusRepoCount", ParaWeight, 0.1)
  696. result += float64(dateRecord.StarRepoCount) * getParaWeightValue("StarRepoCount", ParaWeight, 0.1)
  697. result += float64(dateRecord.LoginCount) * getParaWeightValue("LoginCount", ParaWeight, 0.1)
  698. result += float64(dateRecord.WatchedCount) * getParaWeightValue("WatchedCount", ParaWeight, 0.3)
  699. result += float64(dateRecord.CommitCodeSize) * getParaWeightValue("CommitCodeSize", ParaWeight, 0.1)
  700. result += float64(dateRecord.SolveIssueCount) * getParaWeightValue("SolveIssueCount", ParaWeight, 0.2)
  701. result += float64(dateRecord.EncyclopediasCount) * getParaWeightValue("EncyclopediasCount", ParaWeight, 0.1)
  702. result += float64(dateRecord.CreateRepoCount) * getParaWeightValue("CreateRepoCount", ParaWeight, 0.05)
  703. result += float64(dateRecord.CloudBrainTaskNum) * getParaWeightValue("CloudBrainTaskNum", ParaWeight, 0.3)
  704. result += float64(dateRecord.CommitModelCount) * getParaWeightValue("CommitModelCount", ParaWeight, 0.2)
  705. result += dateRecord.OpenIIndex * getParaWeightValue("OpenIIndex", ParaWeight, 0.1)
  706. return result
  707. }
  708. func getParaWeightValue(key string, valueMap map[string]float64, defaultValue float64) float64 {
  709. if _, ok := valueMap[key]; !ok {
  710. return defaultValue
  711. } else {
  712. return valueMap[key]
  713. }
  714. }
  715. func getMapKeyStringValue(key string, valueMap map[string]int) int {
  716. if _, ok := valueMap[key]; !ok {
  717. return 0
  718. } else {
  719. return valueMap[key]
  720. }
  721. }
  722. func getMapValue(userId int64, valueMap map[int64]int) int {
  723. if _, ok := valueMap[userId]; !ok {
  724. return 0
  725. } else {
  726. return valueMap[userId]
  727. }
  728. }
  729. func getInt(str string) int {
  730. re, err := strconv.ParseInt(str, 10, 32)
  731. if err != nil {
  732. return 0
  733. }
  734. return int(re)
  735. }
  736. func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
  737. CounDataByDateAndReCount(wikiCountMap, startTime, endTime, false)
  738. }
  739. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  740. sess := x.NewSession()
  741. defer sess.Close()
  742. resultMap := make(map[int64]int)
  743. cond := "issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix)
  744. count, err := sess.Table("issue_assignees").Join("inner", "issue", "issue.id=issue_assignees.issue_id").Where(cond).Count(new(IssueAssignees))
  745. if err != nil {
  746. log.Info("query issue error. return.")
  747. return resultMap
  748. }
  749. var indexTotal int64
  750. indexTotal = 0
  751. for {
  752. issueAssigneesList := make([]*IssueAssignees, 0)
  753. sess.Select("issue_assignees.*").Table("issue_assignees").
  754. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  755. Where(cond).OrderBy("issue_assignees.id asc").Limit(PAGE_SIZE, int(indexTotal))
  756. sess.Find(&issueAssigneesList)
  757. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  758. for _, issueAssigneesRecord := range issueAssigneesList {
  759. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  760. resultMap[issueAssigneesRecord.AssigneeID] = 1
  761. } else {
  762. resultMap[issueAssigneesRecord.AssigneeID] += 1
  763. }
  764. }
  765. indexTotal += PAGE_SIZE
  766. if indexTotal >= count {
  767. break
  768. }
  769. }
  770. return resultMap
  771. }
  772. func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
  773. sess := x.NewSession()
  774. defer sess.Close()
  775. resultMap := make(map[int64]int)
  776. cond := "pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix)
  777. count, err := sess.Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Count(new(Issue))
  778. if err != nil {
  779. log.Info("query issue error. return.")
  780. return resultMap
  781. }
  782. var indexTotal int64
  783. indexTotal = 0
  784. for {
  785. issueList := make([]*Issue, 0)
  786. 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))
  787. sess.Find(&issueList)
  788. log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
  789. for _, issueRecord := range issueList {
  790. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  791. resultMap[issueRecord.PosterID] = 1
  792. } else {
  793. resultMap[issueRecord.PosterID] += 1
  794. }
  795. }
  796. indexTotal += PAGE_SIZE
  797. if indexTotal >= count {
  798. break
  799. }
  800. }
  801. return resultMap
  802. }
  803. func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  804. sess := x.NewSession()
  805. defer sess.Close()
  806. resultMap := make(map[int64]int)
  807. 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)
  808. count, err := sess.Where(cond).Count(new(Action))
  809. if err != nil {
  810. log.Info("query action error. return.")
  811. return resultMap
  812. }
  813. var indexTotal int64
  814. indexTotal = 0
  815. for {
  816. sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  817. actionList := make([]*Action, 0)
  818. sess.Find(&actionList)
  819. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  820. for _, actionRecord := range actionList {
  821. if _, ok := resultMap[actionRecord.UserID]; !ok {
  822. resultMap[actionRecord.UserID] = 1
  823. } else {
  824. resultMap[actionRecord.UserID] += 1
  825. }
  826. }
  827. indexTotal += PAGE_SIZE
  828. if indexTotal >= count {
  829. break
  830. }
  831. }
  832. return resultMap
  833. }
  834. func queryCreateIssue(start_unix int64, end_unix int64) map[int64]int {
  835. sess := x.NewSession()
  836. defer sess.Close()
  837. resultMap := make(map[int64]int)
  838. cond := "is_pull=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  839. count, err := sess.Where(cond).Count(new(Issue))
  840. if err != nil {
  841. log.Info("query Issue error. return.")
  842. return resultMap
  843. }
  844. var indexTotal int64
  845. indexTotal = 0
  846. for {
  847. sess.Select("id,poster_id").Table("issue").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  848. issueList := make([]*Issue, 0)
  849. sess.Find(&issueList)
  850. log.Info("query issue size=" + fmt.Sprint(len(issueList)))
  851. for _, issueRecord := range issueList {
  852. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  853. resultMap[issueRecord.PosterID] = 1
  854. } else {
  855. resultMap[issueRecord.PosterID] += 1
  856. }
  857. }
  858. indexTotal += PAGE_SIZE
  859. if indexTotal >= count {
  860. break
  861. }
  862. }
  863. return resultMap
  864. }
  865. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  866. sess := x.NewSession()
  867. defer sess.Close()
  868. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  869. resultMap := make(map[int64]int)
  870. count, err := sess.Where(cond).Count(new(Comment))
  871. if err != nil {
  872. log.Info("query Comment error. return.")
  873. return resultMap
  874. }
  875. var indexTotal int64
  876. indexTotal = 0
  877. for {
  878. sess.Select("id,type,poster_id").Table("comment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  879. commentList := make([]*Comment, 0)
  880. sess.Find(&commentList)
  881. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  882. for _, commentRecord := range commentList {
  883. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  884. resultMap[commentRecord.PosterID] = 1
  885. } else {
  886. resultMap[commentRecord.PosterID] += 1
  887. }
  888. }
  889. indexTotal += PAGE_SIZE
  890. if indexTotal >= count {
  891. break
  892. }
  893. }
  894. return resultMap
  895. }
  896. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  897. sess := x.NewSession()
  898. defer sess.Close()
  899. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  900. resultMap := make(map[int64]int)
  901. count, err := sess.Where(cond).Count(new(Watch))
  902. if err != nil {
  903. log.Info("query issue error. return.")
  904. return resultMap
  905. }
  906. var indexTotal int64
  907. indexTotal = 0
  908. for {
  909. watchList := make([]*Watch, 0)
  910. sess.Select("id,user_id,repo_id").Table("watch").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  911. sess.Find(&watchList)
  912. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  913. for _, watchRecord := range watchList {
  914. if _, ok := resultMap[watchRecord.UserID]; !ok {
  915. resultMap[watchRecord.UserID] = 1
  916. } else {
  917. resultMap[watchRecord.UserID] += 1
  918. }
  919. }
  920. indexTotal += PAGE_SIZE
  921. if indexTotal >= count {
  922. break
  923. }
  924. }
  925. return resultMap
  926. }
  927. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  928. sess := x.NewSession()
  929. defer sess.Close()
  930. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  931. resultMap := make(map[int64]int)
  932. count, err := sess.Where(cond).Count(new(Star))
  933. if err != nil {
  934. log.Info("query star error. return.")
  935. return resultMap
  936. }
  937. var indexTotal int64
  938. indexTotal = 0
  939. for {
  940. sess.Select("id,uid,repo_id").Table("star").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  941. starList := make([]*Star, 0)
  942. sess.Find(&starList)
  943. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  944. for _, starRecord := range starList {
  945. if _, ok := resultMap[starRecord.UID]; !ok {
  946. resultMap[starRecord.UID] = 1
  947. } else {
  948. resultMap[starRecord.UID] += 1
  949. }
  950. }
  951. indexTotal += PAGE_SIZE
  952. if indexTotal >= count {
  953. break
  954. }
  955. }
  956. return resultMap
  957. }
  958. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  959. sess := x.NewSession()
  960. defer sess.Close()
  961. resultMap := make(map[int64]int)
  962. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  963. count, err := sess.Where(cond).Count(new(Follow))
  964. if err != nil {
  965. log.Info("query follow error. return.")
  966. return resultMap
  967. }
  968. var indexTotal int64
  969. indexTotal = 0
  970. for {
  971. sess.Select("id,user_id,follow_id").Table("follow").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  972. followList := make([]*Follow, 0)
  973. sess.Find(&followList)
  974. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  975. for _, followRecord := range followList {
  976. if _, ok := resultMap[followRecord.FollowID]; !ok {
  977. resultMap[followRecord.FollowID] = 1
  978. } else {
  979. resultMap[followRecord.FollowID] += 1
  980. }
  981. }
  982. indexTotal += PAGE_SIZE
  983. if indexTotal >= count {
  984. break
  985. }
  986. }
  987. return resultMap
  988. }
  989. func queryDatasetSize(start_unix int64, end_unix int64) (map[int64]int, map[int64]int) {
  990. sess := x.NewSession()
  991. defer sess.Close()
  992. resultSizeMap := make(map[int64]int)
  993. resultNumMap := make(map[int64]int)
  994. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  995. count, err := sess.Where(cond).Count(new(Attachment))
  996. if err != nil {
  997. log.Info("query attachment error. return.")
  998. return resultSizeMap, resultNumMap
  999. }
  1000. var indexTotal int64
  1001. indexTotal = 0
  1002. for {
  1003. sess.Select("id,uploader_id,size").Table("attachment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1004. attachmentList := make([]*Attachment, 0)
  1005. sess.Find(&attachmentList)
  1006. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  1007. for _, attachRecord := range attachmentList {
  1008. if _, ok := resultSizeMap[attachRecord.UploaderID]; !ok {
  1009. resultSizeMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  1010. resultNumMap[attachRecord.UploaderID] = 1
  1011. } else {
  1012. resultSizeMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  1013. resultNumMap[attachRecord.UploaderID] += 1
  1014. }
  1015. }
  1016. indexTotal += PAGE_SIZE
  1017. if indexTotal >= count {
  1018. break
  1019. }
  1020. }
  1021. return resultSizeMap, resultNumMap
  1022. }
  1023. func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
  1024. sess := x.NewSession()
  1025. defer sess.Close()
  1026. resultMap := make(map[int64]int)
  1027. cond := "is_fork=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1028. count, err := sess.Where(cond).Count(new(Repository))
  1029. if err != nil {
  1030. log.Info("query Repository error. return.")
  1031. return resultMap
  1032. }
  1033. var indexTotal int64
  1034. indexTotal = 0
  1035. for {
  1036. sess.Select("id,owner_id,name").Table("repository").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1037. repoList := make([]*Repository, 0)
  1038. sess.Find(&repoList)
  1039. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  1040. for _, repoRecord := range repoList {
  1041. if _, ok := resultMap[repoRecord.OwnerID]; !ok {
  1042. resultMap[repoRecord.OwnerID] = 1
  1043. } else {
  1044. resultMap[repoRecord.OwnerID] += 1
  1045. }
  1046. }
  1047. indexTotal += PAGE_SIZE
  1048. if indexTotal >= count {
  1049. break
  1050. }
  1051. }
  1052. return resultMap
  1053. }
  1054. func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
  1055. statictisSess := xStatistic.NewSession()
  1056. defer statictisSess.Close()
  1057. 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")
  1058. repoStatisticList := make([]*RepoStatistic, 0)
  1059. statictisSess.Find(&repoStatisticList)
  1060. repoOpenIIndexMap := make(map[int64]float64)
  1061. log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
  1062. for _, repoRecord := range repoStatisticList {
  1063. if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
  1064. repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
  1065. }
  1066. }
  1067. sess := x.NewSession()
  1068. defer sess.Close()
  1069. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
  1070. repoList := make([]*Repository, 0)
  1071. sess.Find(&repoList)
  1072. userMap := make(map[int64]float64)
  1073. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  1074. for _, repoRecord := range repoList {
  1075. if _, ok := userMap[repoRecord.OwnerID]; !ok {
  1076. if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
  1077. userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
  1078. }
  1079. }
  1080. }
  1081. //query collaboration
  1082. sess.Select("repo_id,user_id,mode").Table("collaboration")
  1083. collaborationList := make([]*Collaboration, 0)
  1084. sess.Find(&collaborationList)
  1085. log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
  1086. for _, collaborationRecord := range collaborationList {
  1087. if _, ok := userMap[collaborationRecord.UserID]; !ok {
  1088. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  1089. userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
  1090. }
  1091. } else {
  1092. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  1093. userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
  1094. }
  1095. }
  1096. }
  1097. log.Info("user openi index size=" + fmt.Sprint(len(userMap)))
  1098. return userMap
  1099. }
  1100. func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
  1101. statictisSess := xStatistic.NewSession()
  1102. defer statictisSess.Close()
  1103. resultMap := make(map[int64]int)
  1104. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1105. count, err := statictisSess.Where(cond).Count(new(UserLoginLog))
  1106. if err != nil {
  1107. log.Info("query UserLoginLog error. return.")
  1108. return resultMap
  1109. }
  1110. var indexTotal int64
  1111. indexTotal = 0
  1112. for {
  1113. statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1114. userLoginLogList := make([]*UserLoginLog, 0)
  1115. statictisSess.Find(&userLoginLogList)
  1116. log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
  1117. for _, loginRecord := range userLoginLogList {
  1118. if _, ok := resultMap[loginRecord.UId]; !ok {
  1119. resultMap[loginRecord.UId] = 1
  1120. } else {
  1121. resultMap[loginRecord.UId] += 1
  1122. }
  1123. }
  1124. indexTotal += PAGE_SIZE
  1125. if indexTotal >= count {
  1126. break
  1127. }
  1128. }
  1129. log.Info("user login size=" + fmt.Sprint(len(resultMap)))
  1130. return resultMap
  1131. }
  1132. func queryCommitCodeSize(start_unix int64, end_unix int64) map[int64]int {
  1133. statictisSess := xStatistic.NewSession()
  1134. defer statictisSess.Close()
  1135. resultMap := make(map[int64]int)
  1136. cond := "count_date>=" + fmt.Sprint(start_unix) + " and count_date<=" + fmt.Sprint(end_unix)
  1137. count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
  1138. if err != nil {
  1139. log.Info("query commit code size error. return.")
  1140. return resultMap
  1141. }
  1142. var indexTotal int64
  1143. indexTotal = 0
  1144. for {
  1145. statictisSess.Select("id,commit_code_size").Table("user_business_analysis").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1146. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  1147. statictisSess.Find(&userBusinessAnalysisList)
  1148. log.Info("query user login size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  1149. for _, analysisRecord := range userBusinessAnalysisList {
  1150. if _, ok := resultMap[analysisRecord.ID]; !ok {
  1151. resultMap[analysisRecord.ID] = analysisRecord.CommitCodeSize
  1152. } else {
  1153. resultMap[analysisRecord.ID] += analysisRecord.CommitCodeSize
  1154. }
  1155. }
  1156. indexTotal += PAGE_SIZE
  1157. if indexTotal >= count {
  1158. break
  1159. }
  1160. }
  1161. log.Info("user commit code size=" + fmt.Sprint(len(resultMap)))
  1162. return resultMap
  1163. }
  1164. func queryUserModel(start_unix int64, end_unix int64) map[int64]int {
  1165. sess := x.NewSession()
  1166. defer sess.Close()
  1167. resultMap := make(map[int64]int)
  1168. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1169. count, err := sess.Where(cond).Count(new(AiModelManage))
  1170. if err != nil {
  1171. log.Info("query AiModelManage error. return.")
  1172. return resultMap
  1173. }
  1174. var indexTotal int64
  1175. indexTotal = 0
  1176. for {
  1177. sess.Select("id,user_id").Table("ai_model_manage").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1178. aiModelList := make([]*AiModelManage, 0)
  1179. sess.Find(&aiModelList)
  1180. log.Info("query AiModelManage size=" + fmt.Sprint(len(aiModelList)))
  1181. for _, aiModelRecord := range aiModelList {
  1182. if _, ok := resultMap[aiModelRecord.UserId]; !ok {
  1183. resultMap[aiModelRecord.UserId] = 1
  1184. } else {
  1185. resultMap[aiModelRecord.UserId] += 1
  1186. }
  1187. }
  1188. indexTotal += PAGE_SIZE
  1189. if indexTotal >= count {
  1190. break
  1191. }
  1192. }
  1193. return resultMap
  1194. }
  1195. func queryCloudBrainTask(start_unix int64, end_unix int64) (map[int64]int, map[string]int) {
  1196. sess := x.NewSession()
  1197. defer sess.Close()
  1198. resultMap := make(map[int64]int)
  1199. resultItemMap := make(map[string]int)
  1200. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1201. count, err := sess.Where(cond).Count(new(Cloudbrain))
  1202. if err != nil {
  1203. log.Info("query cloudbrain error. return.")
  1204. return resultMap, resultItemMap
  1205. }
  1206. var indexTotal int64
  1207. indexTotal = 0
  1208. for {
  1209. sess.Select("id,job_type,user_id,duration,train_job_duration,type").Table("cloudbrain").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1210. cloudTaskList := make([]*Cloudbrain, 0)
  1211. sess.Find(&cloudTaskList)
  1212. log.Info("query cloudbrain size=" + fmt.Sprint(len(cloudTaskList)))
  1213. for _, cloudTaskRecord := range cloudTaskList {
  1214. if _, ok := resultMap[cloudTaskRecord.UserID]; !ok {
  1215. resultMap[cloudTaskRecord.UserID] = 1
  1216. } else {
  1217. resultMap[cloudTaskRecord.UserID] += 1
  1218. }
  1219. setMapKey("CloudBrainRunTime", cloudTaskRecord.UserID, int(cloudTaskRecord.Duration), resultItemMap)
  1220. if cloudTaskRecord.Type == 1 { //npu
  1221. if cloudTaskRecord.JobType == "TRAIN" {
  1222. setMapKey("NpuTrainJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1223. } else if cloudTaskRecord.JobType == "INFERENCE" {
  1224. setMapKey("NpuInferenceJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1225. } else {
  1226. setMapKey("NpuDebugJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1227. }
  1228. } else { //type=0 gpu
  1229. if cloudTaskRecord.JobType == "TRAIN" {
  1230. setMapKey("GpuTrainJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1231. } else if cloudTaskRecord.JobType == "BENCHMARK" {
  1232. setMapKey("GpuBenchMarkJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1233. } else {
  1234. setMapKey("GpuDebugJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1235. }
  1236. }
  1237. }
  1238. indexTotal += PAGE_SIZE
  1239. if indexTotal >= count {
  1240. break
  1241. }
  1242. }
  1243. return resultMap, resultItemMap
  1244. }
  1245. func setMapKey(key string, userId int64, value int, resultItemMap map[string]int) {
  1246. newKey := fmt.Sprint(userId) + "_" + key
  1247. if _, ok := resultItemMap[newKey]; !ok {
  1248. resultItemMap[newKey] = value
  1249. } else {
  1250. resultItemMap[newKey] += value
  1251. }
  1252. }
  1253. func subMonth(t1, t2 time.Time) (month int) {
  1254. y1 := t1.Year()
  1255. y2 := t2.Year()
  1256. m1 := int(t1.Month())
  1257. m2 := int(t2.Month())
  1258. d1 := t1.Day()
  1259. d2 := t2.Day()
  1260. yearInterval := y1 - y2
  1261. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  1262. if m1 < m2 || m1 == m2 && d1 < d2 {
  1263. yearInterval--
  1264. }
  1265. // 获取月数差值
  1266. monthInterval := (m1 + 12) - m2
  1267. if d1 < d2 {
  1268. monthInterval--
  1269. }
  1270. monthInterval %= 12
  1271. month = yearInterval*12 + monthInterval
  1272. if month == 0 {
  1273. month = 1
  1274. }
  1275. return month
  1276. }