diff --git a/models/cloudbrain.go b/models/cloudbrain.go index cdd9698fe..1e7c702ab 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -2344,6 +2344,61 @@ func CloudbrainAllStatic(opts *CloudbrainsOptions) ([]*CloudbrainInfo, int64, er return cloudbrains, count, nil } +func CloudbrainAllKanBan(opts *CloudbrainsOptions) ([]*CloudbrainInfo, int64, error) { + sess := x.NewSession() + defer sess.Close() + + var cond = builder.NewCond() + + if (opts.Type) >= 0 { + cond = cond.And( + builder.Eq{"cloudbrain.type": opts.Type}, + ) + } + if opts.BeginTimeUnix > 0 && opts.EndTimeUnix > 0 { + cond = cond.And( + builder.And(builder.Gte{"cloudbrain.created_unix": opts.BeginTimeUnix}, builder.Lte{"cloudbrain.created_unix": opts.EndTimeUnix}), + ) + } + var count int64 + var err error + count, err = sess.Unscoped().Where(cond).Count(new(Cloudbrain)) + + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } + + if opts.Page >= 0 && opts.PageSize > 0 { + var start int + if opts.Page == 0 { + start = 0 + } else { + start = (opts.Page - 1) * opts.PageSize + } + sess.Limit(opts.PageSize, start) + } + // sess.OrderBy("cloudbrain.created_unix DESC") + cloudbrains := make([]*CloudbrainInfo, 0, setting.UI.IssuePagingNum) + if err := sess.Cols("id", "type", "work_server_number", "duration", "train_job_duration", "ai_center", "cluster").Table(&Cloudbrain{}).Unscoped().Where(cond). + Find(&cloudbrains); err != nil { + return nil, 0, fmt.Errorf("Find: %v", err) + } + if opts.NeedRepoInfo { + var ids []int64 + for _, task := range cloudbrains { + ids = append(ids, task.RepoID) + } + repositoryMap, err := GetRepositoriesMapByIDs(ids) + if err == nil { + for _, task := range cloudbrains { + task.Repo = repositoryMap[task.RepoID] + } + } + + } + return cloudbrains, count, nil +} + func GetStartedCloudbrainTaskByUpdatedUnix(startTime, endTime time.Time) ([]Cloudbrain, error) { r := make([]Cloudbrain, 0) err := x.Where("updated_unix >= ? and updated_unix <= ? and start_time > 0", startTime.Unix(), endTime.Unix()).Unscoped().Find(&r) diff --git a/models/cloudbrain_static.go b/models/cloudbrain_static.go index a7678b267..a213f179c 100644 --- a/models/cloudbrain_static.go +++ b/models/cloudbrain_static.go @@ -46,7 +46,8 @@ type CloudbrainDurationStatistic struct { ComputeResource string AccCardType string `xorm:"INDEX"` - DateTime timeutil.TimeStamp `xorm:"INDEX"` + DateTime timeutil.TimeStamp `xorm:"INDEX DEFAULT 0"` + DateTimeUnix timeutil.TimeStamp `xorm:"INDEX DEFAULT 0"` DayTime string `xorm:"INDEX"` HourTime int `xorm:"INDEX"` CardsUseDuration int @@ -274,7 +275,6 @@ func GetCloudbrainByTime(beginTime int64, endTime int64) ([]*CloudbrainInfo, err sess := x.NewSession() defer sess.Close() var cond = builder.NewCond() - sess.Exec("if ") cond = cond.Or( builder.And(builder.Gte{"cloudbrain.end_time": beginTime}, builder.Lte{"cloudbrain.start_time": beginTime}, builder.Gt{"cloudbrain.start_time": 0}), ) @@ -284,11 +284,12 @@ func GetCloudbrainByTime(beginTime int64, endTime int64) ([]*CloudbrainInfo, err cond = cond.Or( builder.And(builder.Eq{"cloudbrain.status": string(JobRunning)}), ) - sess.OrderBy("cloudbrain.created_unix ASC") + sess.OrderBy("cloudbrain.id ASC") cloudbrains := make([]*CloudbrainInfo, 0, 10) if err := sess.Table(&Cloudbrain{}).Unscoped().Where(cond). Find(&cloudbrains); err != nil { - log.Info("find error.") + log.Error("find error.") + return nil, err } return cloudbrains, nil } @@ -303,7 +304,8 @@ func GetSpecByAiCenterCodeAndType(aiCenterCode string, accCardType string) ([]*C cloudbrainSpecs := make([]*CloudbrainSpec, 0, 10) if err := sess.Table(&CloudbrainSpec{}).Where(cond). Find(&cloudbrainSpecs); err != nil { - log.Info("find error.") + log.Error("find error.") + return nil, err } return cloudbrainSpecs, nil } @@ -328,7 +330,8 @@ func GetCanUseCardInfo() ([]*ResourceQueue, error) { sess.OrderBy("resource_queue.cluster DESC, resource_queue.ai_center_code ASC") ResourceQueues := make([]*ResourceQueue, 0, 10) if err := sess.Table(&ResourceQueue{}).Find(&ResourceQueues); err != nil { - log.Info("find error.") + log.Error("find error.") + return nil, err } return ResourceQueues, nil } @@ -339,7 +342,7 @@ func GetCardDurationStatistics(opts *DurationStatisticOptions) ([]*CloudbrainDur var cond = builder.NewCond() if opts.BeginTime.Unix() > 0 && opts.EndTime.Unix() > 0 { cond = cond.And( - builder.And(builder.Gte{"cloudbrain_duration_statistic.date_time": opts.BeginTime.Unix()}, builder.Lt{"cloudbrain_duration_statistic.date_time": opts.EndTime.Unix()}), + builder.And(builder.Gte{"cloudbrain_duration_statistic.date_time_unix": opts.BeginTime.Unix()}, builder.Lt{"cloudbrain_duration_statistic.date_time_unix": opts.EndTime.Unix()}), ) } if opts.AiCenterCode != "" { @@ -350,7 +353,8 @@ func GetCardDurationStatistics(opts *DurationStatisticOptions) ([]*CloudbrainDur CloudbrainDurationStatistics := make([]*CloudbrainDurationStatistic, 0, 10) if err := sess.Table(&CloudbrainDurationStatistic{}).Where(cond). Find(&CloudbrainDurationStatistics); err != nil { - log.Info("find error.") + log.Error("find error.") + return nil, err } return CloudbrainDurationStatistics, nil } @@ -358,10 +362,18 @@ func GetCardDurationStatistics(opts *DurationStatisticOptions) ([]*CloudbrainDur func GetDurationRecordBeginTime() ([]*CloudbrainDurationStatistic, error) { sess := xStatistic.NewSession() defer sess.Close() - sess.OrderBy("cloudbrain_duration_statistic.date_time ASC limit 1") + + var cond = builder.NewCond() + + cond = cond.And( + builder.Gt{"cloudbrain_duration_statistic.date_time_unix": 0}, + ) + + sess.OrderBy("cloudbrain_duration_statistic.date_time_unix ASC limit 1") CloudbrainDurationStatistics := make([]*CloudbrainDurationStatistic, 0) - if err := sess.Table(&CloudbrainDurationStatistic{}).Find(&CloudbrainDurationStatistics); err != nil { - log.Info("find error.") + if err := sess.Table(&CloudbrainDurationStatistic{}).Where(cond).Find(&CloudbrainDurationStatistics); err != nil { + log.Error("find error.") + return nil, err } return CloudbrainDurationStatistics, nil } @@ -369,10 +381,16 @@ func GetDurationRecordBeginTime() ([]*CloudbrainDurationStatistic, error) { func GetDurationRecordUpdateTime() ([]*CloudbrainDurationStatistic, error) { sess := xStatistic.NewSession() defer sess.Close() - sess.OrderBy("cloudbrain_duration_statistic.date_time DESC limit 1") + var cond = builder.NewCond() + + cond = cond.And( + builder.Gt{"cloudbrain_duration_statistic.date_time_unix": 1577808000}, + ) + sess.OrderBy("cloudbrain_duration_statistic.date_time_unix DESC limit 1") CloudbrainDurationStatistics := make([]*CloudbrainDurationStatistic, 0) - if err := sess.Table(&CloudbrainDurationStatistic{}).Find(&CloudbrainDurationStatistics); err != nil { - log.Info("find error.") + if err := sess.Table(&CloudbrainDurationStatistic{}).Where(cond).Find(&CloudbrainDurationStatistics); err != nil { + log.Error("find error.") + return nil, err } return CloudbrainDurationStatistics, nil } @@ -380,8 +398,8 @@ func GetDurationRecordUpdateTime() ([]*CloudbrainDurationStatistic, error) { func DeleteCloudbrainDurationStatistic(beginTime timeutil.TimeStamp, endTime timeutil.TimeStamp) error { sess := xStatistic.NewSession() defer sess.Close() - if _, err := sess.Exec("DELETE FROM cloudbrain_duration_statistic WHERE cloudbrain_duration_statistic.date_time BETWEEN ? AND ?", beginTime, endTime); err != nil { - log.Info("DELETE cloudbrain_duration_statistic data error.") + if _, err := sess.Exec("DELETE FROM cloudbrain_duration_statistic WHERE cloudbrain_duration_statistic.date_time_unix BETWEEN ? AND ?", beginTime, endTime); err != nil { + log.Error("DELETE cloudbrain_duration_statistic data error.") return err } return nil diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 374e6a99d..ee3dfd90c 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -584,6 +584,8 @@ var ( TrainJobFLAVORINFOS string ModelArtsSpecialPools string ModelArtsMultiNode string + //kanban + IsCloudbrainTimingEnabled bool // modelarts-cd config ModelartsCD = struct { @@ -1609,6 +1611,9 @@ func NewContext() { FileNoteBook.ImageNPUDescription = sec.Key("image_npu_desc").MustString("") FileNoteBook.ImageNPUCDDescription = sec.Key("image_npu_cd_desc").MustString("") + sec = Cfg.Section("kanban") + IsCloudbrainTimingEnabled = sec.Key("ENABLED").MustBool(false) + getGrampusConfig() getModelartsCDConfig() getModelConvertConfig() diff --git a/routers/api/v1/repo/cloudbrain_dashboard.go b/routers/api/v1/repo/cloudbrain_dashboard.go index 09e65b007..84154cd95 100755 --- a/routers/api/v1/repo/cloudbrain_dashboard.go +++ b/routers/api/v1/repo/cloudbrain_dashboard.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/routers/repo" cloudbrainService "code.gitea.io/gitea/services/cloudbrain" "code.gitea.io/gitea/services/cloudbrain/resource" @@ -58,42 +59,30 @@ func GetAllCloudbrainsOverview(ctx *context.Context) { return } cloudbrainTypeCount, err := models.GetCloudbrainTypeCount() - log.Info("cloudbrainTypeCount:", cloudbrainTypeCount) if err != nil { log.Error("Can not query cloudbrainTypeCount.", err) return } - cloudbrainTpyeDurationSum, err := models.GetCloudbrainTpyeDurationSum() - log.Info("cloudbrainTpyeDurationSum:", cloudbrainTpyeDurationSum) - if err != nil { - log.Error("Can not query cloudbrainTpyeDurationSum.", err) - return - } - todayCloudbrainCount, err := models.GetTodayCloudbrainCount(beginTime, endTime) - log.Info("todayCloudbrainCount:", todayCloudbrainCount) if err != nil { log.Error("Can not query todayCloudbrainCount.", err) return } todayRunningCount, err := models.GetTodayRunningCount(beginTime, endTime) - log.Info("todayRunningCount:", todayRunningCount) if err != nil { log.Error("Can not query todayRunningCount.", err) return } todayWaitingCount, err := models.GetTodayWaitingCount(beginTime, endTime) - log.Info("todayWaittingCount:", todayWaitingCount) if err != nil { log.Error("Can not query todayWaitingCount.", err) return } todayCompletedCount := todayCloudbrainCount - todayRunningCount - todayWaitingCount - log.Info("todayCompletedCount:", todayCompletedCount) creatorCount, err := models.GetCreatorCount() if err != nil { @@ -139,60 +128,48 @@ func GetOverviewDuration(ctx *context.Context) { c2NetDuration := int64(0) cDCenterDuration := int64(0) - page := 1 - pagesize := 10000 - count := pagesize - // Each time a maximum of 10000 pieces of data are detected to the memory, batch processing - for count == pagesize && count != 0 { - cloudbrains, _, err := models.CloudbrainAllStatic(&models.CloudbrainsOptions{ - ListOptions: models.ListOptions{ - Page: page, - PageSize: pagesize, - }, - Type: models.TypeCloudBrainAll, - BeginTimeUnix: int64(recordBeginTime), - EndTimeUnix: endTime.Unix(), - }) - if err != nil { - ctx.ServerError("Get cloudbrains failed:", err) - return - } - models.LoadSpecs4CloudbrainInfo(cloudbrains) - - for _, cloudbrain := range cloudbrains { - cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain) - if cloudbrain.Cloudbrain.Spec != nil { - cardNum = int64(cloudbrain.Cloudbrain.Spec.AccCardsNum) - } else { - cardNum = 1 - } - if cloudbrain.Cloudbrain.WorkServerNumber >= 1 { - workServerNumber = int64(cloudbrain.Cloudbrain.WorkServerNumber) - } else { - workServerNumber = 1 - } - duration := models.ConvertStrToDuration(cloudbrain.TrainJobDuration) - CardDuration := workServerNumber * int64(cardNum) * duration - - if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne { - cloudBrainOneDuration += duration - cloudBrainOneCardDuSum += CardDuration - } else if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo { - cloudBrainTwoDuration += duration - cloudBrainTwoCardDuSum += CardDuration - } else if cloudbrain.Cloudbrain.Type == models.TypeC2Net { - c2NetDuration += duration - c2NetCardDuSum += CardDuration - } else if cloudbrain.Cloudbrain.Type == models.TypeCDCenter { - cDCenterDuration += duration - cDNetCardDuSum += CardDuration - } + cloudbrains, _, err := models.CloudbrainAllKanBan(&models.CloudbrainsOptions{ + Type: models.TypeCloudBrainAll, + BeginTimeUnix: int64(recordBeginTime), + EndTimeUnix: endTime.Unix(), + }) + if err != nil { + ctx.ServerError("Get cloudbrains failed:", err) + return + } + models.LoadSpecs4CloudbrainInfo(cloudbrains) - durationAllSum += duration - cardDuSum += CardDuration + for _, cloudbrain := range cloudbrains { + cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain) + if cloudbrain.Cloudbrain.Spec != nil { + cardNum = int64(cloudbrain.Cloudbrain.Spec.AccCardsNum) + } else { + cardNum = 1 } - count = len(cloudbrains) - page += 1 + if cloudbrain.Cloudbrain.WorkServerNumber >= 1 { + workServerNumber = int64(cloudbrain.Cloudbrain.WorkServerNumber) + } else { + workServerNumber = 1 + } + duration := models.ConvertStrToDuration(cloudbrain.TrainJobDuration) + CardDuration := workServerNumber * int64(cardNum) * duration + + if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne { + cloudBrainOneDuration += duration + cloudBrainOneCardDuSum += CardDuration + } else if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo { + cloudBrainTwoDuration += duration + cloudBrainTwoCardDuSum += CardDuration + } else if cloudbrain.Cloudbrain.Type == models.TypeC2Net { + c2NetDuration += duration + c2NetCardDuSum += CardDuration + } else if cloudbrain.Cloudbrain.Type == models.TypeCDCenter { + cDCenterDuration += duration + cDNetCardDuSum += CardDuration + } + + durationAllSum += duration + cardDuSum += CardDuration } ctx.JSON(http.StatusOK, map[string]interface{}{ "cloudBrainOneCardDuSum": cloudBrainOneCardDuSum, @@ -1488,12 +1465,17 @@ func getCloudbrainTimePeroid(ctx *context.Context, recordBeginTime time.Time) (t } func GetCloudbrainResourceOverview(ctx *context.Context) { + var recordBeginTime timeutil.TimeStamp recordCloudbrainDuration, err := models.GetDurationRecordBeginTime() if err != nil { log.Error("Can not get GetDurationRecordBeginTime", err) return } - recordBeginTime := recordCloudbrainDuration[0].DateTime + if len(recordCloudbrainDuration) > 0 && err == nil { + recordBeginTime = recordCloudbrainDuration[0].DateTimeUnix + } else { + recordBeginTime = timeutil.TimeStamp(time.Now().Unix()) + } recordUpdateTime := time.Now().Unix() resourceQueues, err := models.GetCanUseCardInfo() if err != nil { @@ -1620,6 +1602,7 @@ func getBeginAndEndTime(ctx *context.Context) (time.Time, time.Time) { now := time.Now() beginTimeStr := ctx.QueryTrim("beginTime") endTimeStr := ctx.QueryTrim("endTime") + var brainRecordBeginTime time.Time var beginTime time.Time var endTime time.Time @@ -1632,7 +1615,12 @@ func getBeginAndEndTime(ctx *context.Context) (time.Time, time.Time) { ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err")) return beginTime, endTime } - brainRecordBeginTime := recordCloudbrainDuration[0].DateTime.AsTime() + if len(recordCloudbrainDuration) > 0 && err == nil { + brainRecordBeginTime = recordCloudbrainDuration[0].DateTimeUnix.AsTime() + } else { + brainRecordBeginTime = now + } + beginTime = brainRecordBeginTime endTime = now } else if queryType == "today" { @@ -1674,7 +1662,11 @@ func getBeginAndEndTime(ctx *context.Context) (time.Time, time.Time) { ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err")) return beginTime, endTime } - brainRecordBeginTime := recordCloudbrainDuration[0].DateTime.AsTime() + if len(recordCloudbrainDuration) > 0 && err == nil { + brainRecordBeginTime = recordCloudbrainDuration[0].DateTimeUnix.AsTime() + } else { + brainRecordBeginTime = now + } beginTime = brainRecordBeginTime endTime = now } else { @@ -1705,7 +1697,7 @@ func getAiCenterUsageDuration(beginTime time.Time, endTime time.Time, cloudbrain usageRate := float64(0) for _, cloudbrainStatistic := range cloudbrainStatistics { - if int64(cloudbrainStatistic.DateTime) >= beginTime.Unix() && int64(cloudbrainStatistic.DateTime) < endTime.Unix() { + if int64(cloudbrainStatistic.DateTimeUnix) >= beginTime.Unix() && int64(cloudbrainStatistic.DateTimeUnix) < endTime.Unix() { totalDuration += cloudbrainStatistic.CardsTotalDuration usageDuration += cloudbrainStatistic.CardsUseDuration } diff --git a/routers/repo/cloudbrain_statistic.go b/routers/repo/cloudbrain_statistic.go index 2ec12cd41..2c63ea70c 100644 --- a/routers/repo/cloudbrain_statistic.go +++ b/routers/repo/cloudbrain_statistic.go @@ -1,6 +1,7 @@ package repo import ( + "fmt" "net/http" "strings" "time" @@ -8,37 +9,39 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" cloudbrainService "code.gitea.io/gitea/services/cloudbrain" ) func CloudbrainDurationStatisticHour() { - var statisticTime time.Time - var count int64 - recordDurationUpdateTime, err := models.GetDurationRecordUpdateTime() - if err != nil { - log.Error("Can not get GetDurationRecordBeginTime", err) - } - now := time.Now() - currentTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) - if err == nil && len(recordDurationUpdateTime) > 0 { - statisticTime = time.Unix(int64(recordDurationUpdateTime[0].DateTime), 0).Add(+1 * time.Hour) - } else { - statisticTime = currentTime - } - deleteBeginTime := time.Unix(int64(recordDurationUpdateTime[0].DateTime), 0) + if setting.IsCloudbrainTimingEnabled { + var statisticTime time.Time + var count int64 + recordDurationUpdateTime, err := models.GetDurationRecordUpdateTime() + if err != nil { + log.Error("Can not get GetDurationRecordBeginTime", err) + } + now := time.Now() + currentTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) + if err == nil && len(recordDurationUpdateTime) > 0 { + statisticTime = time.Unix(int64(recordDurationUpdateTime[0].DateTimeUnix), 0).Add(+1 * time.Hour) + } else { + statisticTime = currentTime + } - err = models.DeleteCloudbrainDurationStatistic(timeutil.TimeStamp(deleteBeginTime.Unix()), timeutil.TimeStamp(currentTime.Unix())) - if err != nil { - log.Error("DeleteCloudbrainDurationStatistic failed", err) - } + err = models.DeleteCloudbrainDurationStatistic(timeutil.TimeStamp(statisticTime.Unix()), timeutil.TimeStamp(currentTime.Unix())) + if err != nil { + log.Error("DeleteCloudbrainDurationStatistic failed", err) + } - for statisticTime.Before(currentTime) || statisticTime.Equal(currentTime) { - countEach := summaryDurationStat(statisticTime) - count += countEach - statisticTime = statisticTime.Add(+1 * time.Hour) + for statisticTime.Before(currentTime) || statisticTime.Equal(currentTime) { + countEach := summaryDurationStat(statisticTime) + count += countEach + statisticTime = statisticTime.Add(+1 * time.Hour) + } + log.Info("summaryDurationStat count: %v", count) } - log.Info("summaryDurationStat count: %v", count) } func UpdateDurationStatisticHistoryData(beginTime time.Time, endTime time.Time) int64 { var count int64 @@ -55,7 +58,7 @@ func UpdateDurationStatisticHistoryData(beginTime time.Time, endTime time.Time) //statisticTime是当前的时辰,比如当前是2019-01-01 12:01:01,那么statisticTime就是2019-01-01 12:00:00 func summaryDurationStat(statisticTime time.Time) int64 { var count int64 - dateTime := timeutil.TimeStamp(statisticTime.Add(-1 * time.Hour).Unix()) + dateTimeUnix := timeutil.TimeStamp(statisticTime.Add(-1 * time.Hour).Unix()) beginTime := statisticTime.Add(-1 * time.Hour).Unix() dayTime := statisticTime.Add(-1 * time.Hour).Format("2006-01-02") hourTime := statisticTime.Add(-1 * time.Hour).Hour() @@ -66,27 +69,9 @@ func summaryDurationStat(statisticTime time.Time) int64 { log.Info("GetCloudbrainByTime err: %v", err) return 0 } - cloudbrainMap := make(map[string]*models.Cloudbrain) + fmt.Printf("ciTasks:", ciTasks[0].Cloudbrain.JobName) models.LoadSpecs4CloudbrainInfo(ciTasks) - - for _, cloudbrain := range ciTasks { - if cloudbrain.Cloudbrain.StartTime == 0 { - cloudbrain.Cloudbrain.StartTime = cloudbrain.Cloudbrain.CreatedUnix - } - if cloudbrain.Cloudbrain.EndTime == 0 { - cloudbrain.Cloudbrain.EndTime = cloudbrain.Cloudbrain.UpdatedUnix - } - cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain) - if cloudbrain.Cloudbrain.Spec != nil { - if _, ok := cloudbrainMap[cloudbrain.Cloudbrain.AiCenter+"/"+cloudbrain.Cloudbrain.Spec.AccCardType]; !ok { - if cloudbrain.Cloudbrain.Spec != nil { - cloudbrainMap[cloudbrain.Cloudbrain.AiCenter+"/"+cloudbrain.Cloudbrain.Spec.AccCardType] = &cloudbrain.Cloudbrain - } - } - } - } - - cloudBrainCenterCodeAndCardTypeInfo := getcloudBrainCenterCodeAndCardTypeInfo(ciTasks, beginTime, endTime) + cloudBrainCenterCodeAndCardTypeInfo, cloudbrainMap := getcloudBrainCenterCodeAndCardTypeInfo(ciTasks, beginTime, endTime) resourceQueues, err := models.GetCanUseCardInfo() if err != nil { @@ -111,7 +96,7 @@ func summaryDurationStat(statisticTime time.Time) int64 { cardsTotalDurationMap[cloudbrainTable.Cluster+"/"+centerCode+"/"+cardType] = 0 } cloudbrainDurationStat := models.CloudbrainDurationStatistic{ - DateTime: dateTime, + DateTimeUnix: dateTimeUnix, DayTime: dayTime, HourTime: hourTime, Cluster: cloudbrainTable.Cluster, @@ -133,7 +118,7 @@ func summaryDurationStat(statisticTime time.Time) int64 { for key, cardsTotalDuration := range cardsTotalDurationMap { cloudbrainDurationStat := models.CloudbrainDurationStatistic{ - DateTime: dateTime, + DateTimeUnix: dateTimeUnix, DayTime: dayTime, HourTime: hourTime, Cluster: strings.Split(key, "/")[0], @@ -170,11 +155,27 @@ func GetAiCenterNameByCode(centerCode string, language string) string { return aiCenterName } -func getcloudBrainCenterCodeAndCardTypeInfo(ciTasks []*models.CloudbrainInfo, beginTime int64, endTime int64) map[string]map[string]int { +func getcloudBrainCenterCodeAndCardTypeInfo(ciTasks []*models.CloudbrainInfo, beginTime int64, endTime int64) (map[string]map[string]int, map[string]*models.Cloudbrain) { var WorkServerNumber int var AccCardsNum int + cloudbrainMap := make(map[string]*models.Cloudbrain) cloudBrainCenterCodeAndCardType := make(map[string]map[string]int) for _, cloudbrain := range ciTasks { + if cloudbrain.Cloudbrain.StartTime == 0 { + cloudbrain.Cloudbrain.StartTime = cloudbrain.Cloudbrain.CreatedUnix + } + if cloudbrain.Cloudbrain.EndTime == 0 { + cloudbrain.Cloudbrain.EndTime = timeutil.TimeStamp(time.Now().Unix()) + } + cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain) + if cloudbrain.Cloudbrain.Spec != nil { + if _, ok := cloudbrainMap[cloudbrain.Cloudbrain.AiCenter+"/"+cloudbrain.Cloudbrain.Spec.AccCardType]; !ok { + if cloudbrain.Cloudbrain.Spec != nil { + cloudbrainMap[cloudbrain.Cloudbrain.AiCenter+"/"+cloudbrain.Cloudbrain.Spec.AccCardType] = &cloudbrain.Cloudbrain + } + } + } + cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain) if cloudbrain.Cloudbrain.StartTime == 0 { cloudbrain.Cloudbrain.StartTime = cloudbrain.Cloudbrain.CreatedUnix @@ -196,7 +197,7 @@ func getcloudBrainCenterCodeAndCardTypeInfo(ciTasks []*models.CloudbrainInfo, be cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter] = make(map[string]int) } if cloudbrain.Cloudbrain.Spec != nil { - if cloudbrain.Cloudbrain.Status == string(models.ModelArtsRunning) { + if cloudbrain.Cloudbrain.Status == string(models.ModelArtsRunning) && cloudbrain.Cloudbrain.DeletedAt.IsZero() { if _, ok := cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType]; !ok { if int64(cloudbrain.Cloudbrain.StartTime) < beginTime { cloudBrainCenterCodeAndCardType[cloudbrain.Cloudbrain.AiCenter][cloudbrain.Cloudbrain.Spec.AccCardType] = AccCardsNum * WorkServerNumber * (int(endTime) - int(beginTime)) @@ -240,7 +241,7 @@ func getcloudBrainCenterCodeAndCardTypeInfo(ciTasks []*models.CloudbrainInfo, be } } - return cloudBrainCenterCodeAndCardType + return cloudBrainCenterCodeAndCardType, cloudbrainMap } func CloudbrainUpdateHistoryData(ctx *context.Context) { @@ -258,7 +259,7 @@ func CloudbrainUpdateHistoryData(ctx *context.Context) { endTimeUnix := timeutil.TimeStamp(endTime.Unix()) err = models.DeleteCloudbrainDurationStatistic(beginTimeUnix, endTimeUnix) - count = UpdateDurationStatisticHistoryData(beginTime.Add(+1*time.Hour), endTime) + count = UpdateDurationStatisticHistoryData(beginTime.Add(+1*time.Hour), endTime.Add(+1*time.Hour)) } ctx.JSON(http.StatusOK, map[string]interface{}{ "message": 0,