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

@@ -16,25 +16,25 @@ var NewPerfKPIImpl = &PerfKPIImpl{}
type PerfKPIImpl struct{}
// 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 params []any
if rmUID != "" {
if query.RmUID != "" {
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 = ?")
params = append(params, neType)
params = append(params, query.NeType)
}
if startTime != "" {
if query.StartTime != "" {
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 <= ?")
params = append(params, endTime)
params = append(params, query.EndTime)
}
// 构建查询条件语句
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:')"
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)
var fields = []string{
groupByField,
@@ -58,7 +58,14 @@ func (r *PerfKPIImpl) SelectGoldKPI(rmUID, neType string, startTime, endTime str
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)
if err != nil {
logger.Errorf("query err => %v", err)