fix: kpi指标数据查询支持根据字段排序

This commit is contained in:
TsMask
2023-12-27 17:30:46 +08:00
parent cc9f779957
commit 55893741ef
6 changed files with 48 additions and 35 deletions

View File

@@ -5,6 +5,7 @@ import (
"ems.agt/src/framework/i18n" "ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx" "ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/vo/result" "ems.agt/src/framework/vo/result"
"ems.agt/src/modules/network_element/model"
neService "ems.agt/src/modules/network_element/service" neService "ems.agt/src/modules/network_element/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -30,13 +31,7 @@ type PerfKPIController struct {
// GET /data // GET /data
func (s *PerfKPIController) GoldKPI(c *gin.Context) { func (s *PerfKPIController) GoldKPI(c *gin.Context) {
language := ctx.AcceptLanguage(c) language := ctx.AcceptLanguage(c)
var querys struct { var querys model.GoldKPIQuery
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
StartTime string `form:"startTime" binding:"required"`
EndTime string `form:"endTime" binding:"required"`
Interval int64 `form:"interval" binding:"required"`
}
if err := c.ShouldBindQuery(&querys); err != nil { if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return return
@@ -47,12 +42,10 @@ func (s *PerfKPIController) GoldKPI(c *gin.Context) {
if startTime.IsZero() { if startTime.IsZero() {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
} }
startTimeStr := startTime.Format(date.YYYY_MM_DD_HH_MM_SS)
endTime := date.ParseStrToDate(querys.EndTime, date.YYYY_MM_DD_HH_MM_SS) endTime := date.ParseStrToDate(querys.EndTime, date.YYYY_MM_DD_HH_MM_SS)
if startTime.IsZero() { if endTime.IsZero() {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
} }
endTimeStr := endTime.Format(date.YYYY_MM_DD_HH_MM_SS)
// 查询网元获取IP // 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID) neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
@@ -60,16 +53,10 @@ func (s *PerfKPIController) GoldKPI(c *gin.Context) {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo"))) c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return return
} }
querys.RmUID = neInfo.RmUID
// 获取数据指标id
var kpiIds []string
kpiTitles := s.perfKPIService.SelectGoldKPITitle(neInfo.NeType)
for _, kpiId := range kpiTitles {
kpiIds = append(kpiIds, kpiId.KPIID)
}
// 查询数据 // 查询数据
kpiData := s.perfKPIService.SelectGoldKPI(neInfo.RmUID, neInfo.NeType, startTimeStr, endTimeStr, kpiIds, querys.Interval) kpiData := s.perfKPIService.SelectGoldKPI(querys)
c.JSON(200, result.OkData(kpiData)) c.JSON(200, result.OkData(kpiData))
} }

View File

@@ -9,3 +9,15 @@ type GoldKPITitle struct {
CnTitle string `json:"cnTitle" gorm:"column:cn_title"` CnTitle string `json:"cnTitle" gorm:"column:cn_title"`
EnTitle string `json:"enTitle" gorm:"column:en_title"` EnTitle string `json:"enTitle" gorm:"column:en_title"`
} }
// GoldKPIQuery 黄金指标查询参数结构体
type GoldKPIQuery struct {
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
StartTime string `form:"startTime" binding:"required"`
EndTime string `form:"endTime" binding:"required"`
Interval int64 `form:"interval" binding:"required"`
RmUID string `form:"rmUID"`
SortField string `form:"sortField" binding:"omitempty,oneof=timeGroup"`
SortOrder string `form:"sortOrder" binding:"omitempty,oneof=asc desc"`
}

View File

@@ -4,8 +4,8 @@ import "ems.agt/src/modules/network_element/model"
// 性能统计 数据层接口 // 性能统计 数据层接口
type IPerfKPI interface { type IPerfKPI interface {
// SelectGoldKPI 通过ne_type和ne_id查询网元信息 // SelectGoldKPI 通过网元指标数据信息
SelectGoldKPI(rmUID, neType string, startTime, endTime string, kpiIds []string, interval int64) []map[string]any SelectGoldKPI(query model.GoldKPIQuery, kpiIds []string) []map[string]any
// SelectGoldKPITitle // SelectGoldKPITitle
SelectGoldKPITitle(neType string) []model.GoldKPITitle SelectGoldKPITitle(neType string) []model.GoldKPITitle

View File

@@ -16,25 +16,25 @@ var NewPerfKPIImpl = &PerfKPIImpl{}
type PerfKPIImpl struct{} type PerfKPIImpl struct{}
// SelectGoldKPI 通过网元指标数据信息 // SelectGoldKPI 通过网元指标数据信息
func (r *PerfKPIImpl) SelectGoldKPI(rmUID, neType string, startTime, endTime string, kpiIds []string, interval int64) []map[string]any { func (r *PerfKPIImpl) SelectGoldKPI(query model.GoldKPIQuery, kpiIds []string) []map[string]any {
// 查询条件拼接 // 查询条件拼接
var conditions []string var conditions []string
var params []any var params []any
if rmUID != "" { if query.RmUID != "" {
conditions = append(conditions, "gk.rm_uid = ?") conditions = append(conditions, "gk.rm_uid = ?")
params = append(params, rmUID) params = append(params, query.RmUID)
} }
if neType != "" { if query.NeType != "" {
conditions = append(conditions, "gk.ne_type = ?") conditions = append(conditions, "gk.ne_type = ?")
params = append(params, neType) params = append(params, query.NeType)
} }
if startTime != "" { if query.StartTime != "" {
conditions = append(conditions, "gk.start_time >= ?") conditions = append(conditions, "gk.start_time >= ?")
params = append(params, startTime) params = append(params, query.StartTime)
} }
if endTime != "" { if query.EndTime != "" {
conditions = append(conditions, "gk.start_time <= ?") conditions = append(conditions, "gk.start_time <= ?")
params = append(params, endTime) params = append(params, query.EndTime)
} }
// 构建查询条件语句 // 构建查询条件语句
whereSql := "" whereSql := ""
@@ -44,7 +44,7 @@ func (r *PerfKPIImpl) SelectGoldKPI(rmUID, neType string, startTime, endTime str
// 查询字段列 // 查询字段列
timeFormat := "DATE_FORMAT(gk.start_time, '%Y-%m-%d %H:')" timeFormat := "DATE_FORMAT(gk.start_time, '%Y-%m-%d %H:')"
minuteGroup := fmt.Sprintf("LPAD(FLOOR(MINUTE(gk.start_time) / %d) * %d, 2, '0')", interval, interval) minuteGroup := fmt.Sprintf("LPAD(FLOOR(MINUTE(gk.start_time) / %d) * %d, 2, '0')", query.Interval, query.Interval)
groupByField := fmt.Sprintf("CONCAT( %s, %s ) AS timeGroup", timeFormat, minuteGroup) groupByField := fmt.Sprintf("CONCAT( %s, %s ) AS timeGroup", timeFormat, minuteGroup)
var fields = []string{ var fields = []string{
groupByField, groupByField,
@@ -58,7 +58,14 @@ func (r *PerfKPIImpl) SelectGoldKPI(rmUID, neType string, startTime, endTime str
fieldsSql := strings.Join(fields, ",") fieldsSql := strings.Join(fields, ",")
// 查询数据 // 查询数据
querySql := fmt.Sprintf("SELECT %s FROM gold_kpi gk %s GROUP BY timeGroup", fieldsSql, whereSql) if query.SortField == "" {
query.SortField = "timeGroup"
}
if query.SortOrder == "" {
query.SortOrder = "desc"
}
orderSql := fmt.Sprintf(" order by %s %s", query.SortField, query.SortOrder)
querySql := fmt.Sprintf("SELECT %s FROM gold_kpi gk %s GROUP BY timeGroup %s", fieldsSql, whereSql, orderSql)
results, err := datasource.RawDB("", querySql, params) results, err := datasource.RawDB("", querySql, params)
if err != nil { if err != nil {
logger.Errorf("query err => %v", err) logger.Errorf("query err => %v", err)

View File

@@ -4,8 +4,8 @@ import "ems.agt/src/modules/network_element/model"
// 性能统计 数据层接口 // 性能统计 数据层接口
type IPerfKPI interface { type IPerfKPI interface {
// SelectGoldKPI 通过ne_type和ne_id查询网元信息 // SelectGoldKPI 通过网元指标数据信息
SelectGoldKPI(rmUID, neType string, startTime, endTime string, kpiIds []string, interval int64) []map[string]any SelectGoldKPI(query model.GoldKPIQuery) []map[string]any
// SelectGoldKPITitle // SelectGoldKPITitle
SelectGoldKPITitle(neType string) []model.GoldKPITitle SelectGoldKPITitle(neType string) []model.GoldKPITitle

View File

@@ -17,8 +17,15 @@ type PerfKPIImpl struct {
} }
// SelectGoldKPI 通过网元指标数据信息 // SelectGoldKPI 通过网元指标数据信息
func (r *PerfKPIImpl) SelectGoldKPI(rmUID, neType string, startTime, endTime string, kpiIds []string, interval int64) []map[string]any { func (r *PerfKPIImpl) SelectGoldKPI(query model.GoldKPIQuery) []map[string]any {
data := r.perfKPIRepository.SelectGoldKPI(rmUID, neType, startTime, endTime, kpiIds, interval) // 获取数据指标id
var kpiIds []string
kpiTitles := r.perfKPIRepository.SelectGoldKPITitle(query.NeType)
for _, kpiId := range kpiTitles {
kpiIds = append(kpiIds, kpiId.KPIID)
}
data := r.perfKPIRepository.SelectGoldKPI(query, kpiIds)
if data == nil { if data == nil {
return []map[string]any{} return []map[string]any{}
} }