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.

cloudbrain_statistic.go 8.5 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package repo
  2. import (
  3. "time"
  4. "code.gitea.io/gitea/models"
  5. "code.gitea.io/gitea/modules/context"
  6. "code.gitea.io/gitea/modules/log"
  7. )
  8. func CloudbrainDurationStatistic() {
  9. log.Info("Generate Cloudbrain Duration statistic begin")
  10. // yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  11. // CloudbrainDurationStatisticHour(yesterday)
  12. log.Info("Generate Cloudbrain Duration statistic end")
  13. }
  14. func CloudbrainDurationStatisticHour(ctx *context.Context) {
  15. //获取规定时间段的云脑任务列表
  16. // page := ctx.QueryInt("page")
  17. // pageSize := ctx.QueryInt("pagesize")
  18. // if page <= 0 {
  19. // page = 1
  20. // }
  21. // if pageSize <= 0 {
  22. // pageSize = 10
  23. // }
  24. // cloudBrainDurationRes := make(map[string]map[string]int)
  25. // cloudBrainOneCardRes := make(map[string]int)
  26. // cloudBrainTwoCardRes := make(map[string]int)
  27. // c2NetCardRes := make(map[string]int)
  28. // cDNetCenterCardRes := make(map[string]int)
  29. // var WorkServerNumber int
  30. // var AccCardsNum int
  31. // endTime := time.Now().Unix()
  32. // beginTime := time.Now().AddDate(0, 0, -1).Unix()
  33. // hour := time.Now().Hour()
  34. // tStr := time.Now().Format("2006-01-02 15:04:05")
  35. currentTime := time.Now()
  36. m, _ := time.ParseDuration("-1h")
  37. beginTime := currentTime.Add(m).Unix()
  38. endTime := currentTime.Unix()
  39. // fmt.Println(beginTime)
  40. ciTasks1, err := models.GetCloudbrainRunning()
  41. if err != nil {
  42. // ctx.ServerError("Get job failed:", err)
  43. log.Info("GetCloudbrainRunning err: %v", err)
  44. return
  45. }
  46. ciTasks2, err := models.GetCloudbrainCompleteByTime(beginTime, endTime)
  47. ciTasks := append(ciTasks1, ciTasks2...)
  48. log.Info("beginTime: %s", beginTime)
  49. log.Info("endTime: %s", endTime)
  50. if err != nil {
  51. // ctx.ServerError("Get job failed:", err)
  52. log.Info("GetCloudbrainCompleteByTime err: %v", err)
  53. return
  54. }
  55. models.LoadSpecs4CloudbrainInfo(ciTasks)
  56. log.Info("ciTasks here: %s", ciTasks)
  57. log.Info("count here: %s", len(ciTasks))
  58. cloudBrainCardRes := getCloudBrainCardRes(ciTasks, beginTime, endTime)
  59. //根据云脑任务列表获取云脑任务已使用的卡时,并区分是哪个智算中心,哪个卡类型的卡时,将这些信息存入新表
  60. // cloudbrainDurationStat := models.CloudbrainDurationStatistic{
  61. // DateTime: date,
  62. // HourTime: userNumber,
  63. // Cluster: repositorySize,
  64. // AiCenterName: allDatasetSize,
  65. // AiCenterCode: organizationNumber,
  66. // ComputeResource: repositoryNumer,
  67. // AccCardType: forkRepositoryNumber,
  68. // CardsTotalNum: mirrorRepositoryNumber,
  69. // CardsTotalDuration: privateRepositoryNumer,
  70. // QueueCode: publicRepositoryNumer,
  71. // CreatedTime: privateRepositoryNumer,
  72. // UpdatedTime: publicRepositoryNumer,
  73. // }
  74. // if _, err = models.InsertCloudbrainDurationStatistic(&cloudbrainDurationStat); err != nil {
  75. // log.Error("Insert cloudbrainDurationStat failed: %v", err.Error())
  76. // }
  77. // log.Info("cloudBrainDurationRes2: %s", cloudBrainDurationRes)
  78. // cloudBrainDurationRes = append(cloudBrainDurationRes, cloudBrainOneCardRes)
  79. // log.Info("cloudBrainDurationRes: %s", cloudBrainDurationRes)
  80. // log.Info("cloudBrainOneCardRes: %s", cloudBrainOneCardRes)
  81. log.Info("cloudBrainCardRes: %s", cloudBrainCardRes)
  82. // log.Info("c2NetCardRes: %s", c2NetCardRes)
  83. log.Info("finish summary cloudbrainDurationStat")
  84. }
  85. func getCloudBrainCardRes(ciTasks []*models.CloudbrainInfo, beginTime int64, endTime int64) map[string]map[string]int {
  86. var WorkServerNumber int
  87. var AccCardsNum int
  88. // cloudBrainCardRes := make(map[string]int)
  89. cloudBrainAicenterNameList := make(map[string]string)
  90. cloudBrainCardTypeList := make(map[string]string)
  91. cloudBrainCenterNameAndCardType := make(map[string]map[string]int)
  92. // var cloudbrainDurationInfo models.CloudbrainDurationInfo
  93. for _, cloudbrain := range ciTasks {
  94. if _, ok := cloudBrainAicenterNameList[cloudbrain.Cloudbrain.Spec.AiCenterName]; !ok {
  95. cloudBrainAicenterNameList[cloudbrain.Cloudbrain.Spec.AiCenterName] = cloudbrain.Cloudbrain.Spec.AiCenterName
  96. }
  97. if cloudbrain.Cloudbrain.StartTime == 0 {
  98. cloudbrain.Cloudbrain.StartTime = cloudbrain.Cloudbrain.CreatedUnix
  99. }
  100. if cloudbrain.Cloudbrain.EndTime == 0 {
  101. cloudbrain.Cloudbrain.EndTime = cloudbrain.Cloudbrain.UpdatedUnix
  102. }
  103. if cloudbrain.Cloudbrain.WorkServerNumber >= 1 {
  104. WorkServerNumber = cloudbrain.Cloudbrain.WorkServerNumber
  105. } else {
  106. WorkServerNumber = 1
  107. }
  108. if cloudbrain.Cloudbrain.Spec == nil {
  109. AccCardsNum = 1
  110. } else {
  111. AccCardsNum = cloudbrain.Cloudbrain.Spec.AccCardsNum
  112. }
  113. for k, _ := range cloudBrainAicenterNameList {
  114. if cloudbrain.Cloudbrain.Spec.AiCenterName == cloudBrainAicenterNameList[k] {
  115. if _, ok := cloudBrainCardTypeList[cloudbrain.Cloudbrain.Spec.AccCardType]; !ok {
  116. cloudBrainCardTypeList[cloudbrain.Cloudbrain.Spec.AccCardType] = cloudbrain.Cloudbrain.Spec.AccCardType
  117. }
  118. for i, _ := range cloudBrainCardTypeList {
  119. if cloudBrainCenterNameAndCardType == nil {
  120. cloudBrainCenterNameAndCardType[cloudBrainAicenterNameList[k]][cloudBrainCardTypeList[i]] = 0
  121. }
  122. if cloudbrain.Cloudbrain.Spec.AccCardType == cloudBrainCardTypeList[i] {
  123. if cloudbrain.Cloudbrain.Status == string(models.ModelArtsRunning) {
  124. if int64(cloudbrain.Cloudbrain.StartTime) < beginTime {
  125. cloudBrainCenterNameAndCardType[cloudBrainAicenterNameList[k]][cloudBrainCardTypeList[i]] += AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  126. } else {
  127. cloudBrainCenterNameAndCardType[cloudBrainAicenterNameList[k]][cloudBrainCardTypeList[i]] += AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  128. }
  129. } else {
  130. if int64(cloudbrain.Cloudbrain.StartTime) < beginTime && int64(cloudbrain.Cloudbrain.EndTime) < endTime {
  131. cloudBrainCenterNameAndCardType[cloudBrainAicenterNameList[k]][cloudBrainCardTypeList[i]] += AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(beginTime))
  132. } else {
  133. cloudBrainCenterNameAndCardType[cloudBrainAicenterNameList[k]][cloudBrainCardTypeList[i]] += AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(cloudbrain.Cloudbrain.StartTime))
  134. }
  135. }
  136. }
  137. }
  138. // cloudBrainCenterNameAndCardType[cloudBrainAicenterNameList[k]][cloudBrainCardTypeList[i]] = cloudBrainCardRes[cloudBrainCardTypeList[i]]
  139. // if cloudbrain.Cloudbrain.Status == string(models.ModelArtsRunning) {
  140. // if _, ok := cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType]; ok {
  141. // if int64(cloudbrain.Cloudbrain.StartTime) < beginTime {
  142. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  143. // } else {
  144. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  145. // }
  146. // } else {
  147. // if int64(cloudbrain.Cloudbrain.StartTime) < beginTime {
  148. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  149. // } else {
  150. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  151. // }
  152. // }
  153. // } else {
  154. // if _, ok := cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType]; ok {
  155. // if int64(cloudbrain.Cloudbrain.StartTime) < beginTime && int64(cloudbrain.Cloudbrain.EndTime) < endTime {
  156. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(beginTime))
  157. // } else {
  158. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(cloudbrain.Cloudbrain.StartTime))
  159. // }
  160. // } else {
  161. // if int64(cloudbrain.Cloudbrain.StartTime) < beginTime && int64(cloudbrain.Cloudbrain.EndTime) < endTime {
  162. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(beginTime))
  163. // } else {
  164. // cloudBrainCardRes[cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(cloudbrain.Cloudbrain.StartTime))
  165. // }
  166. // }
  167. // }
  168. }
  169. }
  170. }
  171. return cloudBrainCenterNameAndCardType
  172. }