From 4206670658a168bfdc2a32e49f5494af9a3de1ca Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 21 Oct 2022 10:04:12 +0800 Subject: [PATCH] update --- routers/api/v1/api.go | 1 - routers/api/v1/repo/cloudbrain_dashboard.go | 131 ++++++-------------- 2 files changed, 35 insertions(+), 97 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 2964e87ce..b719db71e 100755 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -602,7 +602,6 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/overview_resource", repo.GetCloudbrainResourceOverview) m.Get("/resource_usage_statistic", repo.GetDurationRateStatistic) - m.Get("/resource_usage_rate", repo.GetCloudbrainResourceUsage) m.Get("/resource_usage_rate_detail", repo.GetCloudbrainResourceUsageDetail) }) }, operationReq) diff --git a/routers/api/v1/repo/cloudbrain_dashboard.go b/routers/api/v1/repo/cloudbrain_dashboard.go index 3ce46c60a..6824858fb 100755 --- a/routers/api/v1/repo/cloudbrain_dashboard.go +++ b/routers/api/v1/repo/cloudbrain_dashboard.go @@ -1520,63 +1520,10 @@ func GetCloudbrainResourceOverview(ctx *context.Context) { }) } -func GetCloudbrainResourceUsage(ctx *context.Context) { - recordBeginTime := time.Now().AddDate(0, 0, -6) - beginTime, endTime, err := getCloudbrainTimePeroid(ctx, recordBeginTime) - if err != nil { - log.Error("getCloudbrainTimePeroid error:", err) - return - } - cardUsageRes := make(map[string]int) - cardCanUsageRes := make(map[string]int) - cardUtilizationRate := make(map[string]int) - // cardDurationStatistics, err := models.GetCardDurationStatistics(beginTime, endTime) - cardDurationStatistics, err := models.GetCardDurationStatistics(&models.DurationStatisticOptions{ - BeginTime: beginTime, - EndTime: endTime, - }) - if err != nil { - log.Error("GetCardDurationStatistics error:", err) - return - } - - for _, cloudbrainStat := range cardDurationStatistics { - if cloudbrainStat.TotalCanUse { - if _, ok := cardCanUsageRes[cloudbrainStat.AiCenterCode]; !ok { - cardCanUsageRes[cloudbrainStat.AiCenterCode] = cloudbrainStat.CardsTotalDuration - } else { - cardCanUsageRes[cloudbrainStat.AiCenterCode] += cloudbrainStat.CardsTotalDuration - } - } else { - if _, ok := cardUsageRes[cloudbrainStat.AiCenterCode]; !ok { - cardUsageRes[cloudbrainStat.AiCenterCode] = cloudbrainStat.CardsTotalDuration - } else { - cardUsageRes[cloudbrainStat.AiCenterCode] += cloudbrainStat.CardsTotalDuration - } - } - } - for k, v := range cardCanUsageRes { - for j, i := range cardUsageRes { - if k == j { - cardUtilizationRate[k] = i / v - } - } - } - - ctx.JSON(http.StatusOK, map[string]interface{}{ - "cardDurationStatistics": cardDurationStatistics, - "cardUsageRes": cardUsageRes, - "cardCanUsageRes": cardCanUsageRes, - "cardUtilizationRate": cardUtilizationRate, - }) - -} - func GetCloudbrainResourceUsageDetail(ctx *context.Context) { aiCenterCode := ctx.QueryTrim("aiCenterCode") - log.Info("aiCenterCode: %v", aiCenterCode) if aiCenterCode == "" { - aiCenterCode = "OpenIOne" + aiCenterCode = models.AICenterOfCloudBrainOne } beginTime, endTime := getBeginAndEndTime(ctx) dayCloudbrainDuration, count, err := getDayCloudbrainDuration(beginTime, endTime, aiCenterCode) @@ -1713,40 +1660,16 @@ func getAiCenterUsageDuration(beginTime time.Time, endTime time.Time, cloudbrain } else { usageDuration += cloudbrainStatistic.CardsTotalDuration } - // if cloudbrainStatistic.TotalCanUse { - // if _, ok := aiCenterTotalDuration[Date]; !ok { - // aiCenterTotalDuration[Date] = cloudbrainStatistic.CardsTotalDuration - // } else { - // aiCenterTotalDuration[Date] += cloudbrainStatistic.CardsTotalDuration - // } - // } else { - // if _, ok := aiCenterUsageDuration[Date]; !ok { - // aiCenterUsageDuration[Date] = cloudbrainStatistic.CardsTotalDuration - // } else { - // aiCenterUsageDuration[Date] += cloudbrainStatistic.CardsTotalDuration - // } - // } } } - // ResourceAiCenterRes, err := models.GetResourceAiCenters() - // if err != nil { - // log.Error("Can not get ResourceAiCenterRes.", err) - // return nil, nil, nil - // } - // for _, v := range ResourceAiCenterRes { - // if _, ok := aiCenterUsageDuration[v.AiCenterCode]; !ok { - // aiCenterUsageDuration[v.AiCenterCode] = 0 - // } - // } - - // for k, v := range aiCenterTotalDuration { - // for i, j := range aiCenterUsageDuration { - // if k == i { - // aiCenterUsageRate[k] = float64(j) / float64(v) - // } - // } - // } - // usageRate = float64(usageDuration) / float64(totalDuration) + if totalDuration == 0 || usageDuration == 0 { + usageRate = 0 + } else { + usageRate = float64(usageDuration) / float64(totalDuration) + } + // if + // usageRate, _ = strconv.ParseFloat(fmt.Sprintf("%.4f", float32(usageDuration)/float32(totalDuration)), 64) + // totalUsageRate = totalUse / totalCanUse return totalDuration, usageDuration, usageRate } @@ -1817,8 +1740,8 @@ func getDurationStatistic(beginTime time.Time, endTime time.Time) (models.Durati } } } - // totalCanUse := float64(0) - // totalUse := float64(0) + totalCanUse := float32(0) + totalUse := float32(0) totalUsageRate := float32(0) for k, v := range OpenITotalDuration { for i, j := range OpenIUsageDuration { @@ -1827,7 +1750,19 @@ func getDurationStatistic(beginTime time.Time, endTime time.Time) (models.Durati } } } - // totalUsageRate := totalUse / totalCanUse + for _, v := range OpenITotalDuration { + totalCanUse += float32(v) + } + for _, v := range OpenIUsageRate { + totalUse += float32(v) + } + if totalCanUse == 0 || totalUse == 0 { + totalUsageRate = 0 + } else { + totalUsageRate = totalUse / totalCanUse + } + // totalUsageRate = totalUse / totalCanUse + // strconv.FormatFloat(*100, 'f', 4, 64) + "%" OpenIDurationRate.AiCenterTotalDurationStat = OpenITotalDuration OpenIDurationRate.AiCenterUsageDurationStat = OpenIUsageDuration @@ -1919,13 +1854,17 @@ func getHourCloudbrainDuration(beginTime time.Time, endTime time.Time, aiCenterC } - // for k, v := range hourTimeTotalDuration { - // for i, j := range hourTimeUsageDuration { - // if k == i { - // hourTimeUsageRate[k] = float64(j) / float64(v) - // } - // } - // } + for k, v := range hourTimeTotalDuration { + for i, j := range hourTimeUsageDuration { + if k == i { + if v == 0 || j == 0 { + hourTimeUsageRate[k] = 0 + } else { + hourTimeUsageRate[k] = float64(j) / float64(v) + } + } + } + } hourTimeStatistic.HourTimeTotalDuration = hourTimeTotalDuration hourTimeStatistic.HourTimeUsageDuration = hourTimeUsageDuration