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