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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. statictisSess.Insert(&useMetrics)
  597. return nil
  598. }
  599. func setUserMetrics(userMetrics map[string]int, user *User, start_time int64, end_time int64, dateRecord UserBusinessAnalysis) {
  600. //ActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  601. //NotActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  602. //HasActivityUser int `xorm:"NOT NULL DEFAULT 0"`
  603. //TotalActivateRegistUser int `xorm:"NOT NULL DEFAULT 0"`
  604. //TotalHasActivityUser
  605. regist_time := user.CreatedUnix.AsTime().Unix()
  606. if regist_time >= start_time && regist_time <= end_time {
  607. if user.IsActive {
  608. userMetrics["ActivateRegistUser"] = getMapKeyStringValue("ActivateRegistUser", userMetrics) + 1
  609. } else {
  610. userMetrics["NotActivateRegistUser"] = getMapKeyStringValue("NotActivateRegistUser", userMetrics) + 1
  611. }
  612. }
  613. if user.IsActive {
  614. userMetrics["TotalActivateRegistUser"] = getMapKeyStringValue("TotalActivateRegistUser", userMetrics) + 1
  615. }
  616. if dateRecord.UserIndex > 0 || dateRecord.LoginCount > 0 {
  617. userMetrics["HasActivityUser"] = getMapKeyStringValue("HasActivityUser", userMetrics) + 1
  618. }
  619. }
  620. func getParaWeight() map[string]float64 {
  621. result := make(map[string]float64)
  622. statictisSess := xStatistic.NewSession()
  623. defer statictisSess.Close()
  624. statictisSess.Select("*").Table(new(UserAnalysisPara))
  625. paraList := make([]*UserAnalysisPara, 0)
  626. statictisSess.Find(&paraList)
  627. for _, paraRecord := range paraList {
  628. result[paraRecord.Key] = paraRecord.Value
  629. }
  630. return result
  631. }
  632. func getUserIndexFromAnalysisAll(dateRecord UserBusinessAnalysisAll, ParaWeight map[string]float64) float64 {
  633. var result float64
  634. // PR数 0.20
  635. // commit数 0.20
  636. // 提出任务数 0.20
  637. // 评论数 0.20
  638. // 关注项目数 0.10
  639. // 点赞项目数 0.10
  640. // 登录次数 0.10
  641. result = float64(dateRecord.CodeMergeCount) * getParaWeightValue("CodeMergeCount", ParaWeight, 0.2)
  642. result += float64(dateRecord.CommitCount) * getParaWeightValue("CommitCount", ParaWeight, 0.2)
  643. log.Info("1 result=" + fmt.Sprint(result))
  644. result += float64(dateRecord.IssueCount) * getParaWeightValue("IssueCount", ParaWeight, 0.2)
  645. result += float64(dateRecord.CommentCount) * getParaWeightValue("CommentCount", ParaWeight, 0.2)
  646. result += float64(dateRecord.FocusRepoCount) * getParaWeightValue("FocusRepoCount", ParaWeight, 0.1)
  647. result += float64(dateRecord.StarRepoCount) * getParaWeightValue("StarRepoCount", ParaWeight, 0.1)
  648. result += float64(dateRecord.LoginCount) * getParaWeightValue("LoginCount", ParaWeight, 0.1)
  649. result += float64(dateRecord.WatchedCount) * getParaWeightValue("WatchedCount", ParaWeight, 0.3)
  650. result += float64(dateRecord.CommitCodeSize) * getParaWeightValue("CommitCodeSize", ParaWeight, 0.1)
  651. result += float64(dateRecord.SolveIssueCount) * getParaWeightValue("SolveIssueCount", ParaWeight, 0.2)
  652. result += float64(dateRecord.EncyclopediasCount) * getParaWeightValue("EncyclopediasCount", ParaWeight, 0.1)
  653. result += float64(dateRecord.CreateRepoCount) * getParaWeightValue("CreateRepoCount", ParaWeight, 0.05)
  654. result += float64(dateRecord.CloudBrainTaskNum) * getParaWeightValue("CloudBrainTaskNum", ParaWeight, 0.3)
  655. result += float64(dateRecord.CommitModelCount) * getParaWeightValue("CommitModelCount", ParaWeight, 0.2)
  656. result += dateRecord.OpenIIndex * getParaWeightValue("OpenIIndex", ParaWeight, 0.1)
  657. return result
  658. }
  659. func getUserIndex(dateRecord UserBusinessAnalysis, ParaWeight map[string]float64) float64 {
  660. var result float64
  661. // PR数 0.20
  662. // commit数 0.20
  663. // 提出任务数 0.20
  664. // 评论数 0.20
  665. // 关注项目数 0.10
  666. // 点赞项目数 0.10
  667. // 登录次数 0.10
  668. result = float64(dateRecord.CodeMergeCount) * getParaWeightValue("CodeMergeCount", ParaWeight, 0.2)
  669. result += float64(dateRecord.CommitCount) * getParaWeightValue("CommitCount", ParaWeight, 0.2)
  670. log.Info("2 result=" + fmt.Sprint(result))
  671. result += float64(dateRecord.IssueCount) * getParaWeightValue("IssueCount", ParaWeight, 0.2)
  672. result += float64(dateRecord.CommentCount) * getParaWeightValue("CommentCount", ParaWeight, 0.2)
  673. result += float64(dateRecord.FocusRepoCount) * getParaWeightValue("FocusRepoCount", ParaWeight, 0.1)
  674. result += float64(dateRecord.StarRepoCount) * getParaWeightValue("StarRepoCount", ParaWeight, 0.1)
  675. result += float64(dateRecord.LoginCount) * getParaWeightValue("LoginCount", ParaWeight, 0.1)
  676. result += float64(dateRecord.WatchedCount) * getParaWeightValue("WatchedCount", ParaWeight, 0.3)
  677. result += float64(dateRecord.CommitCodeSize) * getParaWeightValue("CommitCodeSize", ParaWeight, 0.1)
  678. result += float64(dateRecord.SolveIssueCount) * getParaWeightValue("SolveIssueCount", ParaWeight, 0.2)
  679. result += float64(dateRecord.EncyclopediasCount) * getParaWeightValue("EncyclopediasCount", ParaWeight, 0.1)
  680. result += float64(dateRecord.CreateRepoCount) * getParaWeightValue("CreateRepoCount", ParaWeight, 0.05)
  681. result += float64(dateRecord.CloudBrainTaskNum) * getParaWeightValue("CloudBrainTaskNum", ParaWeight, 0.3)
  682. result += float64(dateRecord.CommitModelCount) * getParaWeightValue("CommitModelCount", ParaWeight, 0.2)
  683. result += dateRecord.OpenIIndex * getParaWeightValue("OpenIIndex", ParaWeight, 0.1)
  684. return result
  685. }
  686. func getParaWeightValue(key string, valueMap map[string]float64, defaultValue float64) float64 {
  687. if _, ok := valueMap[key]; !ok {
  688. return defaultValue
  689. } else {
  690. return valueMap[key]
  691. }
  692. }
  693. func getMapKeyStringValue(key string, valueMap map[string]int) int {
  694. if _, ok := valueMap[key]; !ok {
  695. return 0
  696. } else {
  697. return valueMap[key]
  698. }
  699. }
  700. func getMapValue(userId int64, valueMap map[int64]int) int {
  701. if _, ok := valueMap[userId]; !ok {
  702. return 0
  703. } else {
  704. return valueMap[userId]
  705. }
  706. }
  707. func getInt(str string) int {
  708. re, err := strconv.ParseInt(str, 10, 32)
  709. if err != nil {
  710. return 0
  711. }
  712. return int(re)
  713. }
  714. func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
  715. CounDataByDateAndReCount(wikiCountMap, startTime, endTime, false)
  716. }
  717. func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
  718. sess := x.NewSession()
  719. defer sess.Close()
  720. resultMap := make(map[int64]int)
  721. cond := "issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix)
  722. count, err := sess.Table("issue_assignees").Join("inner", "issue", "issue.id=issue_assignees.issue_id").Where(cond).Count(new(IssueAssignees))
  723. if err != nil {
  724. log.Info("query issue error. return.")
  725. return resultMap
  726. }
  727. var indexTotal int64
  728. indexTotal = 0
  729. for {
  730. issueAssigneesList := make([]*IssueAssignees, 0)
  731. sess.Select("issue_assignees.*").Table("issue_assignees").
  732. Join("inner", "issue", "issue.id=issue_assignees.issue_id").
  733. Where(cond).OrderBy("issue_assignees.id asc").Limit(PAGE_SIZE, int(indexTotal))
  734. sess.Find(&issueAssigneesList)
  735. log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
  736. for _, issueAssigneesRecord := range issueAssigneesList {
  737. if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
  738. resultMap[issueAssigneesRecord.AssigneeID] = 1
  739. } else {
  740. resultMap[issueAssigneesRecord.AssigneeID] += 1
  741. }
  742. }
  743. indexTotal += PAGE_SIZE
  744. if indexTotal >= count {
  745. break
  746. }
  747. }
  748. return resultMap
  749. }
  750. func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
  751. sess := x.NewSession()
  752. defer sess.Close()
  753. resultMap := make(map[int64]int)
  754. cond := "pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix)
  755. count, err := sess.Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Count(new(Issue))
  756. if err != nil {
  757. log.Info("query issue error. return.")
  758. return resultMap
  759. }
  760. var indexTotal int64
  761. indexTotal = 0
  762. for {
  763. issueList := make([]*Issue, 0)
  764. 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))
  765. sess.Find(&issueList)
  766. log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
  767. for _, issueRecord := range issueList {
  768. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  769. resultMap[issueRecord.PosterID] = 1
  770. } else {
  771. resultMap[issueRecord.PosterID] += 1
  772. }
  773. }
  774. indexTotal += PAGE_SIZE
  775. if indexTotal >= count {
  776. break
  777. }
  778. }
  779. return resultMap
  780. }
  781. func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
  782. sess := x.NewSession()
  783. defer sess.Close()
  784. resultMap := make(map[int64]int)
  785. 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)
  786. count, err := sess.Where(cond).Count(new(Action))
  787. if err != nil {
  788. log.Info("query action error. return.")
  789. return resultMap
  790. }
  791. var indexTotal int64
  792. indexTotal = 0
  793. for {
  794. sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  795. actionList := make([]*Action, 0)
  796. sess.Find(&actionList)
  797. log.Info("query action size=" + fmt.Sprint(len(actionList)))
  798. for _, actionRecord := range actionList {
  799. if _, ok := resultMap[actionRecord.UserID]; !ok {
  800. resultMap[actionRecord.UserID] = 1
  801. } else {
  802. resultMap[actionRecord.UserID] += 1
  803. }
  804. }
  805. indexTotal += PAGE_SIZE
  806. if indexTotal >= count {
  807. break
  808. }
  809. }
  810. return resultMap
  811. }
  812. func queryCreateIssue(start_unix int64, end_unix int64) map[int64]int {
  813. sess := x.NewSession()
  814. defer sess.Close()
  815. resultMap := make(map[int64]int)
  816. cond := "is_pull=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  817. count, err := sess.Where(cond).Count(new(Issue))
  818. if err != nil {
  819. log.Info("query Issue error. return.")
  820. return resultMap
  821. }
  822. var indexTotal int64
  823. indexTotal = 0
  824. for {
  825. sess.Select("id,poster_id").Table("issue").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  826. issueList := make([]*Issue, 0)
  827. sess.Find(&issueList)
  828. log.Info("query issue size=" + fmt.Sprint(len(issueList)))
  829. for _, issueRecord := range issueList {
  830. if _, ok := resultMap[issueRecord.PosterID]; !ok {
  831. resultMap[issueRecord.PosterID] = 1
  832. } else {
  833. resultMap[issueRecord.PosterID] += 1
  834. }
  835. }
  836. indexTotal += PAGE_SIZE
  837. if indexTotal >= count {
  838. break
  839. }
  840. }
  841. return resultMap
  842. }
  843. func queryComment(start_unix int64, end_unix int64) map[int64]int {
  844. sess := x.NewSession()
  845. defer sess.Close()
  846. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  847. resultMap := make(map[int64]int)
  848. count, err := sess.Where(cond).Count(new(Comment))
  849. if err != nil {
  850. log.Info("query Comment error. return.")
  851. return resultMap
  852. }
  853. var indexTotal int64
  854. indexTotal = 0
  855. for {
  856. sess.Select("id,type,poster_id").Table("comment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  857. commentList := make([]*Comment, 0)
  858. sess.Find(&commentList)
  859. log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
  860. for _, commentRecord := range commentList {
  861. if _, ok := resultMap[commentRecord.PosterID]; !ok {
  862. resultMap[commentRecord.PosterID] = 1
  863. } else {
  864. resultMap[commentRecord.PosterID] += 1
  865. }
  866. }
  867. indexTotal += PAGE_SIZE
  868. if indexTotal >= count {
  869. break
  870. }
  871. }
  872. return resultMap
  873. }
  874. func queryWatch(start_unix int64, end_unix int64) map[int64]int {
  875. sess := x.NewSession()
  876. defer sess.Close()
  877. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  878. resultMap := make(map[int64]int)
  879. count, err := sess.Where(cond).Count(new(Watch))
  880. if err != nil {
  881. log.Info("query issue error. return.")
  882. return resultMap
  883. }
  884. var indexTotal int64
  885. indexTotal = 0
  886. for {
  887. watchList := make([]*Watch, 0)
  888. sess.Select("id,user_id,repo_id").Table("watch").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  889. sess.Find(&watchList)
  890. log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
  891. for _, watchRecord := range watchList {
  892. if _, ok := resultMap[watchRecord.UserID]; !ok {
  893. resultMap[watchRecord.UserID] = 1
  894. } else {
  895. resultMap[watchRecord.UserID] += 1
  896. }
  897. }
  898. indexTotal += PAGE_SIZE
  899. if indexTotal >= count {
  900. break
  901. }
  902. }
  903. return resultMap
  904. }
  905. func queryStar(start_unix int64, end_unix int64) map[int64]int {
  906. sess := x.NewSession()
  907. defer sess.Close()
  908. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  909. resultMap := make(map[int64]int)
  910. count, err := sess.Where(cond).Count(new(Star))
  911. if err != nil {
  912. log.Info("query star error. return.")
  913. return resultMap
  914. }
  915. var indexTotal int64
  916. indexTotal = 0
  917. for {
  918. sess.Select("id,uid,repo_id").Table("star").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  919. starList := make([]*Star, 0)
  920. sess.Find(&starList)
  921. log.Info("query Star size=" + fmt.Sprint(len(starList)))
  922. for _, starRecord := range starList {
  923. if _, ok := resultMap[starRecord.UID]; !ok {
  924. resultMap[starRecord.UID] = 1
  925. } else {
  926. resultMap[starRecord.UID] += 1
  927. }
  928. }
  929. indexTotal += PAGE_SIZE
  930. if indexTotal >= count {
  931. break
  932. }
  933. }
  934. return resultMap
  935. }
  936. func queryFollow(start_unix int64, end_unix int64) map[int64]int {
  937. sess := x.NewSession()
  938. defer sess.Close()
  939. resultMap := make(map[int64]int)
  940. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  941. count, err := sess.Where(cond).Count(new(Follow))
  942. if err != nil {
  943. log.Info("query follow error. return.")
  944. return resultMap
  945. }
  946. var indexTotal int64
  947. indexTotal = 0
  948. for {
  949. sess.Select("id,user_id,follow_id").Table("follow").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  950. followList := make([]*Follow, 0)
  951. sess.Find(&followList)
  952. log.Info("query Follow size=" + fmt.Sprint(len(followList)))
  953. for _, followRecord := range followList {
  954. if _, ok := resultMap[followRecord.FollowID]; !ok {
  955. resultMap[followRecord.FollowID] = 1
  956. } else {
  957. resultMap[followRecord.FollowID] += 1
  958. }
  959. }
  960. indexTotal += PAGE_SIZE
  961. if indexTotal >= count {
  962. break
  963. }
  964. }
  965. return resultMap
  966. }
  967. func queryDatasetSize(start_unix int64, end_unix int64) (map[int64]int, map[int64]int) {
  968. sess := x.NewSession()
  969. defer sess.Close()
  970. resultSizeMap := make(map[int64]int)
  971. resultNumMap := make(map[int64]int)
  972. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  973. count, err := sess.Where(cond).Count(new(Attachment))
  974. if err != nil {
  975. log.Info("query attachment error. return.")
  976. return resultSizeMap, resultNumMap
  977. }
  978. var indexTotal int64
  979. indexTotal = 0
  980. for {
  981. sess.Select("id,uploader_id,size").Table("attachment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  982. attachmentList := make([]*Attachment, 0)
  983. sess.Find(&attachmentList)
  984. log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
  985. for _, attachRecord := range attachmentList {
  986. if _, ok := resultSizeMap[attachRecord.UploaderID]; !ok {
  987. resultSizeMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
  988. resultNumMap[attachRecord.UploaderID] = 1
  989. } else {
  990. resultSizeMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
  991. resultNumMap[attachRecord.UploaderID] += 1
  992. }
  993. }
  994. indexTotal += PAGE_SIZE
  995. if indexTotal >= count {
  996. break
  997. }
  998. }
  999. return resultSizeMap, resultNumMap
  1000. }
  1001. func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
  1002. sess := x.NewSession()
  1003. defer sess.Close()
  1004. resultMap := make(map[int64]int)
  1005. cond := "is_fork=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1006. count, err := sess.Where(cond).Count(new(Repository))
  1007. if err != nil {
  1008. log.Info("query Repository error. return.")
  1009. return resultMap
  1010. }
  1011. var indexTotal int64
  1012. indexTotal = 0
  1013. for {
  1014. sess.Select("id,owner_id,name").Table("repository").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1015. repoList := make([]*Repository, 0)
  1016. sess.Find(&repoList)
  1017. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  1018. for _, repoRecord := range repoList {
  1019. if _, ok := resultMap[repoRecord.OwnerID]; !ok {
  1020. resultMap[repoRecord.OwnerID] = 1
  1021. } else {
  1022. resultMap[repoRecord.OwnerID] += 1
  1023. }
  1024. }
  1025. indexTotal += PAGE_SIZE
  1026. if indexTotal >= count {
  1027. break
  1028. }
  1029. }
  1030. return resultMap
  1031. }
  1032. func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
  1033. statictisSess := xStatistic.NewSession()
  1034. defer statictisSess.Close()
  1035. 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")
  1036. repoStatisticList := make([]*RepoStatistic, 0)
  1037. statictisSess.Find(&repoStatisticList)
  1038. repoOpenIIndexMap := make(map[int64]float64)
  1039. log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
  1040. for _, repoRecord := range repoStatisticList {
  1041. if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
  1042. repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
  1043. }
  1044. }
  1045. sess := x.NewSession()
  1046. defer sess.Close()
  1047. sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
  1048. repoList := make([]*Repository, 0)
  1049. sess.Find(&repoList)
  1050. userMap := make(map[int64]float64)
  1051. log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
  1052. for _, repoRecord := range repoList {
  1053. if _, ok := userMap[repoRecord.OwnerID]; !ok {
  1054. if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
  1055. userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
  1056. }
  1057. }
  1058. }
  1059. //query collaboration
  1060. sess.Select("repo_id,user_id,mode").Table("collaboration")
  1061. collaborationList := make([]*Collaboration, 0)
  1062. sess.Find(&collaborationList)
  1063. log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
  1064. for _, collaborationRecord := range collaborationList {
  1065. if _, ok := userMap[collaborationRecord.UserID]; !ok {
  1066. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  1067. userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
  1068. }
  1069. } else {
  1070. if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
  1071. userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
  1072. }
  1073. }
  1074. }
  1075. log.Info("user openi index size=" + fmt.Sprint(len(userMap)))
  1076. return userMap
  1077. }
  1078. func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
  1079. statictisSess := xStatistic.NewSession()
  1080. defer statictisSess.Close()
  1081. resultMap := make(map[int64]int)
  1082. cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1083. count, err := statictisSess.Where(cond).Count(new(UserLoginLog))
  1084. if err != nil {
  1085. log.Info("query UserLoginLog error. return.")
  1086. return resultMap
  1087. }
  1088. var indexTotal int64
  1089. indexTotal = 0
  1090. for {
  1091. statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1092. userLoginLogList := make([]*UserLoginLog, 0)
  1093. statictisSess.Find(&userLoginLogList)
  1094. log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
  1095. for _, loginRecord := range userLoginLogList {
  1096. if _, ok := resultMap[loginRecord.UId]; !ok {
  1097. resultMap[loginRecord.UId] = 1
  1098. } else {
  1099. resultMap[loginRecord.UId] += 1
  1100. }
  1101. }
  1102. indexTotal += PAGE_SIZE
  1103. if indexTotal >= count {
  1104. break
  1105. }
  1106. }
  1107. log.Info("user login size=" + fmt.Sprint(len(resultMap)))
  1108. return resultMap
  1109. }
  1110. func queryCommitCodeSize(start_unix int64, end_unix int64) map[int64]int {
  1111. statictisSess := xStatistic.NewSession()
  1112. defer statictisSess.Close()
  1113. resultMap := make(map[int64]int)
  1114. cond := "count_date>=" + fmt.Sprint(start_unix) + " and count_date<=" + fmt.Sprint(end_unix)
  1115. count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
  1116. if err != nil {
  1117. log.Info("query commit code size error. return.")
  1118. return resultMap
  1119. }
  1120. var indexTotal int64
  1121. indexTotal = 0
  1122. for {
  1123. statictisSess.Select("id,commit_code_size").Table("user_business_analysis").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1124. userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
  1125. statictisSess.Find(&userBusinessAnalysisList)
  1126. log.Info("query user login size=" + fmt.Sprint(len(userBusinessAnalysisList)))
  1127. for _, analysisRecord := range userBusinessAnalysisList {
  1128. if _, ok := resultMap[analysisRecord.ID]; !ok {
  1129. resultMap[analysisRecord.ID] = analysisRecord.CommitCodeSize
  1130. } else {
  1131. resultMap[analysisRecord.ID] += analysisRecord.CommitCodeSize
  1132. }
  1133. }
  1134. indexTotal += PAGE_SIZE
  1135. if indexTotal >= count {
  1136. break
  1137. }
  1138. }
  1139. log.Info("user commit code size=" + fmt.Sprint(len(resultMap)))
  1140. return resultMap
  1141. }
  1142. func queryUserModel(start_unix int64, end_unix int64) map[int64]int {
  1143. sess := x.NewSession()
  1144. defer sess.Close()
  1145. resultMap := make(map[int64]int)
  1146. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1147. count, err := sess.Where(cond).Count(new(AiModelManage))
  1148. if err != nil {
  1149. log.Info("query AiModelManage error. return.")
  1150. return resultMap
  1151. }
  1152. var indexTotal int64
  1153. indexTotal = 0
  1154. for {
  1155. sess.Select("id,user_id").Table("ai_model_manage").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1156. aiModelList := make([]*AiModelManage, 0)
  1157. sess.Find(&aiModelList)
  1158. log.Info("query AiModelManage size=" + fmt.Sprint(len(aiModelList)))
  1159. for _, aiModelRecord := range aiModelList {
  1160. if _, ok := resultMap[aiModelRecord.UserId]; !ok {
  1161. resultMap[aiModelRecord.UserId] = 1
  1162. } else {
  1163. resultMap[aiModelRecord.UserId] += 1
  1164. }
  1165. }
  1166. indexTotal += PAGE_SIZE
  1167. if indexTotal >= count {
  1168. break
  1169. }
  1170. }
  1171. return resultMap
  1172. }
  1173. func queryCloudBrainTask(start_unix int64, end_unix int64) (map[int64]int, map[string]int) {
  1174. sess := x.NewSession()
  1175. defer sess.Close()
  1176. resultMap := make(map[int64]int)
  1177. resultItemMap := make(map[string]int)
  1178. cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
  1179. count, err := sess.Where(cond).Count(new(Cloudbrain))
  1180. if err != nil {
  1181. log.Info("query cloudbrain error. return.")
  1182. return resultMap, resultItemMap
  1183. }
  1184. var indexTotal int64
  1185. indexTotal = 0
  1186. for {
  1187. sess.Select("id,job_type,user_id,duration,train_job_duration,type").Table("cloudbrain").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
  1188. cloudTaskList := make([]*Cloudbrain, 0)
  1189. sess.Find(&cloudTaskList)
  1190. log.Info("query cloudbrain size=" + fmt.Sprint(len(cloudTaskList)))
  1191. for _, cloudTaskRecord := range cloudTaskList {
  1192. if _, ok := resultMap[cloudTaskRecord.UserID]; !ok {
  1193. resultMap[cloudTaskRecord.UserID] = 1
  1194. } else {
  1195. resultMap[cloudTaskRecord.UserID] += 1
  1196. }
  1197. setMapKey("CloudBrainRunTime", cloudTaskRecord.UserID, int(cloudTaskRecord.Duration), resultItemMap)
  1198. if cloudTaskRecord.Type == 1 { //npu
  1199. if cloudTaskRecord.JobType == "TRAIN" {
  1200. setMapKey("NpuTrainJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1201. } else if cloudTaskRecord.JobType == "INFERENCE" {
  1202. setMapKey("NpuInferenceJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1203. } else {
  1204. setMapKey("NpuDebugJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1205. }
  1206. } else { //type=0 gpu
  1207. if cloudTaskRecord.JobType == "TRAIN" {
  1208. setMapKey("GpuTrainJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1209. } else if cloudTaskRecord.JobType == "BENCHMARK" {
  1210. setMapKey("GpuBenchMarkJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1211. } else {
  1212. setMapKey("GpuDebugJob", cloudTaskRecord.UserID, 1, resultItemMap)
  1213. }
  1214. }
  1215. }
  1216. indexTotal += PAGE_SIZE
  1217. if indexTotal >= count {
  1218. break
  1219. }
  1220. }
  1221. return resultMap, resultItemMap
  1222. }
  1223. func setMapKey(key string, userId int64, value int, resultItemMap map[string]int) {
  1224. newKey := fmt.Sprint(userId) + "_" + key
  1225. if _, ok := resultItemMap[newKey]; !ok {
  1226. resultItemMap[newKey] = value
  1227. } else {
  1228. resultItemMap[newKey] += value
  1229. }
  1230. }
  1231. func subMonth(t1, t2 time.Time) (month int) {
  1232. y1 := t1.Year()
  1233. y2 := t2.Year()
  1234. m1 := int(t1.Month())
  1235. m2 := int(t2.Month())
  1236. d1 := t1.Day()
  1237. d2 := t2.Day()
  1238. yearInterval := y1 - y2
  1239. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  1240. if m1 < m2 || m1 == m2 && d1 < d2 {
  1241. yearInterval--
  1242. }
  1243. // 获取月数差值
  1244. monthInterval := (m1 + 12) - m2
  1245. if d1 < d2 {
  1246. monthInterval--
  1247. }
  1248. monthInterval %= 12
  1249. month = yearInterval*12 + monthInterval
  1250. if month == 0 {
  1251. month = 1
  1252. }
  1253. return month
  1254. }