From 0f587196d0299ddf2fae747540fa5524482a2da5 Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Fri, 8 Nov 2024 11:46:08 +0800 Subject: [PATCH 1/2] merge from main branch about customized kpi --- features/pm/kpi_c_report/controller.go | 16 +++---- features/pm/kpi_c_title/controller.go | 65 +++++++++++++++++++++++++- features/pm/kpi_c_title/model.go | 5 ++ features/pm/performance.go | 2 +- 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/features/pm/kpi_c_report/controller.go b/features/pm/kpi_c_report/controller.go index 1b88013c..a19646b3 100644 --- a/features/pm/kpi_c_report/controller.go +++ b/features/pm/kpi_c_report/controller.go @@ -40,11 +40,11 @@ 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) } @@ -106,11 +106,11 @@ 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) } @@ -185,11 +185,11 @@ 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) } @@ -253,11 +253,11 @@ 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) } diff --git a/features/pm/kpi_c_title/controller.go b/features/pm/kpi_c_title/controller.go index aa2912ce..380d4665 100644 --- a/features/pm/kpi_c_title/controller.go +++ b/features/pm/kpi_c_title/controller.go @@ -3,10 +3,14 @@ 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" ) @@ -14,6 +18,7 @@ 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 { @@ -62,6 +67,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 +77,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 != "" { @@ -89,10 +97,37 @@ func (k *KpiCTitle) Get(c *gin.Context) { return } + k.expressionAlias(titles, i18n) + c.JSON(http.StatusOK, services.DataResp(titles)) //c.JSON(http.StatusOK, titles) } +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 @@ -120,17 +155,45 @@ 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 } + 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.StatusOK, services.ErrResp(err.Error())) return diff --git a/features/pm/kpi_c_title/model.go b/features/pm/kpi_c_title/model.go index 77bb9423..cdc2b5cc 100644 --- a/features/pm/kpi_c_title/model.go +++ b/features/pm/kpi_c_title/model.go @@ -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"` diff --git a/features/pm/performance.go b/features/pm/performance.go index 577886dd..cffa01a8 100644 --- a/features/pm/performance.go +++ b/features/pm/performance.go @@ -302,7 +302,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, "tenantID": kpiData.TenantID, From f55983492d9e8eebd68bfdf940452d007385d127 Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Fri, 8 Nov 2024 15:42:26 +0800 Subject: [PATCH 2/2] release 2.2411.1 --- captrace/makefile | 2 +- crontask/makefile | 2 +- data2html/makefile | 2 +- makefile | 2 +- mkpkg.sh | 2 +- restagent/makefile | 2 +- src/framework/config/config/config.default.yaml | 2 +- sshsvc/makefile | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/captrace/makefile b/captrace/makefile index ffc38920..ab59c0ac 100644 --- a/captrace/makefile +++ b/captrace/makefile @@ -1,7 +1,7 @@ # Makefile for rest agent project PROJECT = OMC -VERSION = 2.2410.2 +VERSION = 2.2411.1 PLATFORM = amd64 ARMPLATFORM = aarch64 BUILDDIR = ../../build diff --git a/crontask/makefile b/crontask/makefile index 1b28282c..b4ea2b88 100644 --- a/crontask/makefile +++ b/crontask/makefile @@ -1,7 +1,7 @@ # Makefile for OMC-OMC-crontask project PROJECT = OMC -VERSION = 2.2410.2 +VERSION = 2.2411.1 LIBDIR = be.ems/lib BINNAME = crontask diff --git a/data2html/makefile b/data2html/makefile index 01f685a0..c5196d84 100644 --- a/data2html/makefile +++ b/data2html/makefile @@ -1,7 +1,7 @@ # Makefile for rest agent project PROJECT = OMC -VERSION = 2.2410.2 +VERSION = 2.2411.1 RelDate = `date +%Y%m%d` Release = $(RelDate) RelVer = $(VERSION)-$(RelDate) diff --git a/makefile b/makefile index 8f279075..1fd234ce 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ ProjectL = omc ProjectU = OMC PROJECT = $(ProjectL) -VERSION = 2.2410.2 +VERSION = 2.2411.1 RelDate = `date +%Y%m%d` Release = $(RelDate) RelVer = $(VERSION)-$(RelDate) diff --git a/mkpkg.sh b/mkpkg.sh index 262c31ca..c02cec51 100755 --- a/mkpkg.sh +++ b/mkpkg.sh @@ -2,7 +2,7 @@ ProcList="restagent crontask sshsvc captrace data2html" ProjectL=omc -VERSION=2.2410.2 +VERSION=2.2411.1 RelDate=`date +%Y%m%d` Release=${RelDate} RelVer=${VERSION}-${RelDate} diff --git a/restagent/makefile b/restagent/makefile index a8aa4ff5..99bc86aa 100644 --- a/restagent/makefile +++ b/restagent/makefile @@ -1,7 +1,7 @@ # Makefile for rest agent project PROJECT = OMC -VERSION = 2.2410.2 +VERSION = 2.2411.1 PLATFORM = amd64 ARMPLATFORM = aarch64 BUILDDIR = ../../build diff --git a/src/framework/config/config/config.default.yaml b/src/framework/config/config/config.default.yaml index dc60eac3..5fe3f018 100644 --- a/src/framework/config/config/config.default.yaml +++ b/src/framework/config/config/config.default.yaml @@ -1,7 +1,7 @@ # 项目信息 framework: name: "OMC" - version: "2.2410.2" + version: "2.2411.1" # 应用服务配置 server: diff --git a/sshsvc/makefile b/sshsvc/makefile index 961ff039..2d258b9c 100644 --- a/sshsvc/makefile +++ b/sshsvc/makefile @@ -1,7 +1,7 @@ # Makefile for OMC-OMC-crontask project PROJECT = OMC -VERSION = 2.2410.2 +VERSION = 2.2411.1 LIBDIR = be.ems/lib BINNAME = sshsvc