mager: 合并11.3版本,包名和主线一样方便复制

This commit is contained in:
TsMask
2024-11-23 18:54:02 +08:00
parent c0368c07ef
commit d5954d40c8
514 changed files with 24451 additions and 17932 deletions

View File

@@ -5,9 +5,8 @@ import (
"net/http"
"strings"
"nms_cxy/lib/dborm"
"nms_cxy/lib/services"
"be.ems/lib/dborm"
"be.ems/lib/services"
"github.com/gin-gonic/gin"
)
@@ -41,13 +40,14 @@ func (k *KpiCReport) Get(c *gin.Context) {
return
}
if querys.StartTime != "" {
conditions = append(conditions, "created_at >= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
params = append(params, querys.StartTime)
}
if querys.EndTime != "" {
conditions = append(conditions, "created_at <= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
params = append(params, querys.EndTime)
}
conditions = append(conditions, "kpi_values != 'null'")
whereSql := ""
if len(conditions) > 0 {
@@ -71,7 +71,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
//err := dborm.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
err := dbg.Find(&reports).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusOK, services.DataResp(reports))
@@ -84,7 +84,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
var querys KpiCReportQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -93,7 +93,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(querys.NeType))
} else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found required parameter NE type"))
c.JSON(http.StatusOK, services.ErrResp("Not found required parameter NE type"))
return
}
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
@@ -107,13 +107,14 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
return
}
if querys.StartTime != "" {
conditions = append(conditions, "created_at >= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
params = append(params, querys.StartTime)
}
if querys.EndTime != "" {
conditions = append(conditions, "created_at <= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
params = append(params, querys.EndTime)
}
conditions = append(conditions, "kpi_values != 'null'")
whereSql := ""
if len(conditions) > 0 {
@@ -137,7 +138,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
//err := dborm.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
err := dbg.Find(&results).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -150,7 +151,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
"neName": *r.NeName,
"rmUID": *r.RmUID,
"startIndex": r.Index,
"timeGroup": r.Date[:10] + " " + *r.StartTime,
"timeGroup": r.Date[:10] + " " + *r.EndTime,
"createdAt": r.CreatedAt,
"granularity": r.Granularity,
}
@@ -170,7 +171,7 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
var querys KpiCReportQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -179,20 +180,21 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(querys.NeType))
} else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
c.JSON(http.StatusOK, services.ErrResp("Not found NE type"))
return
}
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
dbg := dborm.DefaultDB().Table(tableName)
if querys.StartTime != "" {
conditions = append(conditions, "created_at >= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
params = append(params, querys.StartTime)
}
if querys.EndTime != "" {
conditions = append(conditions, "created_at <= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
params = append(params, querys.EndTime)
}
conditions = append(conditions, "kpi_values != 'null'")
whereSql := ""
if len(conditions) > 0 {
@@ -204,7 +206,7 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
var total int64 = 0
err := dbg.Count(&total).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -225,7 +227,7 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
//err := dborm.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
err = dbg.Find(&reports).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -238,7 +240,7 @@ func (k *KpiCReport) Total(c *gin.Context) {
var querys KpiCReportQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -247,20 +249,21 @@ func (k *KpiCReport) Total(c *gin.Context) {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(querys.NeType))
} else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
c.JSON(http.StatusOK, services.ErrResp("Not found NE type"))
return
}
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
dbg := dborm.DefaultDB().Table(tableName)
if querys.StartTime != "" {
conditions = append(conditions, "created_at >= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
params = append(params, querys.StartTime)
}
if querys.EndTime != "" {
conditions = append(conditions, "created_at <= ?")
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
params = append(params, querys.EndTime)
}
conditions = append(conditions, "kpi_values != 'null'")
whereSql := ""
if len(conditions) > 0 {
@@ -270,7 +273,7 @@ func (k *KpiCReport) Total(c *gin.Context) {
var total int64 = 0
err := dbg.Count(&total).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -281,11 +284,11 @@ func (k *KpiCReport) Post(c *gin.Context) {
var report KpiCReport
if err := c.ShouldBindJSON(&report); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
if err := dborm.DefaultDB().Create(&report).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusCreated, services.DataResp(report))
@@ -296,12 +299,12 @@ func (k *KpiCReport) Put(c *gin.Context) {
id := c.Param("id")
if err := dborm.DefaultDB().First(&report, id).Error; err != nil {
c.JSON(http.StatusNotFound, services.ErrResp("KPI report not found"))
c.JSON(http.StatusOK, services.ErrResp("custom indicator report not found"))
return
}
if err := c.ShouldBindJSON(&report); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
dborm.DefaultDB().Save(&report)
@@ -312,7 +315,7 @@ func (k *KpiCReport) Delete(c *gin.Context) {
id := c.Param("id")
if err := dborm.DefaultDB().Delete(&KpiCReport{}, id).Error; err != nil {
c.JSON(http.StatusNotFound, services.ErrResp("KPI report not found"))
c.JSON(http.StatusOK, services.ErrResp("custom indicator report not found"))
return
}

View File

@@ -30,17 +30,16 @@ type KpiCReport struct {
}
type KpiCReportQuery struct {
NeType string `json:"neType" form:"neType" binding:"required"`
NeID string `json:"neId" form:"neId" binding:"required"`
RmUID string `json:"rmUID" form:"rmUID"`
StartTime string `json:"startTime" form:"startTime"`
EndTime string `json:"endTime" form:"endTime"`
TenantName string `json:"tenantName" form:"tenantName"`
UserName string `json:"userName" form:"userName"`
SortField string `json:"sortField" form:"sortField" binding:"omitempty,oneof=created_at"` // 排序字段,填写结果字段
SortOrder string `json:"sortOrder" form:"sortOrder" binding:"omitempty,oneof=asc desc"` // 排序升降序asc desc
PageNum int `json:"pageNum" form:"pageNum"`
PageSize int `json:"pageSize" form:"pageSize"`
NeType string `json:"neType" form:"neType" binding:"required"`
NeID string `json:"neId" form:"neId" binding:"required"`
RmUID string `json:"rmUID" form:"rmUID"`
StartTime string `json:"startTime" form:"startTime"`
EndTime string `json:"endTime" form:"endTime"`
UserName string `json:"userName" form:"userName"`
SortField string `json:"sortField" form:"sortField" binding:"omitempty,oneof=created_at"` // 排序字段,填写结果字段
SortOrder string `json:"sortOrder" form:"sortOrder" binding:"omitempty,oneof=asc desc"` // 排序升降序asc desc
PageNum int `json:"pageNum" form:"pageNum"`
PageSize int `json:"pageSize" form:"pageSize"`
}
type KpiCReport2FE struct {
@@ -51,7 +50,6 @@ type KpiCReport2FE struct {
TimeGroup string `json:"timeGroup"`
StartIndex int16 `json:"startIndex" gorm:"column:index"`
Granularity int8 `json:"granularity" gorm:"column:granularity"`
TenantID string `json:"tenantID" gorm:"column:tenant_id"`
}
func TableName() string {

View File

@@ -1,8 +1,7 @@
package kpi_c_report
import (
"nms_cxy/src/framework/middleware"
"be.ems/src/framework/middleware"
"github.com/gin-gonic/gin"
)

View File

@@ -3,22 +3,27 @@ package kpi_c_title
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"nms_cxy/lib/dborm"
"nms_cxy/lib/services"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/lib/services"
"be.ems/src/framework/utils/ctx"
"github.com/gin-gonic/gin"
)
// get customize kpi total and list
func (k *KpiCTitle) GetToalList(c *gin.Context) {
var titles []KpiCTitle
var conditions []string
var params []any
i18n := ctx.AcceptLanguage(c)
var querys KpiCTitleQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -41,7 +46,7 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
// Get total number
var total int64 = 0
if err := dbg.Count(&total).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -59,10 +64,12 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
dbg = dbg.Order(orderBy)
}
if err := dbg.Find(&titles).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
k.expressionAlias(titles, i18n)
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
//c.JSON(http.StatusOK, titles)
}
@@ -71,6 +78,7 @@ func (k *KpiCTitle) Get(c *gin.Context) {
var titles []KpiCTitle
var conditions []string
var params []any
i18n := ctx.AcceptLanguage(c)
// construct condition to get
if neType := c.Query("neType"); neType != "" {
@@ -86,14 +94,42 @@ func (k *KpiCTitle) Get(c *gin.Context) {
whereSql += strings.Join(conditions, " and ")
}
if err := dborm.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
k.expressionAlias(titles, i18n)
c.JSON(http.StatusOK, services.DataResp(titles))
//c.JSON(http.StatusOK, titles)
}
// alias customized kpi expression with cn/en title
func (k *KpiCTitle) expressionAlias(titles []KpiCTitle, i18n string) {
var title *KpiCTitle
for i := 0; i < len(titles); i++ {
title = &titles[i]
title.ExprAlias = *title.Expression
re := regexp.MustCompile(`'([^']+)'`)
matches := re.FindAllStringSubmatch(title.ExprAlias, -1)
for _, match := range matches {
var alias, sql string
if i18n == "zh" {
sql = fmt.Sprintf("SELECT cn_title FROM kpi_title WHERE kpi_id='%s'", match[1])
} else {
sql = fmt.Sprintf("SELECT en_title FROM kpi_title WHERE kpi_id='%s'", match[1])
}
err := dborm.XCoreDB().QueryRow(sql).Scan(&alias)
if err != nil {
log.Warn("Failed to QueryRow:", err)
continue
}
title.ExprAlias = regexp.MustCompile(match[1]).ReplaceAllString(title.ExprAlias, alias)
}
}
}
func (k *KpiCTitle) Total(c *gin.Context) {
var conditions []string
var params []any
@@ -113,7 +149,7 @@ func (k *KpiCTitle) Total(c *gin.Context) {
}
var total int64 = 0
if err := dborm.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
@@ -121,19 +157,47 @@ func (k *KpiCTitle) Total(c *gin.Context) {
}
func (k *KpiCTitle) Post(c *gin.Context) {
var title KpiCTitle
var title, res KpiCTitle
if err := c.ShouldBindJSON(&title); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
result := dborm.DefaultDB().Where("ne_type=? and kpi_id=?", title.NeType, title.KpiID).First(&title)
userName := ctx.LoginUserToUserName(c)
title.CreatedBy = &userName
result := dborm.DefaultDB().Where("ne_type=? and (kpi_id=? or title=?)", title.NeType, title.KpiID, title.Title).First(&title)
if result.RowsAffected > 0 {
c.JSON(http.StatusInternalServerError, services.ErrResp("target kpiC title already exist"))
c.JSON(http.StatusOK, services.ErrResp("custom indicator already exist"))
return
}
ret := dborm.DefaultDB().Table("kpi_c_title").Where("ne_type=? ORDER BY kpi_id DESC LIMIT 1", title.NeType).Scan(&res)
if err := ret.Error; err != nil {
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
newKpiID := *title.NeType + ".C" + ".01"
if ret.RowsAffected != 0 {
maxKpiID := *res.KpiID
prefix := maxKpiID[:len(maxKpiID)-2]
suffix := maxKpiID[len(maxKpiID)-2:]
suffixInt, err := strconv.Atoi(suffix)
if err != nil {
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
if suffixInt >= MAX_KPI_C_ID {
err := fmt.Errorf("exceed the max customized KPI ID")
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
suffixInt++
newSuffix := fmt.Sprintf("%02d", suffixInt)
newKpiID = prefix + newSuffix
}
title.KpiID = &newKpiID
if err := dborm.DefaultDB().Create(&title).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
kpiCReportTable := "kpi_c_report_" + strings.ToLower(*title.NeType)
@@ -141,17 +205,17 @@ func (k *KpiCTitle) Post(c *gin.Context) {
// clone table "kpi_c_report" to "kpi_c_report_{neType}"
sql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s AS SELECT * FROM %s WHERE 1=0", kpiCReportTable, "kpi_c_report")
if _, err := dborm.ExecSQL(sql, nil); err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
sql = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST,ADD PRIMARY KEY IF NOT EXISTS (`id`)", kpiCReportTable)
if _, err := dborm.ExecSQL(sql, nil); err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
sql = fmt.Sprintf("ALTER TABLE %s ADD INDEX IF NOT EXISTS `idx_timestamp`(`created_at`) USING BTREE, ADD INDEX IF NOT EXISTS `idx_uid_datetime`(`rm_uid`, `date`, `start_time`) USING BTREE", kpiCReportTable)
if _, err := dborm.ExecSQL(sql, nil); err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
}
@@ -163,12 +227,12 @@ func (k *KpiCTitle) Put(c *gin.Context) {
id := c.Param("id")
if err := dborm.DefaultDB().First(&title, id).Error; err != nil {
c.JSON(http.StatusNotFound, services.ErrResp("KPIC Title not found"))
c.JSON(http.StatusOK, services.ErrResp("custom indicator not found"))
return
}
if err := c.ShouldBindJSON(&title); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
dborm.DefaultDB().Save(&title)
@@ -180,7 +244,7 @@ func (k *KpiCTitle) Delete(c *gin.Context) {
id := c.Param("id")
if err := dborm.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
c.JSON(http.StatusNotFound, services.ErrResp("KPIC Title not found"))
c.JSON(http.StatusOK, services.ErrResp("custom indicator not found"))
return
}

View File

@@ -2,12 +2,17 @@ package kpi_c_title
import "time"
const (
MAX_KPI_C_ID = 99
)
type KpiCTitle struct {
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
NeType *string `gorm:"column:ne_type;default:NULL," json:"neType,omitempty"`
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpiId,omitempty"`
Title *string `gorm:"column:title;default:NULL," json:"title,omitempty"`
Expression *string `gorm:"column:expression;default:NULL," json:"expression,omitempty"`
ExprAlias string `gorm:"-" json:"exprAlias"`
Status string `gorm:"column:status;default:'Active'" json:"status"`
Unit *string `gorm:"column:unit" json:"unit,omitempty"`
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`

View File

@@ -1,8 +1,7 @@
package kpi_c_title
import (
"nms_cxy/src/framework/middleware"
"be.ems/src/framework/middleware"
"github.com/gin-gonic/gin"
)

View File

@@ -10,18 +10,17 @@ import (
"strings"
"time"
"nms_cxy/features/pm/kpi_c_report"
"nms_cxy/features/pm/kpi_c_title"
"nms_cxy/lib/dborm"
evaluate "nms_cxy/lib/eval"
"nms_cxy/lib/global"
"nms_cxy/lib/log"
"nms_cxy/lib/services"
"nms_cxy/omc/config"
neService "nms_cxy/src/modules/network_element/service"
wsService "nms_cxy/src/modules/ws/service"
"be.ems/features/pm/kpi_c_report"
"be.ems/features/pm/kpi_c_title"
"be.ems/lib/dborm"
evaluate "be.ems/lib/eval"
"be.ems/lib/global"
"be.ems/lib/log"
"be.ems/lib/services"
"be.ems/restagent/config"
neService "be.ems/src/modules/network_element/service"
wsService "be.ems/src/modules/ws/service"
"github.com/go-resty/resty/v2"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
@@ -45,7 +44,7 @@ type KpiReport struct {
NeType string `json:"NeType"`
KPIs []struct {
KPIID string `json:"KPIID"`
Value int `json:"Value"`
Value int64 `json:"Value"`
Err string `json:"Err"`
} `json:"KPIs"`
} `json:"NE"`
@@ -63,7 +62,7 @@ type GoldKpi struct {
RmUid string `json:"rmUid" xorm:"rm_uid"`
NEType string `json:"neType" xorm:"ne_type"`
KpiId string `json:"kpiId" xorm:"kpi_id"`
Value int `json:"value"`
Value int64 `json:"value"`
Error string `json:"error"`
Timestamp string `json:"timestamp"`
}
@@ -289,7 +288,7 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
}
// 发送到匹配的网元
neInfo := neService.NewNeInfoImpl.SelectNeInfoByRmuid(kpiData.RmUid)
neInfo := neService.NewNeInfo.SelectNeInfoByRmuid(kpiData.RmUid)
// custom kpi report to FE
kpiCEvent := map[string]any{
// kip_id ...
@@ -298,7 +297,7 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
"neName": kpiData.NEName,
"rmUID": kpiData.RmUid,
"startIndex": kpiData.Index,
"timeGroup": kpiData.Date[:10] + " " + kpiData.StartTime,
"timeGroup": kpiData.Date[:10] + " " + kpiData.EndTime,
"createdAt": kpiData.CreatedAt,
"granularity": kpiData.Granularity,
}
@@ -325,147 +324,17 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
if neInfo.RmUID == kpiData.RmUid {
// 推送到ws订阅组
wsService.NewWSSendImpl.ByGroupID(fmt.Sprintf("%s%s_%s", wsService.GROUP_KPI, neInfo.NeType, neInfo.NeId), kpiEvent)
wsService.NewWSSend.ByGroupID(fmt.Sprintf("%s%s_%s", wsService.GROUP_KPI, neInfo.NeType, neInfo.NeId), kpiEvent)
// 推送自定义KPI到ws订阅组
wsService.NewWSSendImpl.ByGroupID(fmt.Sprintf("%s%s_%s", wsService.GROUP_KPI_C, neInfo.NeType, neInfo.NeId), kpiCEvent)
wsService.NewWSSend.ByGroupID(fmt.Sprintf("%s%s_%s", wsService.GROUP_KPI_C, neInfo.NeType, neInfo.NeId), kpiCEvent)
if neInfo.NeType == "UPF" {
// 推送标识为12_RMUID, exp: 12_4400HXUPF001
wsService.NewWSSendImpl.ByGroupID(wsService.GROUP_KPI_UPF+kpiReport.Task.NE.RmUID, kpiEvent)
wsService.NewWSSend.ByGroupID(wsService.GROUP_KPI_UPF+neInfo.NeId, kpiEvent)
}
}
services.ResponseStatusOK204NoContent(w)
}
// PostGoldKPIFromNF 已废弃
// post kpi report from NEs, insert insto gold_kpi table, discard...
func PostGoldKPIFromNF(w http.ResponseWriter, r *http.Request) {
log.Debug("PostKPIReportFromNF processing... ")
vars := mux.Vars(r)
apiVer := vars["apiVersion"]
if apiVer != global.ApiVersionV1 {
log.Error("Uri api version is invalid. apiVersion:", apiVer)
services.ResponseNotFound404UriNotExist(w, r)
return
}
// body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
if err != nil {
log.Error("Faile to io.ReadAll: ", err)
services.ResponseNotFound404UriNotExist(w, r)
return
}
log.Trace("Request body:", string(body))
kpiReport := new(KpiReport)
_ = json.Unmarshal(body, &kpiReport)
log.Trace("kpiReport:", kpiReport)
session := xEngine.NewSession()
defer session.Close()
goldKpi := new(GoldKpi)
layout := time.RFC3339Nano
goldKpi.Date = GetDateFromTimeString(layout, kpiReport.Task.Period.StartTime)
goldKpi.Index, _ = strconv.Atoi(vars["index"])
goldKpi.StartTime = global.GetFmtTimeString(layout, kpiReport.Task.Period.StartTime, time.DateTime)
goldKpi.EndTime = global.GetFmtTimeString(layout, kpiReport.Task.Period.EndTime, time.DateTime)
// get time granularity from startTime and endTime
seconds, _ := global.GetSecondDuration(goldKpi.StartTime, goldKpi.EndTime)
goldKpi.Granularity = 60
if seconds != 0 && seconds <= math.MaxInt8 && seconds >= math.MinInt8 {
goldKpi.Granularity = int8(seconds)
}
goldKpi.NEName = kpiReport.Task.NE.NEName
goldKpi.RmUid = kpiReport.Task.NE.RmUID
goldKpi.NEType = kpiReport.Task.NE.NeType
goldKpi.Timestamp = global.GetFmtTimeString(layout, kpiReport.Timestamp, time.DateTime)
// 黄金指标事件对象
kpiEvent := map[string]any{
// kip_id ...
"neType": goldKpi.NEType,
"neName": goldKpi.NEName,
"rmUID": goldKpi.RmUid,
"startIndex": goldKpi.Index,
"timeGroup": goldKpi.StartTime,
}
// insert into new kpi_report_xxx table
kpiData := new(KpiData)
kpiData.Date = goldKpi.Date
kpiData.Index = goldKpi.Index
//st, _ := time.ParseInLocation(time.RFC3339Nano, kpiReport.Task.Period.StartTime, time.Local)
//et, _ := time.ParseInLocation(time.RFC3339Nano, kpiReport.Task.Period.EndTime, time.Local)
kpiData.StartTime = goldKpi.StartTime
kpiData.EndTime = goldKpi.EndTime
kpiData.Granularity = goldKpi.Granularity
kpiData.NEName = goldKpi.NEName
kpiData.NEType = goldKpi.NEType
kpiData.RmUid = goldKpi.RmUid
kpiVal := new(KPIVal)
kpiData.CreatedAt = time.Now().UnixMilli()
for _, k := range kpiReport.Task.NE.KPIs {
kpiEvent[k.KPIID] = k.Value // kip_id
goldKpi.KpiId = k.KPIID
goldKpi.Value = k.Value
goldKpi.Error = k.Err
kpiVal.KPIID = k.KPIID
kpiVal.Value = int64(k.Value)
kpiVal.Err = k.Err
kpiData.KPIValues = append(kpiData.KPIValues, *kpiVal)
//log.Trace("goldKpi:", goldKpi)
// 启动事务
err := session.Begin()
if err != nil {
log.Error("Failed to Begin gold_kpi:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
gkpi := &GoldKpi{}
_, err = session.Where("id = ?", 1).ForUpdate().Get(gkpi)
if err != nil {
// 回滚事务
session.Rollback()
log.Error("Failed to ForUpdate gold_kpi:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
affected, err := session.Insert(goldKpi)
if err != nil && affected <= 0 {
session.Rollback()
log.Error("Failed to insert gold_kpi:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
// 提交事务
err = session.Commit()
if err != nil {
log.Error("Failed to Commit gold_kpi:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
}
// insert kpi_report table, no session
tableName := "kpi_report_" + strings.ToLower(kpiReport.Task.NE.NeType)
affected, err := xEngine.Table(tableName).Insert(kpiData)
if err != nil && affected <= 0 {
log.Errorf("Failed to insert %s:%v", tableName, err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
// 推送到ws订阅组
wsService.NewWSSendImpl.ByGroupID(wsService.GROUP_KPI, kpiEvent)
if goldKpi.NEType == "UPF" {
wsService.NewWSSendImpl.ByGroupID(wsService.GROUP_KPI_UPF, kpiEvent)
}
services.ResponseStatusOK204NoContent(w)
}
type MeasureTask struct {
Tasks []Task `json:"Tasks"`
NotifyUrl string `json:"NotifyUrl"` /* "http://xEngine.xEngine.xEngine.x:xxxx/api/rest/performanceManagement/v1/elementType/smf/objectType/measureReport */

View File

@@ -1,10 +1,9 @@
package pm
import (
"nms_cxy/features/pm/kpi_c_report"
"nms_cxy/features/pm/kpi_c_title"
"nms_cxy/lib/log"
"be.ems/features/pm/kpi_c_report"
"be.ems/features/pm/kpi_c_title"
"be.ems/lib/log"
"github.com/gin-gonic/gin"
)