From db1019494707ef2bb581606e081ee636519b4d0c Mon Sep 17 00:00:00 2001 From: liuzx Date: Mon, 30 Jan 2023 17:20:55 +0800 Subject: [PATCH] fix-3578 --- models/cloudbrain_static.go | 18 ++++++++---------- routers/api/v1/repo/cloudbrain_dashboard.go | 28 +++++++++++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/models/cloudbrain_static.go b/models/cloudbrain_static.go index d5ef350ce..9006027a8 100644 --- a/models/cloudbrain_static.go +++ b/models/cloudbrain_static.go @@ -235,7 +235,7 @@ func GetRunningTop() ([]*CloudbrainInfo, error) { } func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) { - countSql := "SELECT count(*) FROM " + + countSql := "SET TIME ZONE 'PRC'; SELECT count(*) FROM " + "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime + "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime + "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') >= '" + hourBeginTime + @@ -243,15 +243,13 @@ func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTim return x.SQL(countSql).Count() } -//SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime)) func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) { - countSql := "SELECT count(*) FROM " + - "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime + - "') or (to_char(to_timestamp(COALESCE(end_time,EXTRACT(epoch FROM now()))), 'HH24:MI:SS') < '" + hourBeginTime + "'))" + - " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime + - "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')" + countSql := "SET TIME ZONE 'PRC'; SELECT count(*) FROM " + + "public.cloudbrain where ((to_char(to_timestamp(start_time), ' HH24:MI:SS') < '" + hourEndTime + + "') and (to_char(to_timestamp(CASE WHEN start_time is not null AND end_time isnull THEN COALESCE(end_time,EXTRACT(epoch FROM now())) ELSE end_time END), 'HH24:MI:SS') >= '" + hourBeginTime + "'))" + + " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + + "' and to_char(to_timestamp(end_time), 'YYYY-MM-DD') >= '" + dateBeginTime + "')" return x.SQL(countSql).Count() - } func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) { @@ -271,8 +269,8 @@ func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[str return dateHourMap, nil } -func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) { - dateHourMap := make(map[string]interface{}) +func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]int64, error) { + dateHourMap := make(map[string]int64) var slice = []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23} for key, value := range slice { hourBeginHour := util.AddZero(value) + ":00:00" diff --git a/routers/api/v1/repo/cloudbrain_dashboard.go b/routers/api/v1/repo/cloudbrain_dashboard.go index b980c70b3..709f982a0 100755 --- a/routers/api/v1/repo/cloudbrain_dashboard.go +++ b/routers/api/v1/repo/cloudbrain_dashboard.go @@ -872,11 +872,13 @@ func GetCloudbrainsCreateHoursData(ctx *context.Context) { if queryType != "" { if queryType == "all" { beginTime = recordBeginTime + beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location()) endTime = now.AddDate(0, 0, 1) endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location()) } else if queryType == "today" { beginTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) endTime = now.AddDate(0, 0, 1) + endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location()) } else if queryType == "yesterday" { beginTime = now.AddDate(0, 0, -1) beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location()) @@ -941,18 +943,30 @@ func GetCloudbrainsCreateHoursData(ctx *context.Context) { ctx.Error(http.StatusBadRequest, ctx.Tr("hourPeriodCount_get_error")) return } - - runHourPeriodCount, err := models.GetRunHourPeriodCount(dateBeginTime, dateEndTime) - if err != nil { - log.Error("Can not query runHourPeriodCount.", err) - ctx.Error(http.StatusBadRequest, ctx.Tr("runHourPeriodCount_get_error")) - return + dataPeriodCount := make(map[string]int64) + days := (endTime.Unix() - beginTime.Unix()) / 3600 / 24 + for i := 0; i < int(days); i++ { + runHourPeriodCount, err := models.GetRunHourPeriodCount(dateBeginTime, dateEndTime) + if err != nil { + log.Error("Can not query runHourPeriodCount.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("runHourPeriodCount_get_error")) + return + } + var slice = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"} + for _, v := range slice { + dataPeriodCount[v] = dataPeriodCount[v] + runHourPeriodCount[v] + } + beginTime = endTime + endTime = endTime.AddDate(0, 0, 1) + dateBeginTime = beginTime.Format("2006-01-02") + dateEndTime = endTime.Format("2006-01-02") } + ctx.JSON(http.StatusOK, map[string]interface{}{ "recordBeginTime": recordCloudbrain[0].Cloudbrain.CreatedUnix, "updateTime": now.Unix(), "createHourPeriodCount": createHourPeriodCount, - "runHourPeriodCount": runHourPeriodCount, + "runHourPeriodCount": dataPeriodCount, }) }