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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package repo
  2. import (
  3. "net/http"
  4. "strings"
  5. "time"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. cloudbrainService "code.gitea.io/gitea/services/cloudbrain"
  11. )
  12. func CloudbrainDurationStatisticHour() {
  13. var statisticTime time.Time
  14. var count int64
  15. recordDurationUpdateTime, err := models.GetDurationRecordUpdateTime()
  16. if err != nil {
  17. log.Error("Can not get GetDurationRecordBeginTime", err)
  18. }
  19. now := time.Now()
  20. currentTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
  21. if err == nil && len(recordDurationUpdateTime) > 0 {
  22. statisticTime = time.Unix(int64(recordDurationUpdateTime[0].DateTime), 0).Add(+1 * time.Hour)
  23. } else {
  24. statisticTime = currentTime
  25. }
  26. err = models.DeleteCloudbrainDurationStatistic(timeutil.TimeStamp(statisticTime.Unix()), timeutil.TimeStamp(currentTime.Unix()))
  27. if err != nil {
  28. log.Error("DeleteCloudbrainDurationStatistic failed", err)
  29. }
  30. for statisticTime.Before(currentTime) || statisticTime.Equal(currentTime) {
  31. countEach := summaryDurationStat(statisticTime)
  32. count += countEach
  33. statisticTime = statisticTime.Add(+1 * time.Hour)
  34. }
  35. log.Info("summaryDurationStat count: %v", count)
  36. }
  37. func UpdateDurationStatisticHistoryData(beginTime time.Time, endTime time.Time) int64 {
  38. var count int64
  39. statisticTime := beginTime
  40. currentTime := endTime
  41. for statisticTime.Before(currentTime) || statisticTime.Equal(currentTime) {
  42. countEach := summaryDurationStat(statisticTime)
  43. count += countEach
  44. statisticTime = statisticTime.Add(+1 * time.Hour)
  45. }
  46. return count
  47. }
  48. //statisticTime是当前的时辰,比如当前是2019-01-01 12:01:01,那么statisticTime就是2019-01-01 12:00:00
  49. func summaryDurationStat(statisticTime time.Time) int64 {
  50. var count int64
  51. dateTime := timeutil.TimeStamp(statisticTime.Add(-1 * time.Hour).Unix())
  52. beginTime := statisticTime.Add(-1 * time.Hour).Unix()
  53. dayTime := statisticTime.Add(-1 * time.Hour).Format("2006-01-02")
  54. hourTime := statisticTime.Add(-1 * time.Hour).Hour()
  55. endTime := statisticTime.Unix()
  56. ciTasks, err := models.GetCloudbrainByTime(beginTime, endTime)
  57. if err != nil {
  58. log.Info("GetCloudbrainByTime err: %v", err)
  59. return 0
  60. }
  61. cloudbrainMap := make(map[string]*models.Cloudbrain)
  62. models.LoadSpecs4CloudbrainInfo(ciTasks)
  63. for _, cloudbrain := range ciTasks {
  64. if cloudbrain.Cloudbrain.StartTime == 0 {
  65. cloudbrain.Cloudbrain.StartTime = cloudbrain.Cloudbrain.CreatedUnix
  66. }
  67. if cloudbrain.Cloudbrain.EndTime == 0 {
  68. cloudbrain.Cloudbrain.EndTime = cloudbrain.Cloudbrain.UpdatedUnix
  69. }
  70. cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain)
  71. if cloudbrain.Cloudbrain.Spec != nil {
  72. if _, ok := cloudbrainMap[cloudbrain.Cloudbrain.AiCenter+"/"+cloudbrain.Cloudbrain.Spec.AccCardType]; !ok {
  73. if cloudbrain.Cloudbrain.Spec != nil {
  74. cloudbrainMap[cloudbrain.Cloudbrain.AiCenter+"/"+cloudbrain.Cloudbrain.Spec.AccCardType] = &cloudbrain.Cloudbrain
  75. }
  76. }
  77. }
  78. }
  79. cloudBrainCenterCodeAndCardTypeInfo := getcloudBrainCenterCodeAndCardTypeInfo(ciTasks, beginTime, endTime)
  80. resourceQueues, err := models.GetCanUseCardInfo()
  81. if err != nil {
  82. log.Info("GetCanUseCardInfo err: %v", err)
  83. return 0
  84. }
  85. cardsTotalDurationMap := make(map[string]int)
  86. for _, resourceQueue := range resourceQueues {
  87. if _, ok := cardsTotalDurationMap[resourceQueue.Cluster+"/"+resourceQueue.AiCenterCode+"/"+resourceQueue.AccCardType]; !ok {
  88. cardsTotalDurationMap[resourceQueue.Cluster+"/"+resourceQueue.AiCenterCode+"/"+resourceQueue.AccCardType] = resourceQueue.CardsTotalNum * 1 * 60 * 60
  89. } else {
  90. cardsTotalDurationMap[resourceQueue.Cluster+"/"+resourceQueue.AiCenterCode+"/"+resourceQueue.AccCardType] += resourceQueue.CardsTotalNum * 1 * 60 * 60
  91. }
  92. }
  93. for centerCode, CardTypes := range cloudBrainCenterCodeAndCardTypeInfo {
  94. for cardType, cardDuration := range CardTypes {
  95. cloudbrainTable := cloudbrainMap[centerCode+"/"+cardType]
  96. if cloudbrainTable != nil {
  97. if _, ok := cardsTotalDurationMap[cloudbrainTable.Cluster+"/"+centerCode+"/"+cardType]; !ok {
  98. cardsTotalDurationMap[cloudbrainTable.Cluster+"/"+centerCode+"/"+cardType] = 0
  99. }
  100. cloudbrainDurationStat := models.CloudbrainDurationStatistic{
  101. DateTime: dateTime,
  102. DayTime: dayTime,
  103. HourTime: hourTime,
  104. Cluster: cloudbrainTable.Cluster,
  105. AiCenterName: GetAiCenterNameByCode(centerCode, "zh-CN"),
  106. AiCenterCode: centerCode,
  107. AccCardType: cardType,
  108. CardsUseDuration: cardDuration,
  109. CardsTotalDuration: cardsTotalDurationMap[cloudbrainTable.Cluster+"/"+centerCode+"/"+cardType],
  110. CreatedUnix: timeutil.TimeStampNow(),
  111. }
  112. if _, err = models.InsertCloudbrainDurationStatistic(&cloudbrainDurationStat); err != nil {
  113. log.Error("Insert cloudbrainDurationStat failed: %v", err.Error())
  114. }
  115. count++
  116. delete(cardsTotalDurationMap, cloudbrainTable.Cluster+"/"+centerCode+"/"+cardType)
  117. }
  118. }
  119. }
  120. for key, cardsTotalDuration := range cardsTotalDurationMap {
  121. cloudbrainDurationStat := models.CloudbrainDurationStatistic{
  122. DateTime: dateTime,
  123. DayTime: dayTime,
  124. HourTime: hourTime,
  125. Cluster: strings.Split(key, "/")[0],
  126. AiCenterName: GetAiCenterNameByCode(strings.Split(key, "/")[1], "zh-CN"),
  127. AiCenterCode: strings.Split(key, "/")[1],
  128. AccCardType: strings.Split(key, "/")[2],
  129. CardsUseDuration: 0,
  130. CardsTotalDuration: cardsTotalDuration,
  131. CardsTotalNum: cardsTotalDuration / 1 / 60 / 60,
  132. CreatedUnix: timeutil.TimeStampNow(),
  133. }
  134. if _, err = models.InsertCloudbrainDurationStatistic(&cloudbrainDurationStat); err != nil {
  135. log.Error("Insert cloudbrainDurationStat failed: %v", err.Error())
  136. }
  137. count++
  138. }
  139. log.Info("finish summary cloudbrainDurationStat")
  140. return count
  141. }
  142. func GetAiCenterNameByCode(centerCode string, language string) string {
  143. var aiCenterName string
  144. aiCenterInfo := cloudbrainService.GetAiCenterInfoByCenterCode(centerCode)
  145. if aiCenterInfo != nil {
  146. if language == "zh-CN" {
  147. aiCenterName = aiCenterInfo.Content
  148. } else {
  149. aiCenterName = aiCenterInfo.ContentEN
  150. }
  151. } else {
  152. aiCenterName = centerCode
  153. }
  154. return aiCenterName
  155. }
  156. func getcloudBrainCenterCodeAndCardTypeInfo(ciTasks []*models.CloudbrainInfo, beginTime int64, endTime int64) map[string]map[string]int {
  157. var WorkServerNumber int
  158. var AccCardsNum int
  159. cloudBrainCenterCodeAndCardType := make(map[string]map[string]int)
  160. for _, cloudbrain := range ciTasks {
  161. cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain)
  162. if cloudbrain.Cloudbrain.StartTime == 0 {
  163. cloudbrain.Cloudbrain.StartTime = cloudbrain.Cloudbrain.CreatedUnix
  164. }
  165. if cloudbrain.Cloudbrain.EndTime == 0 {
  166. cloudbrain.Cloudbrain.EndTime = cloudbrain.Cloudbrain.UpdatedUnix
  167. }
  168. if cloudbrain.Cloudbrain.WorkServerNumber >= 1 {
  169. WorkServerNumber = cloudbrain.Cloudbrain.WorkServerNumber
  170. } else {
  171. WorkServerNumber = 1
  172. }
  173. if cloudbrain.Cloudbrain.Spec == nil {
  174. AccCardsNum = 1
  175. } else {
  176. AccCardsNum = cloudbrain.Cloudbrain.Spec.AccCardsNum
  177. }
  178. if _, ok := cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter]; !ok {
  179. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter] = make(map[string]int)
  180. }
  181. if cloudbrain.Cloudbrain.Spec != nil {
  182. if cloudbrain.Cloudbrain.Status == string(models.ModelArtsRunning) {
  183. if _, ok := cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType]; !ok {
  184. if int64(cloudbrain.Cloudbrain.StartTime) < beginTime {
  185. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  186. } else if beginTime <= int64(cloudbrain.Cloudbrain.StartTime) && int64(cloudbrain.Cloudbrain.StartTime) < endTime {
  187. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  188. } else if int64(cloudbrain.Cloudbrain.StartTime) >= endTime {
  189. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = 0
  190. }
  191. } else {
  192. if int64(cloudbrain.Cloudbrain.StartTime) < beginTime {
  193. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  194. } else if beginTime <= int64(cloudbrain.Cloudbrain.StartTime) && int64(cloudbrain.Cloudbrain.StartTime) < endTime {
  195. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  196. } else if int64(cloudbrain.Cloudbrain.StartTime) >= endTime {
  197. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += 0
  198. }
  199. }
  200. } else {
  201. if _, ok := cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType]; !ok {
  202. if int64(cloudbrain.Cloudbrain.StartTime) <= beginTime && int64(cloudbrain.Cloudbrain.EndTime) <= endTime {
  203. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(beginTime))
  204. } else if int64(cloudbrain.Cloudbrain.StartTime) <= beginTime && int64(cloudbrain.Cloudbrain.EndTime) > endTime {
  205. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  206. } else if beginTime <= int64(cloudbrain.Cloudbrain.StartTime) && int64(cloudbrain.Cloudbrain.StartTime) <= endTime && int64(cloudbrain.Cloudbrain.EndTime) <= endTime {
  207. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(cloudbrain.Cloudbrain.StartTime))
  208. } else if beginTime <= int64(cloudbrain.Cloudbrain.StartTime) && int64(cloudbrain.Cloudbrain.StartTime) <= endTime && int64(cloudbrain.Cloudbrain.EndTime) > endTime {
  209. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  210. }
  211. } else {
  212. if int64(cloudbrain.Cloudbrain.StartTime) <= beginTime && int64(cloudbrain.Cloudbrain.EndTime) <= endTime {
  213. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(beginTime))
  214. } else if int64(cloudbrain.Cloudbrain.StartTime) <= beginTime && int64(cloudbrain.Cloudbrain.EndTime) > endTime {
  215. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime))
  216. } else if beginTime <= int64(cloudbrain.Cloudbrain.StartTime) && int64(cloudbrain.Cloudbrain.StartTime) <= endTime && int64(cloudbrain.Cloudbrain.EndTime) <= endTime {
  217. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(cloudbrain.Cloudbrain.EndTime) - int(cloudbrain.Cloudbrain.StartTime))
  218. } else if beginTime <= int64(cloudbrain.Cloudbrain.StartTime) && int64(cloudbrain.Cloudbrain.StartTime) <= endTime && int64(cloudbrain.Cloudbrain.EndTime) > endTime {
  219. cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] += AccCardsNum * WorkServerNumber * (int(endTime) - int(cloudbrain.Cloudbrain.StartTime))
  220. }
  221. }
  222. }
  223. }
  224. }
  225. return cloudBrainCenterCodeAndCardType
  226. }
  227. func CloudbrainUpdateHistoryData(ctx *context.Context) {
  228. beginTimeStr := ctx.QueryTrim("beginTime")
  229. endTimeStr := ctx.QueryTrim("endTime")
  230. var count int64
  231. var err error
  232. if beginTimeStr != "" && endTimeStr != "" {
  233. beginTime, _ := time.ParseInLocation("2006-01-02 15:04:05", beginTimeStr, time.Local)
  234. endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endTimeStr, time.Local)
  235. if time.Now().Before(endTime) {
  236. endTime = time.Now()
  237. }
  238. beginTimeUnix := timeutil.TimeStamp(beginTime.Unix())
  239. endTimeUnix := timeutil.TimeStamp(endTime.Unix())
  240. err = models.DeleteCloudbrainDurationStatistic(beginTimeUnix, endTimeUnix)
  241. count = UpdateDurationStatisticHistoryData(beginTime.Add(+1*time.Hour), endTime)
  242. }
  243. ctx.JSON(http.StatusOK, map[string]interface{}{
  244. "message": 0,
  245. "count": count,
  246. "err": err,
  247. })
  248. }