feat: 补充缺失的自定义指标接口
This commit is contained in:
183
src/modules/network_data/controller/all_kpi_c.go
Normal file
183
src/modules/network_data/controller/all_kpi_c.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/modules/network_data/model"
|
||||
neDataService "be.ems/src/modules/network_data/service"
|
||||
neService "be.ems/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 KPICController 结构体
|
||||
var NewKPIC = &KPICController{
|
||||
neInfoService: neService.NewNeInfo,
|
||||
kpicReportService: neDataService.NewKpiCReport,
|
||||
}
|
||||
|
||||
// 性能统计
|
||||
//
|
||||
// PATH /kpic
|
||||
type KPICController struct {
|
||||
neInfoService *neService.NeInfo // 网元信息服务
|
||||
kpicReportService *neDataService.KpiCReport // 指标统计服务
|
||||
}
|
||||
|
||||
// 获取统计数据
|
||||
//
|
||||
// GET /data
|
||||
//
|
||||
// @Tags network_data/kpi
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param neType query string true "NE Type" Enums(IMS,AMF,AUSF,UDM,SMF,PCF,NSSF,NRF,UPF,MME,CBC,OMC,SGWC,SMSC) default(AMF)
|
||||
// @Param neId query string true "NE ID" default(001)
|
||||
// @Param beginTime query number true "begin time (timestamped milliseconds)" default(1729162507596)
|
||||
// @Param endTime query number true "end time (timestamped milliseconds)" default(1729164187611)
|
||||
// @Param interval query number true "interval" Enums(5,10,15,30,60,300,600,900,1800,3600) default(60)
|
||||
// @Success 200 {object} object "Response Results"
|
||||
// @Security TokenAuth
|
||||
// @Summary Access to statistical data
|
||||
// @Description Access to statistical data
|
||||
// @Router /neData/kpic/data [get]
|
||||
func (s KPICController) KPIData(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var querys model.KPICQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
querys.RmUID = neInfo.RmUID
|
||||
|
||||
// 查询数据
|
||||
kpiData := s.kpicReportService.FindData(querys)
|
||||
c.JSON(200, resp.OkData(kpiData))
|
||||
}
|
||||
|
||||
// 自定义标题列表
|
||||
//
|
||||
// GET /titlelist
|
||||
func (s KPICController) ListTitle(c *gin.Context) {
|
||||
query := reqctx.QueryMap(c)
|
||||
if v, ok := query["status"]; ok && v == "" {
|
||||
query["status"] = "1"
|
||||
}
|
||||
rows, total := s.kpicReportService.TitleFindByPage(query)
|
||||
c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows}))
|
||||
}
|
||||
|
||||
// 自定义标题新增
|
||||
//
|
||||
// POST /title
|
||||
func (s KPICController) AddTitle(c *gin.Context) {
|
||||
var body model.KpiCTitle
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 校验指标是否存在
|
||||
kpicTitles := s.kpicReportService.TitleFind(model.KpiCTitle{
|
||||
NeType: body.NeType,
|
||||
KpiId: body.KpiId,
|
||||
})
|
||||
if len(kpicTitles) > 0 {
|
||||
for _, v := range kpicTitles {
|
||||
if v.Status == "2" {
|
||||
c.JSON(200, resp.ErrMsg("custom indicator already exist"))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成自定义指标ID
|
||||
if body.KpiId == "" {
|
||||
body.KpiId = fmt.Sprintf("%s.C.01", strings.ToUpper(body.NeType))
|
||||
} else {
|
||||
// 网元类型最后指标ID
|
||||
lastKpiId := s.kpicReportService.TitleLastKPIId(body.NeType)
|
||||
if lastKpiId != "" {
|
||||
// title like AMF.C.01 截断 .C. 并获取后面的数字部分
|
||||
parts := strings.Split(lastKpiId, ".C.")
|
||||
if len(parts) == 2 {
|
||||
numStr := parts[1]
|
||||
if num, err := strconv.Atoi(numStr); err == nil {
|
||||
num++ // 数字加 1
|
||||
// 转换为前面补零的 2 位字符串
|
||||
body.KpiId = fmt.Sprintf("%s.C.%02d", strings.ToUpper(body.NeType), num)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
body.CreatedBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.kpicReportService.TitleInsert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 自定义标题修改
|
||||
//
|
||||
// PUT /title
|
||||
func (s KPICController) EditTitle(c *gin.Context) {
|
||||
var body model.KpiCTitle
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
rows := s.kpicReportService.TitleUpdate(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 自定义标题删除
|
||||
//
|
||||
// DELETE /title/:id
|
||||
func (s KPICController) RemoveTitle(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
id := c.Query("id")
|
||||
if id == "" {
|
||||
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
|
||||
rows, err := s.kpicReportService.TitleDeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
@@ -33,6 +33,31 @@ func Setup(router *gin.Engine) {
|
||||
)
|
||||
}
|
||||
|
||||
// 性能自定义统计信息
|
||||
kpicGroup := neDataGroup.Group("/kpic")
|
||||
{
|
||||
kpicGroup.GET("/data",
|
||||
middleware.AuthorizeUser(nil),
|
||||
controller.NewKPIC.KPIData,
|
||||
)
|
||||
kpicGroup.GET("/title/list",
|
||||
middleware.AuthorizeUser(nil),
|
||||
controller.NewKPIC.ListTitle,
|
||||
)
|
||||
kpicGroup.POST("/title",
|
||||
middleware.AuthorizeUser(nil),
|
||||
controller.NewKPIC.AddTitle,
|
||||
)
|
||||
kpicGroup.PUT("/title",
|
||||
middleware.AuthorizeUser(nil),
|
||||
controller.NewKPIC.EditTitle,
|
||||
)
|
||||
kpicGroup.DELETE("/title",
|
||||
middleware.AuthorizeUser(nil),
|
||||
controller.NewKPIC.RemoveTitle,
|
||||
)
|
||||
}
|
||||
|
||||
// 告警数据信息
|
||||
alarmGroup := neDataGroup.Group("/alarm")
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ var NewKpiCReport = &KpiCReport{}
|
||||
type KpiCReport struct{}
|
||||
|
||||
// SelectGoldKPI 通过网元指标数据信息
|
||||
func (r KpiCReport) SelectKPI(query model.KPIQuery) []model.KpiCReport {
|
||||
func (r KpiCReport) SelectKPI(query model.KPICQuery) []model.KpiCReport {
|
||||
rows := []model.KpiCReport{}
|
||||
if query.NeType == "" {
|
||||
return rows
|
||||
@@ -87,3 +87,132 @@ func (r KpiCReport) SelectKPITitle(neType string) []model.KpiCTitle {
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// TitleLastKPIId 查询指标标题最后kpiid
|
||||
func (r KpiCReport) TitleLastKPIId(neType string) string {
|
||||
tx := db.DB("").Model(&model.KpiCTitle{})
|
||||
tx = tx.Where("ne_type=?", neType)
|
||||
tx = tx.Select("kpi_id").Order("kpi_id DESC")
|
||||
// 查询数据
|
||||
var kpiId string = ""
|
||||
if err := tx.Limit(1).Find(&kpiId).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return kpiId
|
||||
}
|
||||
return kpiId
|
||||
}
|
||||
|
||||
// SelectByPageTitle 分页查询集合
|
||||
func (r KpiCReport) TitleSelectByPage(query map[string]string) ([]model.KpiCTitle, int64) {
|
||||
tx := db.DB("").Model(&model.KpiCTitle{})
|
||||
// 查询条件拼接
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
tx = tx.Where("status = ?", v)
|
||||
} else {
|
||||
tx = tx.Where("status != ?", "2")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.KpiCTitle{}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Offset(int(pageNum * pageSize)).Limit(int(pageSize))
|
||||
|
||||
// 排序
|
||||
if v, ok := query["sortField"]; ok && v != "" {
|
||||
sortSql := v
|
||||
if o, ok := query["sortOrder"]; ok && o != "" {
|
||||
if o == "desc" {
|
||||
sortSql += " desc "
|
||||
} else {
|
||||
sortSql += " asc "
|
||||
}
|
||||
}
|
||||
tx = tx.Order(sortSql)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// TitleSelect 网元对应的指标名称
|
||||
func (r KpiCReport) TitleSelect(param model.KpiCTitle) []model.KpiCTitle {
|
||||
tx := db.DB("").Model(&model.KpiCTitle{})
|
||||
// 构建查询条件
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type =?", param.NeType)
|
||||
}
|
||||
if param.Title != "" {
|
||||
tx = tx.Where("title = ?", param.Title)
|
||||
}
|
||||
if param.Status != "" {
|
||||
tx = tx.Where("status = ?", param.Status)
|
||||
}
|
||||
// 查询数据
|
||||
rows := []model.KpiCTitle{}
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// TitleInsert 新增信息
|
||||
func (r KpiCReport) TitleInsert(param model.KpiCTitle) int64 {
|
||||
if param.CreatedBy != "" {
|
||||
param.UpdatedAt = time.Now().UnixMilli()
|
||||
}
|
||||
param.Status = "1"
|
||||
tx := db.DB("").Create(¶m)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return param.ID
|
||||
}
|
||||
|
||||
// TitleUpdate 修改信息
|
||||
func (r KpiCReport) TitleUpdate(param model.KpiCTitle) int64 {
|
||||
if param.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
param.UpdatedAt = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.KpiCTitle{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id", "created_by")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// TitleDeleteByIds 批量删除信息
|
||||
func (r KpiCReport) TitleDeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Model(&model.KpiCTitle{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
if err := tx.Update("status", 2).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/modules/network_data/model"
|
||||
@@ -21,7 +22,7 @@ type KpiCReport struct {
|
||||
}
|
||||
|
||||
// FindKPI 通过网元指标数据信息
|
||||
func (s KpiCReport) FindData(query model.KPIQuery) []map[string]any {
|
||||
func (s KpiCReport) FindData(query model.KPICQuery) []map[string]any {
|
||||
// 原始数据
|
||||
rows := s.kpiCReportRepository.SelectKPI(query)
|
||||
if len(rows) <= 0 {
|
||||
@@ -100,14 +101,16 @@ func (s KpiCReport) FindData(query model.KPIQuery) []map[string]any {
|
||||
// 遍历kpiIds数组对lastRecord赋值
|
||||
for _, kpiId := range kpiIds {
|
||||
if v, ok := record[kpiId]; ok {
|
||||
// 特殊字段,只取一次收到的非0值
|
||||
if kpiId == "AMF.01" || kpiId == "UDM.01" || kpiId == "UDM.02" || kpiId == "UDM.03" || kpiId == "SMF.01" {
|
||||
// startItem[kpiId] = parse.Number(v)
|
||||
continue // startIndex的值不累加不取最后
|
||||
} else {
|
||||
value := parse.Number(startItem[kpiId])
|
||||
startItem[kpiId] = value + parse.Number(v)
|
||||
value := v.(float64) + startItem[kpiId].(float64)
|
||||
formatted := fmt.Sprintf("%.3f", value)
|
||||
formattedFloat, err := strconv.ParseFloat(formatted, 64)
|
||||
if err != nil {
|
||||
formattedFloat = 0
|
||||
}
|
||||
startItem[kpiId] = formattedFloat
|
||||
|
||||
// value := parse.Number(startItem[kpiId])
|
||||
// startItem[kpiId] = value + parse.Number(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,3 +139,38 @@ func (s KpiCReport) Insert(param model.KpiCReport) int64 {
|
||||
func (r KpiCReport) FindTitle(neType string) []model.KpiCTitle {
|
||||
return r.kpiCReportRepository.SelectKPITitle(neType)
|
||||
}
|
||||
|
||||
// TitleLastKPIId 指标标题最后kpiid
|
||||
func (r KpiCReport) TitleLastKPIId(neType string) string {
|
||||
return r.kpiCReportRepository.TitleLastKPIId(neType)
|
||||
}
|
||||
|
||||
// FindByPage 根据条件分页查询
|
||||
func (r KpiCReport) TitleFindByPage(query map[string]string) ([]model.KpiCTitle, int64) {
|
||||
return r.kpiCReportRepository.TitleSelectByPage(query)
|
||||
}
|
||||
|
||||
// TitleFind 查询信息
|
||||
func (r KpiCReport) TitleFind(param model.KpiCTitle) []model.KpiCTitle {
|
||||
return r.kpiCReportRepository.TitleSelect(param)
|
||||
}
|
||||
|
||||
// TitleUpdate 更新信息
|
||||
func (r KpiCReport) TitleUpdate(param model.KpiCTitle) int64 {
|
||||
return r.kpiCReportRepository.TitleUpdate(param)
|
||||
}
|
||||
|
||||
// TitleDeleteByIds 批量删除信息
|
||||
func (r KpiCReport) TitleDeleteByIds(ids []int64) (int64, error) {
|
||||
rows := r.kpiCReportRepository.TitleDeleteByIds(ids)
|
||||
if rows > 0 {
|
||||
return rows, nil
|
||||
}
|
||||
// 删除信息失败!
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
|
||||
// TitleInsert 新增信息
|
||||
func (r KpiCReport) TitleInsert(param model.KpiCTitle) int64 {
|
||||
return r.kpiCReportRepository.TitleInsert(param)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user