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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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("commit_count 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. dateRecordBatch := make([]UserBusinessAnalysisAll, 0)
  368. for {
  369. sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  370. userList := make([]*User, 0)
  371. sess.Find(&userList)
  372. for _, userRecord := range userList {
  373. var dateRecordAll UserBusinessAnalysisAll
  374. dateRecordAll.ID = userRecord.ID
  375. dateRecordAll.Email = userRecord.Email
  376. dateRecordAll.RegistDate = userRecord.CreatedUnix
  377. dateRecordAll.Name = userRecord.Name
  378. dateRecordAll.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  379. dateRecordAll.DataDate = DataDate
  380. dateRecordAll.UserLocation = userRecord.Location
  381. dateRecordAll.CodeMergeCount = getMapValue(dateRecordAll.ID, CodeMergeCountMap)
  382. dateRecordAll.CommitCount = getMapValue(dateRecordAll.ID, CommitCountMap)
  383. dateRecordAll.IssueCount = getMapValue(dateRecordAll.ID, IssueCountMap)
  384. dateRecordAll.CommentCount = getMapValue(dateRecordAll.ID, CommentCountMap)
  385. dateRecordAll.FocusRepoCount = getMapValue(dateRecordAll.ID, FocusRepoCountMap)
  386. dateRecordAll.StarRepoCount = getMapValue(dateRecordAll.ID, StarRepoCountMap)
  387. dateRecordAll.WatchedCount = getMapValue(dateRecordAll.ID, WatchedCountMap)
  388. dateRecordAll.CommitCodeSize = getMapValue(dateRecordAll.ID, CommitCodeSizeMap)
  389. dateRecordAll.CommitDatasetSize = getMapValue(dateRecordAll.ID, CommitDatasetSizeMap)
  390. dateRecordAll.CommitDatasetNum = getMapValue(dateRecordAll.ID, CommitDatasetNumMap)
  391. dateRecordAll.SolveIssueCount = getMapValue(dateRecordAll.ID, SolveIssueCountMap)
  392. dateRecordAll.EncyclopediasCount = getMapKeyStringValue(dateRecordAll.Name, wikiCountMap)
  393. dateRecordAll.CreateRepoCount = getMapValue(dateRecordAll.ID, CreateRepoCountMap)
  394. dateRecordAll.LoginCount = getMapValue(dateRecordAll.ID, LoginCountMap)
  395. if _, ok := OpenIIndexMap[dateRecordAll.ID]; !ok {
  396. dateRecordAll.OpenIIndex = 0
  397. } else {
  398. dateRecordAll.OpenIIndex = OpenIIndexMap[dateRecordAll.ID]
  399. }
  400. dateRecordAll.CloudBrainTaskNum = getMapValue(dateRecordAll.ID, CloudBrainTaskMap)
  401. dateRecordAll.GpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_GpuDebugJob", CloudBrainTaskItemMap)
  402. dateRecordAll.NpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_NpuDebugJob", CloudBrainTaskItemMap)
  403. dateRecordAll.GpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_GpuTrainJob", CloudBrainTaskItemMap)
  404. dateRecordAll.NpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_NpuTrainJob", CloudBrainTaskItemMap)
  405. dateRecordAll.NpuInferenceJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_NpuInferenceJob", CloudBrainTaskItemMap)
  406. dateRecordAll.GpuBenchMarkJob = getMapKeyStringValue(fmt.Sprint(dateRecordAll.ID)+"_GpuBenchMarkJob", CloudBrainTaskItemMap)
  407. dateRecordAll.CommitModelCount = getMapValue(dateRecordAll.ID, AiModelManageMap)
  408. dateRecordAll.UserIndex = getUserIndexFromAnalysisAll(dateRecordAll, ParaWeight)
  409. dateRecordBatch = append(dateRecordBatch, dateRecordAll)
  410. if len(dateRecordBatch) >= BATCH_INSERT_SIZE {
  411. insertTable(dateRecordBatch, tableName, statictisSess)
  412. insertCount += BATCH_INSERT_SIZE
  413. if err != nil {
  414. log.Info("insert all data failed." + err.Error())
  415. }
  416. dateRecordBatch = make([]UserBusinessAnalysisAll, 0)
  417. }
  418. if tableName == "user_business_analysis_all" {
  419. if dateRecordAll.UserIndex > 0 || dateRecordAll.LoginCount > 0 {
  420. userMetrics["TotalHasActivityUser"] = getMapKeyStringValue("TotalHasActivityUser", userMetrics) + 1
  421. }
  422. }
  423. }
  424. indexTotal += PAGE_SIZE
  425. if indexTotal >= count {
  426. break
  427. }
  428. }
  429. if len(dateRecordBatch) > 0 {
  430. insertTable(dateRecordBatch, tableName, statictisSess)
  431. insertCount += len(dateRecordBatch)
  432. if err != nil {
  433. log.Info("insert all data failed." + err.Error())
  434. }
  435. }
  436. log.Info("refresh data finished.tableName=" + tableName + " total record:" + fmt.Sprint(insertCount))
  437. }
  438. func insertTable(dateRecords []UserBusinessAnalysisAll, tableName string, statictisSess *xorm.Session) {
  439. insertBatchSql := "INSERT INTO public." + tableName +
  440. "(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, " +
  441. "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) " +
  442. "VALUES"
  443. for i, record := range dateRecords {
  444. insertBatchSql += "(" + fmt.Sprint(record.ID) + ", " + fmt.Sprint(record.CountDate) + ", " + fmt.Sprint(record.CodeMergeCount) + ", " + fmt.Sprint(record.CommitCount) +
  445. ", " + fmt.Sprint(record.IssueCount) + ", " + fmt.Sprint(record.CommentCount) + ", " + fmt.Sprint(record.FocusRepoCount) + ", " + fmt.Sprint(record.StarRepoCount) +
  446. ", " + fmt.Sprint(record.WatchedCount) + ", " + fmt.Sprint(record.GiteaAgeMonth) + ", " + fmt.Sprint(record.CommitCodeSize) + ", " + fmt.Sprint(record.CommitDatasetSize) +
  447. ", " + fmt.Sprint(record.CommitModelCount) + ", " + fmt.Sprint(record.SolveIssueCount) + ", " + fmt.Sprint(record.EncyclopediasCount) + ", " + fmt.Sprint(record.RegistDate) +
  448. ", " + 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 + "')"
  449. if i < (len(dateRecords) - 1) {
  450. insertBatchSql += ","
  451. }
  452. }
  453. statictisSess.Exec(insertBatchSql)
  454. }
  455. func RefreshUserStaticAllTabel(wikiCountMap map[string]int, userMetrics map[string]int) {
  456. currentTimeNow := time.Now()
  457. pageStartTime := time.Date(2021, 11, 5, 0, 0, 0, 0, currentTimeNow.Location())
  458. pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  459. refreshUserStaticTable(wikiCountMap, "user_business_analysis_all", pageStartTime, pageEndTime, userMetrics)
  460. log.Info("refresh all data finished.")
  461. pageStartTime = time.Date(currentTimeNow.Year(), 1, 1, 0, 0, 0, 0, currentTimeNow.Location())
  462. refreshUserStaticTable(wikiCountMap, "user_business_analysis_current_year", pageStartTime, pageEndTime, userMetrics)
  463. thisMonth := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
  464. refreshUserStaticTable(wikiCountMap, "user_business_analysis_current_month", thisMonth, pageEndTime, userMetrics)
  465. offset := int(time.Monday - currentTimeNow.Weekday())
  466. if offset > 0 {
  467. offset = -6
  468. }
  469. pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  470. refreshUserStaticTable(wikiCountMap, "user_business_analysis_current_week", pageStartTime, pageEndTime, userMetrics)
  471. pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -30)
  472. refreshUserStaticTable(wikiCountMap, "user_business_analysis_last30_day", pageStartTime, pageEndTime, userMetrics)
  473. pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  474. pageEndTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
  475. refreshUserStaticTable(wikiCountMap, "user_business_analysis_yesterday", pageStartTime, pageEndTime, userMetrics)
  476. pageStartTime = thisMonth.AddDate(0, -1, 0)
  477. pageEndTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
  478. refreshUserStaticTable(wikiCountMap, "user_business_analysis_last_month", pageStartTime, pageEndTime, userMetrics)
  479. }
  480. func CounDataByDateAndReCount(wikiCountMap map[string]int, startTime time.Time, endTime time.Time, isReCount bool) error {
  481. log.Info("start to count other user info data")
  482. sess := x.NewSession()
  483. defer sess.Close()
  484. currentTimeNow := time.Now()
  485. log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
  486. start_unix := startTime.Unix()
  487. log.Info("DB query time:" + startTime.Format("2006-01-02 15:04:05"))
  488. end_unix := endTime.Unix()
  489. CountDate := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  490. if isReCount {
  491. CountDate = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 1, 0, 0, currentTimeNow.Location())
  492. }
  493. DataDate := startTime.Format("2006-01-02")
  494. CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
  495. CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
  496. IssueCountMap := queryCreateIssue(start_unix, end_unix)
  497. CommentCountMap := queryComment(start_unix, end_unix)
  498. FocusRepoCountMap := queryWatch(start_unix, end_unix)
  499. StarRepoCountMap := queryStar(start_unix, end_unix)
  500. WatchedCountMap := queryFollow(start_unix, end_unix)
  501. CommitCodeSizeMap, err := GetAllUserKPIStats()
  502. if err != nil {
  503. log.Info("query commit code errr.")
  504. } else {
  505. log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
  506. }
  507. CommitDatasetSizeMap, CommitDatasetNumMap := queryDatasetSize(start_unix, end_unix)
  508. SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
  509. CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
  510. LoginCountMap := queryLoginCount(start_unix, end_unix)
  511. OpenIIndexMap := queryUserRepoOpenIIndex(start_unix, end_unix)
  512. CloudBrainTaskMap, CloudBrainTaskItemMap := queryCloudBrainTask(start_unix, end_unix)
  513. AiModelManageMap := queryUserModel(start_unix, end_unix)
  514. statictisSess := xStatistic.NewSession()
  515. defer statictisSess.Close()
  516. cond := "type != 1 and is_active=true"
  517. count, err := sess.Where(cond).Count(new(User))
  518. if err != nil {
  519. log.Info("query user error. return.")
  520. return err
  521. }
  522. ParaWeight := getParaWeight()
  523. userMetrics := make(map[string]int)
  524. var indexTotal int64
  525. indexTotal = 0
  526. for {
  527. sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  528. userList := make([]*User, 0)
  529. sess.Find(&userList)
  530. for i, userRecord := range userList {
  531. var dateRecord UserBusinessAnalysis
  532. dateRecord.ID = userRecord.ID
  533. log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  534. dateRecord.CountDate = CountDate.Unix()
  535. statictisSess.Delete(&dateRecord)
  536. dateRecord.Email = userRecord.Email
  537. dateRecord.RegistDate = userRecord.CreatedUnix
  538. dateRecord.Name = userRecord.Name
  539. dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
  540. dateRecord.DataDate = DataDate
  541. dateRecord.CodeMergeCount = getMapValue(dateRecord.ID, CodeMergeCountMap)
  542. dateRecord.CommitCount = getMapValue(dateRecord.ID, CommitCountMap)
  543. dateRecord.IssueCount = getMapValue(dateRecord.ID, IssueCountMap)
  544. dateRecord.CommentCount = getMapValue(dateRecord.ID, CommentCountMap)
  545. dateRecord.FocusRepoCount = getMapValue(dateRecord.ID, FocusRepoCountMap)
  546. dateRecord.StarRepoCount = getMapValue(dateRecord.ID, StarRepoCountMap)
  547. dateRecord.WatchedCount = getMapValue(dateRecord.ID, WatchedCountMap)
  548. if _, ok := CommitCodeSizeMap[dateRecord.Email]; !ok {
  549. dateRecord.CommitCodeSize = 0
  550. } else {
  551. dateRecord.CommitCodeSize = int(CommitCodeSizeMap[dateRecord.Email].CommitLines)
  552. }
  553. dateRecord.CommitDatasetSize = getMapValue(dateRecord.ID, CommitDatasetSizeMap)
  554. dateRecord.CommitDatasetNum = getMapValue(dateRecord.ID, CommitDatasetNumMap)
  555. dateRecord.SolveIssueCount = getMapValue(dateRecord.ID, SolveIssueCountMap)
  556. dateRecord.EncyclopediasCount = getMapKeyStringValue(dateRecord.Name, wikiCountMap)
  557. dateRecord.CreateRepoCount = getMapValue(dateRecord.ID, CreateRepoCountMap)
  558. dateRecord.LoginCount = getMapValue(dateRecord.ID, LoginCountMap)
  559. if _, ok := OpenIIndexMap[dateRecord.ID]; !ok {
  560. dateRecord.OpenIIndex = 0
  561. } else {
  562. dateRecord.OpenIIndex = OpenIIndexMap[dateRecord.ID]
  563. }
  564. dateRecord.CloudBrainTaskNum = getMapValue(dateRecord.ID, CloudBrainTaskMap)
  565. dateRecord.GpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_GpuDebugJob", CloudBrainTaskItemMap)
  566. dateRecord.NpuDebugJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_NpuDebugJob", CloudBrainTaskItemMap)
  567. dateRecord.GpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_GpuTrainJob", CloudBrainTaskItemMap)
  568. dateRecord.NpuTrainJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_NpuTrainJob", CloudBrainTaskItemMap)
  569. dateRecord.NpuInferenceJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_NpuInferenceJob", CloudBrainTaskItemMap)
  570. dateRecord.GpuBenchMarkJob = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_GpuBenchMarkJob", CloudBrainTaskItemMap)
  571. dateRecord.CloudBrainRunTime = getMapKeyStringValue(fmt.Sprint(dateRecord.ID)+"_CloudBrainRunTime", CloudBrainTaskItemMap)
  572. dateRecord.CommitModelCount = getMapValue(dateRecord.ID, AiModelManageMap)
  573. dateRecord.UserIndex = getUserIndex(dateRecord, ParaWeight)
  574. setUserMetrics(userMetrics, userRecord, start_unix, end_unix, dateRecord)
  575. _, err = statictisSess.Insert(&dateRecord)
  576. if err != nil {
  577. log.Info("insert daterecord failed." + err.Error())
  578. return err
  579. }
  580. }
  581. indexTotal += PAGE_SIZE
  582. if indexTotal >= count {
  583. break
  584. }
  585. }
  586. RefreshUserStaticAllTabel(wikiCountMap, userMetrics)
  587. //insert userMetrics table
  588. var useMetrics UserMetrics
  589. useMetrics.CountDate = CountDate.Unix()
  590. statictisSess.Delete(&useMetrics)
  591. useMetrics.ActivateRegistUser = getMapKeyStringValue("ActivateRegistUser", userMetrics)
  592. useMetrics.HasActivityUser = getMapKeyStringValue("HasActivityUser", userMetrics)
  593. useMetrics.NotActivateRegistUser = getMapKeyStringValue("NotActivateRegistUser", userMetrics)
  594. useMetrics.TotalActivateRegistUser = getMapKeyStringValue("TotalActivateRegistUser", userMetrics)
  595. useMetrics.TotalHasActivityUser = getMapKeyStringValue("TotalHasActivityUser", userMetrics)
  596. useMetrics.DataDate = currentTimeNow.Format("2006-01-02 15:04:05")
  597. statictisSess.Insert(&useMetrics)
  598. return nil
  599. }
  600. func setUserMetrics(userMetrics map[string]int, user *User, start_time int64, end_time int64, dateRecord UserBusinessAnalysis) {
  601. //ActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  602. //NotActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  603. //HasActivityUser int `xorm:"NOT NULL DEFAULT 0"`
  604. //TotalActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  605. //TotalHasActivityUser
  606. regist_time := user.CreatedUnix.AsTime().Unix()
  607. if regist_time >= start_time && regist_time <= end_time {
  608. if user.IsActive {
  609. userMetrics["ActivateRegistUser"] = getMapKeyStringValue("ActivateRegistUser", userMetrics) + 1
  610. } else {
  611. userMetrics["NotActivateRegistUser"] = getMapKeyStringValue("NotActivateRegistUser", userMetrics) + 1
  612. }
  613. }
  614. if user.IsActive {
  615. userMetrics["TotalActivateRegistUser"] = getMapKeyStringValue("TotalActivateRegistUser", userMetrics) + 1
  616. }
  617. if dateRecord.UserIndex > 0 || dateRecord.LoginCount > 0 {
  618. userMetrics["HasActivityUser"] = getMapKeyStringValue("HasActivityUser", userMetrics) + 1
  619. }
  620. }
  621. func getParaWeight() map[string]float64 {
  622. result := make(map[string]float64)
  623. statictisSess := xStatistic.NewSession()
  624. defer statictisSess.Close()
  625. statictisSess.Select("*").Table(new(UserAnalysisPara))
  626. paraList := make([]*UserAnalysisPara, 0)
  627. statictisSess.Find(&paraList)
  628. for _, paraRecord := range paraList {
  629. result[paraRecord.Key] = paraRecord.Value
  630. }
  631. return result
  632. }
  633. func getUserIndexFromAnalysisAll(dateRecord UserBusinessAnalysisAll, ParaWeight map[string]float64) float64 {
  634. var result float64
  635. // PR数 0.20
  636. // commit数 0.20
  637. // 提出任务数 0.20
  638. // 评论数 0.20
  639. // 关注项目数 0.10
  640. // 点赞项目数 0.10
  641. // 登录次数 0.10
  642. result = float64(dateRecord.CodeMergeCount) * getParaWeightValue("CodeMergeCount", ParaWeight, 0.2)
  643. result += float64(dateRecord.CommitCount) * getParaWeightValue("CommitCount", ParaWeight, 0.2)
  644. log.Info("1 result=" + fmt.Sprint(result))
  645. result += float64(dateRecord.IssueCount) * getParaWeightValue("IssueCount", ParaWeight, 0.2)
  646. result += float64(dateRecord.CommentCount) * getParaWeightValue("CommentCount", ParaWeight, 0.2)
  647. result += float64(dateRecord.FocusRepoCount) * getParaWeightValue("FocusRepoCount", ParaWeight, 0.1)
  648. result += float64(dateRecord.StarRepoCount) * getParaWeightValue("StarRepoCount", ParaWeight, 0.1)
  649. result += float64(dateRecord.LoginCount) * getParaWeightValue("LoginCount", ParaWeight, 0.1)
  650. result += float64(dateRecord.WatchedCount) * getParaWeightValue("WatchedCount", ParaWeight, 0.3)
  651. result += float64(dateRecord.CommitCodeSize) * getParaWeightValue("CommitCodeSize", ParaWeight, 0.1)
  652. result += float64(dateRecord.SolveIssueCount) * getParaWeightValue("SolveIssueCount", ParaWeight, 0.2)
  653. result += float64(dateRecord.EncyclopediasCount) * getParaWeightValue("EncyclopediasCount", ParaWeight, 0.1)
  654. result += float64(dateRecord.CreateRepoCount) * getParaWeightValue("CreateRepoCount", ParaWeight, 0.05)
  655. result += float64(dateRecord.CloudBrainTaskNum) * getParaWeightValue("CloudBrainTaskNum", ParaWeight, 0.3)
  656. result += float64(dateRecord.CommitModelCount) * getParaWeightValue("CommitModelCount", ParaWeight, 0.2)
  657. result += dateRecord.OpenIIndex * getParaWeightValue("OpenIIndex", ParaWeight, 0.1)
  658. return result
  659. }
  660. func getUserIndex(dateRecord UserBusinessAnalysis, ParaWeight map[string]float64) float64 {
  661. var result float64
  662. // PR数 0.20
  663. // commit数 0.20
  664. // 提出任务数 0.20
  665. // 评论数 0.20
  666. // 关注项目数 0.10
  667. // 点赞项目数 0.10
  668. // 登录次数 0.10
  669. result = float64(dateRecord.CodeMergeCount) * getParaWeightValue("CodeMergeCount", ParaWeight, 0.2)
  670. result += float64(dateRecord.CommitCount) * getParaWeightValue("CommitCount", ParaWeight, 0.2)
  671. log.Info("2 result=" + fmt.Sprint(result))
  672. result += float64(dateRecord.IssueCount) * getParaWeightValue("IssueCount", ParaWeight, 0.2)
  673. result += float64(dateRecord.CommentCount) * getParaWeightValue("CommentCount", ParaWeight, 0.2)
  674. result += float64(dateRecord.FocusRepoCount) * getParaWeightValue("FocusRepoCount", ParaWeight, 0.1)
  675. result += float64(dateRecord.StarRepoCount) * getParaWeightValue("StarRepoCount", ParaWeight, 0.1)
  676. result += float64(dateRecord.LoginCount) * getParaWeightValue("LoginCount", ParaWeight, 0.1)
  677. result += float64(dateRecord.WatchedCount) * getParaWeightValue("WatchedCount", ParaWeight, 0.3)
  678. result += float64(dateRecord.CommitCodeSize) * getParaWeightValue("CommitCodeSize", ParaWeight, 0.1)
  679. result += float64(dateRecord.SolveIssueCount) * getParaWeightValue("SolveIssueCount", ParaWeight, 0.2)
  680. result += float64(dateRecord.EncyclopediasCount) * getParaWeightValue("EncyclopediasCount", ParaWeight, 0.1)
  681. result += float64(dateRecord.CreateRepoCount) * getParaWeightValue("CreateRepoCount", ParaWeight, 0.05)
  682. result += float64(dateRecord.CloudBrainTaskNum) * getParaWeightValue("CloudBrainTaskNum", ParaWeight, 0.3)
  683. result += float64(dateRecord.CommitModelCount) * getParaWeightValue("CommitModelCount", ParaWeight, 0.2)
  684. result += dateRecord.OpenIIndex * getParaWeightValue("OpenIIndex", ParaWeight, 0.1)
  685. return result
  686. }
  687. func getParaWeightValue(key string, valueMap map[string]float64, defaultValue float64) float64 {
  688. if _, ok := valueMap[key]; !ok {
  689. return defaultValue
  690. } else {
  691. return valueMap[key]
  692. }
  693. }
  694. func getMapKeyStringValue(key string, valueMap map[string]int) int {
  695. if _, ok := valueMap[key]; !ok {
  696. return 0
  697. } else {
  698. return valueMap[key]
  699. }
  700. }
  701. func getMapValue(userId int64, valueMap map[int64]int) int {
  702. if _, ok := valueMap[userId]; !ok {
  703. return 0
  704. } else {
  705. return valueMap[userId]
  706. }
  707. }
  708. func getInt(str string) int {
  709. re, err := strconv.ParseInt(str, 10, 32)
  710. if err != nil {
  711. return 0
  712. }
  713. return int(re)
  714. }
  715. func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
  716. CounDataByDateAndReCount(wikiCountMap, startTime, endTime, false)
  717. }
  718. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  719. sess := x.NewSession()
  720. defer sess.Close()
  721. resultMap := make(map[int64]int)
  722. cond := "issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix)
  723. count, err := sess.Table("issue_assignees").Join("inner", "issue", "issue.id=issue_assignees.issue_id").Where(cond).Count(new(IssueAssignees))
  724. if err != nil {
  725. log.Info("query issue error. return.")
  726. return resultMap
  727. }
  728. var indexTotal int64
  729. indexTotal = 0
  730. for {
  731. issueAssigneesList := make([]*IssueAssignees, 0)
  732. sess.Select("issue_assignees.*").Table("issue_assignees").
  733. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  734. Where(cond).OrderBy("issue_assignees.id asc").Limit(PAGE_SIZE, int(indexTotal))
  735. sess.Find(&issueAssigneesList)
  736. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  737. for _, issueAssigneesRecord := range issueAssigneesList {
  738. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  739. resultMap[issueAssigneesRecord.AssigneeID] = 1
  740. } else {
  741. resultMap[issueAssigneesRecord.AssigneeID] += 1
  742. }
  743. }
  744. indexTotal += PAGE_SIZE
  745. if indexTotal >= count {
  746. break
  747. }
  748. }
  749. return resultMap
  750. }
  751. func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
  752. sess := x.NewSession()
  753. defer sess.Close()
  754. resultMap := make(map[int64]int)
  755. cond := "pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix)
  756. count, err := sess.Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Count(new(Issue))
  757. if err != nil {
  758. log.Info("query issue error. return.")
  759. return resultMap
  760. }
  761. var indexTotal int64
  762. indexTotal = 0
  763. for {
  764. issueList := make([]*Issue, 0)
  765. 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))
  766. sess.Find(&issueList)
  767. log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
  768. for _, issueRecord := range issueList {
  769. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  770. resultMap[issueRecord.PosterID] = 1
  771. } else {
  772. resultMap[issueRecord.PosterID] += 1
  773. }
  774. }
  775. indexTotal += PAGE_SIZE
  776. if indexTotal >= count {
  777. break
  778. }
  779. }
  780. return resultMap
  781. }
  782. func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  783. sess := x.NewSession()
  784. defer sess.Close()
  785. resultMap := make(map[int64]int)
  786. 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)
  787. count, err := sess.Where(cond).Count(new(Action))
  788. if err != nil {
  789. log.Info("query action error. return.")
  790. return resultMap
  791. }
  792. var indexTotal int64
  793. indexTotal = 0
  794. for {
  795. sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  796. actionList := make([]*Action, 0)
  797. sess.Find(&actionList)
  798. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  799. for _, actionRecord := range actionList {
  800. if _, ok := resultMap[actionRecord.UserID]; !ok {
  801. resultMap[actionRecord.UserID] = 1
  802. } else {
  803. resultMap[actionRecord.UserID] += 1
  804. }
  805. }
  806. indexTotal += PAGE_SIZE
  807. if indexTotal >= count {
  808. break
  809. }
  810. }
  811. return resultMap
  812. }
  813. func queryCreateIssue(start_unix int64, end_unix int64) map[int64]int {
  814. sess := x.NewSession()
  815. defer sess.Close()
  816. resultMap := make(map[int64]int)
  817. cond := "is_pull=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  818. count, err := sess.Where(cond).Count(new(Issue))
  819. if err != nil {
  820. log.Info("query Issue error. return.")
  821. return resultMap
  822. }
  823. var indexTotal int64
  824. indexTotal = 0
  825. for {
  826. sess.Select("id,poster_id").Table("issue").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  827. issueList := make([]*Issue, 0)
  828. sess.Find(&issueList)
  829. log.Info("query issue size=" + fmt.Sprint(len(issueList)))
  830. for _, issueRecord := range issueList {
  831. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  832. resultMap[issueRecord.PosterID] = 1
  833. } else {
  834. resultMap[issueRecord.PosterID] += 1
  835. }
  836. }
  837. indexTotal += PAGE_SIZE
  838. if indexTotal >= count {
  839. break
  840. }
  841. }
  842. return resultMap
  843. }
  844. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  845. sess := x.NewSession()
  846. defer sess.Close()
  847. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  848. resultMap := make(map[int64]int)
  849. count, err := sess.Where(cond).Count(new(Comment))
  850. if err != nil {
  851. log.Info("query Comment error. return.")
  852. return resultMap
  853. }
  854. var indexTotal int64
  855. indexTotal = 0
  856. for {
  857. sess.Select("id,type,poster_id").Table("comment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  858. commentList := make([]*Comment, 0)
  859. sess.Find(&commentList)
  860. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  861. for _, commentRecord := range commentList {
  862. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  863. resultMap[commentRecord.PosterID] = 1
  864. } else {
  865. resultMap[commentRecord.PosterID] += 1
  866. }
  867. }
  868. indexTotal += PAGE_SIZE
  869. if indexTotal >= count {
  870. break
  871. }
  872. }
  873. return resultMap
  874. }
  875. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  876. sess := x.NewSession()
  877. defer sess.Close()
  878. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  879. resultMap := make(map[int64]int)
  880. count, err := sess.Where(cond).Count(new(Watch))
  881. if err != nil {
  882. log.Info("query issue error. return.")
  883. return resultMap
  884. }
  885. var indexTotal int64
  886. indexTotal = 0
  887. for {
  888. watchList := make([]*Watch, 0)
  889. sess.Select("id,user_id,repo_id").Table("watch").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  890. sess.Find(&watchList)
  891. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  892. for _, watchRecord := range watchList {
  893. if _, ok := resultMap[watchRecord.UserID]; !ok {
  894. resultMap[watchRecord.UserID] = 1
  895. } else {
  896. resultMap[watchRecord.UserID] += 1
  897. }
  898. }
  899. indexTotal += PAGE_SIZE
  900. if indexTotal >= count {
  901. break
  902. }
  903. }
  904. return resultMap
  905. }
  906. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  907. sess := x.NewSession()
  908. defer sess.Close()
  909. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  910. resultMap := make(map[int64]int)
  911. count, err := sess.Where(cond).Count(new(Star))
  912. if err != nil {
  913. log.Info("query star error. return.")
  914. return resultMap
  915. }
  916. var indexTotal int64
  917. indexTotal = 0
  918. for {
  919. sess.Select("id,uid,repo_id").Table("star").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  920. starList := make([]*Star, 0)
  921. sess.Find(&starList)
  922. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  923. for _, starRecord := range starList {
  924. if _, ok := resultMap[starRecord.UID]; !ok {
  925. resultMap[starRecord.UID] = 1
  926. } else {
  927. resultMap[starRecord.UID] += 1
  928. }
  929. }
  930. indexTotal += PAGE_SIZE
  931. if indexTotal >= count {
  932. break
  933. }
  934. }
  935. return resultMap
  936. }
  937. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  938. sess := x.NewSession()
  939. defer sess.Close()
  940. resultMap := make(map[int64]int)
  941. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  942. count, err := sess.Where(cond).Count(new(Follow))
  943. if err != nil {
  944. log.Info("query follow error. return.")
  945. return resultMap
  946. }
  947. var indexTotal int64
  948. indexTotal = 0
  949. for {
  950. sess.Select("id,user_id,follow_id").Table("follow").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  951. followList := make([]*Follow, 0)
  952. sess.Find(&followList)
  953. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  954. for _, followRecord := range followList {
  955. if _, ok := resultMap[followRecord.FollowID]; !ok {
  956. resultMap[followRecord.FollowID] = 1
  957. } else {
  958. resultMap[followRecord.FollowID] += 1
  959. }
  960. }
  961. indexTotal += PAGE_SIZE
  962. if indexTotal >= count {
  963. break
  964. }
  965. }
  966. return resultMap
  967. }
  968. func queryDatasetSize(start_unix int64, end_unix int64) (map[int64]int, map[int64]int) {
  969. sess := x.NewSession()
  970. defer sess.Close()
  971. resultSizeMap := make(map[int64]int)
  972. resultNumMap := make(map[int64]int)
  973. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  974. count, err := sess.Where(cond).Count(new(Attachment))
  975. if err != nil {
  976. log.Info("query attachment error. return.")
  977. return resultSizeMap, resultNumMap
  978. }
  979. var indexTotal int64
  980. indexTotal = 0
  981. for {
  982. sess.Select("id,uploader_id,size").Table("attachment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  983. attachmentList := make([]*Attachment, 0)
  984. sess.Find(&attachmentList)
  985. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  986. for _, attachRecord := range attachmentList {
  987. if _, ok := resultSizeMap[attachRecord.UploaderID]; !ok {
  988. resultSizeMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  989. resultNumMap[attachRecord.UploaderID] = 1
  990. } else {
  991. resultSizeMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  992. resultNumMap[attachRecord.UploaderID] += 1
  993. }
  994. }
  995. indexTotal += PAGE_SIZE
  996. if indexTotal >= count {
  997. break
  998. }
  999. }
  1000. return resultSizeMap, resultNumMap
  1001. }
  1002. func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
  1003. sess := x.NewSession()
  1004. defer sess.Close()
  1005. resultMap := make(map[int64]int)
  1006. cond := "is_fork=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1007. count, err := sess.Where(cond).Count(new(Repository))
  1008. if err != nil {
  1009. log.Info("query Repository error. return.")
  1010. return resultMap
  1011. }
  1012. var indexTotal int64
  1013. indexTotal = 0
  1014. for {
  1015. sess.Select("id,owner_id,name").Table("repository").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1016. repoList := make([]*Repository, 0)
  1017. sess.Find(&repoList)
  1018. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  1019. for _, repoRecord := range repoList {
  1020. if _, ok := resultMap[repoRecord.OwnerID]; !ok {
  1021. resultMap[repoRecord.OwnerID] = 1
  1022. } else {
  1023. resultMap[repoRecord.OwnerID] += 1
  1024. }
  1025. }
  1026. indexTotal += PAGE_SIZE
  1027. if indexTotal >= count {
  1028. break
  1029. }
  1030. }
  1031. return resultMap
  1032. }
  1033. func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
  1034. statictisSess := xStatistic.NewSession()
  1035. defer statictisSess.Close()
  1036. 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")
  1037. repoStatisticList := make([]*RepoStatistic, 0)
  1038. statictisSess.Find(&repoStatisticList)
  1039. repoOpenIIndexMap := make(map[int64]float64)
  1040. log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
  1041. for _, repoRecord := range repoStatisticList {
  1042. if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
  1043. repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
  1044. }
  1045. }
  1046. sess := x.NewSession()
  1047. defer sess.Close()
  1048. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
  1049. repoList := make([]*Repository, 0)
  1050. sess.Find(&repoList)
  1051. userMap := make(map[int64]float64)
  1052. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  1053. for _, repoRecord := range repoList {
  1054. if _, ok := userMap[repoRecord.OwnerID]; !ok {
  1055. if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
  1056. userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
  1057. }
  1058. }
  1059. }
  1060. //query collaboration
  1061. sess.Select("repo_id,user_id,mode").Table("collaboration")
  1062. collaborationList := make([]*Collaboration, 0)
  1063. sess.Find(&collaborationList)
  1064. log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
  1065. for _, collaborationRecord := range collaborationList {
  1066. if _, ok := userMap[collaborationRecord.UserID]; !ok {
  1067. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  1068. userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
  1069. }
  1070. } else {
  1071. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  1072. userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
  1073. }
  1074. }
  1075. }
  1076. log.Info("user openi index size=" + fmt.Sprint(len(userMap)))
  1077. return userMap
  1078. }
  1079. func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
  1080. statictisSess := xStatistic.NewSession()
  1081. defer statictisSess.Close()
  1082. resultMap := make(map[int64]int)
  1083. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1084. count, err := statictisSess.Where(cond).Count(new(UserLoginLog))
  1085. if err != nil {
  1086. log.Info("query UserLoginLog error. return.")
  1087. return resultMap
  1088. }
  1089. var indexTotal int64
  1090. indexTotal = 0
  1091. for {
  1092. statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1093. userLoginLogList := make([]*UserLoginLog, 0)
  1094. statictisSess.Find(&userLoginLogList)
  1095. log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
  1096. for _, loginRecord := range userLoginLogList {
  1097. if _, ok := resultMap[loginRecord.UId]; !ok {
  1098. resultMap[loginRecord.UId] = 1
  1099. } else {
  1100. resultMap[loginRecord.UId] += 1
  1101. }
  1102. }
  1103. indexTotal += PAGE_SIZE
  1104. if indexTotal >= count {
  1105. break
  1106. }
  1107. }
  1108. log.Info("user login size=" + fmt.Sprint(len(resultMap)))
  1109. return resultMap
  1110. }
  1111. func queryCommitCodeSize(start_unix int64, end_unix int64) map[int64]int {
  1112. statictisSess := xStatistic.NewSession()
  1113. defer statictisSess.Close()
  1114. resultMap := make(map[int64]int)
  1115. cond := "count_date>=" + fmt.Sprint(start_unix) + " and count_date<=" + fmt.Sprint(end_unix)
  1116. count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
  1117. if err != nil {
  1118. log.Info("query commit code size error. return.")
  1119. return resultMap
  1120. }
  1121. var indexTotal int64
  1122. indexTotal = 0
  1123. for {
  1124. statictisSess.Select("id,commit_code_size").Table("user_business_analysis").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1125. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  1126. statictisSess.Find(&userBusinessAnalysisList)
  1127. log.Info("query user login size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  1128. for _, analysisRecord := range userBusinessAnalysisList {
  1129. if _, ok := resultMap[analysisRecord.ID]; !ok {
  1130. resultMap[analysisRecord.ID] = analysisRecord.CommitCodeSize
  1131. } else {
  1132. resultMap[analysisRecord.ID] += analysisRecord.CommitCodeSize
  1133. }
  1134. }
  1135. indexTotal += PAGE_SIZE
  1136. if indexTotal >= count {
  1137. break
  1138. }
  1139. }
  1140. log.Info("user commit code size=" + fmt.Sprint(len(resultMap)))
  1141. return resultMap
  1142. }
  1143. func queryUserModel(start_unix int64, end_unix int64) map[int64]int {
  1144. sess := x.NewSession()
  1145. defer sess.Close()
  1146. resultMap := make(map[int64]int)
  1147. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1148. count, err := sess.Where(cond).Count(new(AiModelManage))
  1149. if err != nil {
  1150. log.Info("query AiModelManage error. return.")
  1151. return resultMap
  1152. }
  1153. var indexTotal int64
  1154. indexTotal = 0
  1155. for {
  1156. sess.Select("id,user_id").Table("ai_model_manage").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1157. aiModelList := make([]*AiModelManage, 0)
  1158. sess.Find(&aiModelList)
  1159. log.Info("query AiModelManage size=" + fmt.Sprint(len(aiModelList)))
  1160. for _, aiModelRecord := range aiModelList {
  1161. if _, ok := resultMap[aiModelRecord.UserId]; !ok {
  1162. resultMap[aiModelRecord.UserId] = 1
  1163. } else {
  1164. resultMap[aiModelRecord.UserId] += 1
  1165. }
  1166. }
  1167. indexTotal += PAGE_SIZE
  1168. if indexTotal >= count {
  1169. break
  1170. }
  1171. }
  1172. return resultMap
  1173. }
  1174. func queryCloudBrainTask(start_unix int64, end_unix int64) (map[int64]int, map[string]int) {
  1175. sess := x.NewSession()
  1176. defer sess.Close()
  1177. resultMap := make(map[int64]int)
  1178. resultItemMap := make(map[string]int)
  1179. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1180. count, err := sess.Where(cond).Count(new(Cloudbrain))
  1181. if err != nil {
  1182. log.Info("query cloudbrain error. return.")
  1183. return resultMap, resultItemMap
  1184. }
  1185. var indexTotal int64
  1186. indexTotal = 0
  1187. for {
  1188. sess.Select("id,job_type,user_id,duration,train_job_duration,type").Table("cloudbrain").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1189. cloudTaskList := make([]*Cloudbrain, 0)
  1190. sess.Find(&cloudTaskList)
  1191. log.Info("query cloudbrain size=" + fmt.Sprint(len(cloudTaskList)))
  1192. for _, cloudTaskRecord := range cloudTaskList {
  1193. if _, ok := resultMap[cloudTaskRecord.UserID]; !ok {
  1194. resultMap[cloudTaskRecord.UserID] = 1
  1195. } else {
  1196. resultMap[cloudTaskRecord.UserID] += 1
  1197. }
  1198. setMapKey("CloudBrainRunTime", cloudTaskRecord.UserID, int(cloudTaskRecord.Duration), resultItemMap)
  1199. if cloudTaskRecord.Type == 1 { //npu
  1200. if cloudTaskRecord.JobType == "TRAIN" {
  1201. setMapKey("NpuTrainJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1202. } else if cloudTaskRecord.JobType == "INFERENCE" {
  1203. setMapKey("NpuInferenceJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1204. } else {
  1205. setMapKey("NpuDebugJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1206. }
  1207. } else { //type=0 gpu
  1208. if cloudTaskRecord.JobType == "TRAIN" {
  1209. setMapKey("GpuTrainJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1210. } else if cloudTaskRecord.JobType == "BENCHMARK" {
  1211. setMapKey("GpuBenchMarkJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1212. } else {
  1213. setMapKey("GpuDebugJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1214. }
  1215. }
  1216. }
  1217. indexTotal += PAGE_SIZE
  1218. if indexTotal >= count {
  1219. break
  1220. }
  1221. }
  1222. return resultMap, resultItemMap
  1223. }
  1224. func setMapKey(key string, userId int64, value int, resultItemMap map[string]int) {
  1225. newKey := fmt.Sprint(userId) + "_" + key
  1226. if _, ok := resultItemMap[newKey]; !ok {
  1227. resultItemMap[newKey] = value
  1228. } else {
  1229. resultItemMap[newKey] += value
  1230. }
  1231. }
  1232. func subMonth(t1, t2 time.Time) (month int) {
  1233. y1 := t1.Year()
  1234. y2 := t2.Year()
  1235. m1 := int(t1.Month())
  1236. m2 := int(t2.Month())
  1237. d1 := t1.Day()
  1238. d2 := t2.Day()
  1239. yearInterval := y1 - y2
  1240. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  1241. if m1 < m2 || m1 == m2 && d1 < d2 {
  1242. yearInterval--
  1243. }
  1244. // 获取月数差值
  1245. monthInterval := (m1 + 12) - m2
  1246. if d1 < d2 {
  1247. monthInterval--
  1248. }
  1249. monthInterval %= 12
  1250. month = yearInterval*12 + monthInterval
  1251. if month == 0 {
  1252. month = 1
  1253. }
  1254. return month
  1255. }