1
0

merge: 合并代码20241211

This commit is contained in:
TsMask
2024-12-11 16:15:13 +08:00
parent aedea2ce2d
commit 1fa9442c1f
43 changed files with 885 additions and 479 deletions

View File

@@ -40,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 {
@@ -106,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 {
@@ -185,13 +187,14 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
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 {
@@ -253,13 +256,14 @@ func (k *KpiCReport) Total(c *gin.Context) {
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 {

View File

@@ -3,17 +3,23 @@ package kpi_c_title
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"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 {
@@ -30,7 +36,10 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
if status := querys.Status; status != "" {
conditions = append(conditions, "status = ?")
params = append(params, status)
} else {
conditions = append(conditions, "status != 'Deleted'")
}
whereSql := ""
if len(conditions) > 0 {
whereSql += strings.Join(conditions, " and ")
@@ -62,6 +71,8 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
return
}
k.expressionAlias(titles, i18n)
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
//c.JSON(http.StatusOK, titles)
}
@@ -70,6 +81,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 != "" {
@@ -79,7 +91,10 @@ func (k *KpiCTitle) Get(c *gin.Context) {
if status := c.Query("status"); status != "" {
conditions = append(conditions, "status = ?")
params = append(params, status)
} else {
conditions = append(conditions, "status != 'Deleted'")
}
whereSql := ""
if len(conditions) > 0 {
whereSql += strings.Join(conditions, " and ")
@@ -89,10 +104,38 @@ func (k *KpiCTitle) Get(c *gin.Context) {
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
@@ -105,7 +148,10 @@ func (k *KpiCTitle) Total(c *gin.Context) {
if status := c.Query("status"); status != "" {
conditions = append(conditions, "status = ?")
params = append(params, status)
} else {
conditions = append(conditions, "status != 'Deleted'")
}
whereSql := ""
if len(conditions) > 0 {
whereSql += strings.Join(conditions, " and ")
@@ -120,17 +166,49 @@ 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.StatusOK, services.ErrResp(err.Error()))
return
}
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.StatusOK, services.ErrResp("custom indicator already exist"))
return
}
// Regexp match like AMF.C.01
kpiIDRegexp := "^" + *title.NeType + "\\.C\\.[0-9]{2}$"
ret := dborm.DefaultDB().Table("kpi_c_title").
Where("ne_type=? and kpi_id REGEXP ? ORDER BY kpi_id DESC LIMIT 1", title.NeType, kpiIDRegexp).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.StatusOK, services.ErrResp(err.Error()))
return
@@ -178,8 +256,8 @@ func (k *KpiCTitle) Put(c *gin.Context) {
func (k *KpiCTitle) Delete(c *gin.Context) {
id := c.Param("id")
if err := dborm.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
c.JSON(http.StatusOK, services.ErrResp("custom indicator not found"))
if err := dborm.DefaultDB().Table(k.TableName()).Where("id=?", id).Update("status", "Deleted").Error; err != nil {
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
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

@@ -762,14 +762,14 @@ func GetStateFromNF(w http.ResponseWriter, r *http.Request) {
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
Get(requestURI2NF)
if err != nil {
log.Error("Get system state from NF is failed:", err)
log.Error("Fail to get state:", err)
errorMessage := services.ErrorMessage{
ErrorCode: "1", ErrorInfo: "Internal server error, NF connnect refused",
}
result["error"] = errorMessage
SN, Version, _ := dborm.XormGetNEStateInfo(ne.NeType, ne.NeId)
result["serialNum"] = SN
result["version"] = Version
systemState := make(map[string]interface{})
systemState["error"] = errorMessage
result["systemState"] = systemState
} else {
systemState := make(map[string]interface{})
_ = json.Unmarshal(resp.Body(), &systemState)
@@ -786,49 +786,62 @@ func GetStateFromNF(w http.ResponseWriter, r *http.Request) {
response.Data = data
services.ResponseWithJson(w, http.StatusOK, response)
return
}
if neType == "omc" {
} else if neType == "omc" {
emsState := GetEMSState("127.0.0.1")
services.ResponseWithJson(w, http.StatusOK, emsState)
return
}
var neList []dborm.NeInfo
err := dborm.XormGetNeInfoByNeType(neType, &neList)
if err != nil {
log.Error("Failed to dborm.XormGetNeInfoByNeType:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
data := make([]map[string]interface{}, 0)
for _, ne := range neList {
hostUri := fmt.Sprintf("http://%s:%v", ne.Ip, ne.Port)
requestURI2NF := fmt.Sprintf("%s/api/rest/systemManagement/v1/elementType/%s/objectType/systemState",
hostUri, strings.ToLower(ne.NeType))
log.Debug("requestURI2NF:", requestURI2NF)
// only support omc and all elementType
err := fmt.Errorf("only support omc or all elementTypeValue")
log.Error("Fail to get state:", err)
services.ResponseInternalServerError500ProcessError(w, err)
resp, err := client.R().
EnableTrace().
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
Get(requestURI2NF)
if err != nil {
log.Error("Get system state from NF is failed:", err)
} else {
systemState := make(map[string]interface{})
_ = json.Unmarshal(resp.Body(), &systemState)
data = append(data, systemState)
}
}
// var neList []dborm.NeInfo
// err := dborm.XormGetNeInfoByNeType(neType, &neList)
// if err != nil {
// log.Error("Failed to dborm.XormGetNeInfoByNeType:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// var omcList []dborm.NeInfo
// err = dborm.XormGetNeInfoByNeType("omc", &omcList)
// if err != nil {
// log.Error("Failed to omc ne list:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// for i, ne := range neList {
if len(data) == 1 {
services.ResponseWithJson(w, http.StatusOK, data[0])
return
}
var response Response
response.Data = data
services.ResponseWithJson(w, http.StatusOK, response)
// }
// data := make([]map[string]interface{}, 0)
// for _, ne := range neList {
// hostUri := fmt.Sprintf("http://%s:%v", ne.Ip, ne.Port)
// requestURI2NF := fmt.Sprintf("%s/api/rest/systemManagement/v1/elementType/%s/objectType/systemState",
// hostUri, strings.ToLower(ne.NeType))
// log.Debug("requestURI2NF:", requestURI2NF)
// resp, err := client.R().
// EnableTrace().
// SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
// SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
// Get(requestURI2NF)
// if err != nil {
// log.Error("Get system state from NF is failed:", err)
// } else {
// systemState := make(map[string]interface{})
// _ = json.Unmarshal(resp.Body(), &systemState)
// data = append(data, systemState)
// }
// }
// if len(data) == 1 {
// services.ResponseWithJson(w, http.StatusOK, data[0])
// return
// }
// var response Response
// response.Data = data
// services.ResponseWithJson(w, http.StatusOK, response)
}
// GetStateFromNF 旧函数