merge from main branch about customized kpi
This commit is contained in:
@@ -40,11 +40,11 @@ func (k *KpiCReport) Get(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if querys.StartTime != "" {
|
if querys.StartTime != "" {
|
||||||
conditions = append(conditions, "created_at >= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||||
params = append(params, querys.StartTime)
|
params = append(params, querys.StartTime)
|
||||||
}
|
}
|
||||||
if querys.EndTime != "" {
|
if querys.EndTime != "" {
|
||||||
conditions = append(conditions, "created_at <= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
|
||||||
params = append(params, querys.EndTime)
|
params = append(params, querys.EndTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,11 +106,11 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if querys.StartTime != "" {
|
if querys.StartTime != "" {
|
||||||
conditions = append(conditions, "created_at >= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||||
params = append(params, querys.StartTime)
|
params = append(params, querys.StartTime)
|
||||||
}
|
}
|
||||||
if querys.EndTime != "" {
|
if querys.EndTime != "" {
|
||||||
conditions = append(conditions, "created_at <= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
|
||||||
params = append(params, querys.EndTime)
|
params = append(params, querys.EndTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,11 +185,11 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
|
|||||||
dbg := dborm.DefaultDB().Table(tableName)
|
dbg := dborm.DefaultDB().Table(tableName)
|
||||||
|
|
||||||
if querys.StartTime != "" {
|
if querys.StartTime != "" {
|
||||||
conditions = append(conditions, "created_at >= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||||
params = append(params, querys.StartTime)
|
params = append(params, querys.StartTime)
|
||||||
}
|
}
|
||||||
if querys.EndTime != "" {
|
if querys.EndTime != "" {
|
||||||
conditions = append(conditions, "created_at <= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
|
||||||
params = append(params, querys.EndTime)
|
params = append(params, querys.EndTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,11 +253,11 @@ func (k *KpiCReport) Total(c *gin.Context) {
|
|||||||
dbg := dborm.DefaultDB().Table(tableName)
|
dbg := dborm.DefaultDB().Table(tableName)
|
||||||
|
|
||||||
if querys.StartTime != "" {
|
if querys.StartTime != "" {
|
||||||
conditions = append(conditions, "created_at >= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||||
params = append(params, querys.StartTime)
|
params = append(params, querys.StartTime)
|
||||||
}
|
}
|
||||||
if querys.EndTime != "" {
|
if querys.EndTime != "" {
|
||||||
conditions = append(conditions, "created_at <= ?")
|
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
|
||||||
params = append(params, querys.EndTime)
|
params = append(params, querys.EndTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ package kpi_c_title
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"be.ems/lib/dborm"
|
"be.ems/lib/dborm"
|
||||||
|
"be.ems/lib/log"
|
||||||
"be.ems/lib/services"
|
"be.ems/lib/services"
|
||||||
|
"be.ems/src/framework/utils/ctx"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,6 +18,7 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
|
|||||||
var titles []KpiCTitle
|
var titles []KpiCTitle
|
||||||
var conditions []string
|
var conditions []string
|
||||||
var params []any
|
var params []any
|
||||||
|
i18n := ctx.AcceptLanguage(c)
|
||||||
|
|
||||||
var querys KpiCTitleQuery
|
var querys KpiCTitleQuery
|
||||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||||
@@ -62,6 +67,8 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
k.expressionAlias(titles, i18n)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
|
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
|
||||||
//c.JSON(http.StatusOK, titles)
|
//c.JSON(http.StatusOK, titles)
|
||||||
}
|
}
|
||||||
@@ -70,6 +77,7 @@ func (k *KpiCTitle) Get(c *gin.Context) {
|
|||||||
var titles []KpiCTitle
|
var titles []KpiCTitle
|
||||||
var conditions []string
|
var conditions []string
|
||||||
var params []any
|
var params []any
|
||||||
|
i18n := ctx.AcceptLanguage(c)
|
||||||
|
|
||||||
// construct condition to get
|
// construct condition to get
|
||||||
if neType := c.Query("neType"); neType != "" {
|
if neType := c.Query("neType"); neType != "" {
|
||||||
@@ -89,10 +97,37 @@ func (k *KpiCTitle) Get(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
k.expressionAlias(titles, i18n)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, services.DataResp(titles))
|
c.JSON(http.StatusOK, services.DataResp(titles))
|
||||||
//c.JSON(http.StatusOK, 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) {
|
func (k *KpiCTitle) Total(c *gin.Context) {
|
||||||
var conditions []string
|
var conditions []string
|
||||||
var params []any
|
var params []any
|
||||||
@@ -120,17 +155,45 @@ func (k *KpiCTitle) Total(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KpiCTitle) Post(c *gin.Context) {
|
func (k *KpiCTitle) Post(c *gin.Context) {
|
||||||
var title KpiCTitle
|
var title, res KpiCTitle
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&title); err != nil {
|
if err := c.ShouldBindJSON(&title); err != nil {
|
||||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||||
return
|
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)
|
result := dborm.DefaultDB().Where("ne_type=? and (kpi_id=? or title=?)", title.NeType, title.KpiID, title.Title).First(&title)
|
||||||
if result.RowsAffected > 0 {
|
if result.RowsAffected > 0 {
|
||||||
c.JSON(http.StatusOK, services.ErrResp("custom indicator already exist"))
|
c.JSON(http.StatusOK, services.ErrResp("custom indicator already exist"))
|
||||||
return
|
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 {
|
if err := dborm.DefaultDB().Create(&title).Error; err != nil {
|
||||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -2,12 +2,17 @@ package kpi_c_title
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
const (
|
||||||
|
MAX_KPI_C_ID = 99
|
||||||
|
)
|
||||||
|
|
||||||
type KpiCTitle struct {
|
type KpiCTitle struct {
|
||||||
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
|
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
|
||||||
NeType *string `gorm:"column:ne_type;default:NULL," json:"neType,omitempty"`
|
NeType *string `gorm:"column:ne_type;default:NULL," json:"neType,omitempty"`
|
||||||
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpiId,omitempty"`
|
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpiId,omitempty"`
|
||||||
Title *string `gorm:"column:title;default:NULL," json:"title,omitempty"`
|
Title *string `gorm:"column:title;default:NULL," json:"title,omitempty"`
|
||||||
Expression *string `gorm:"column:expression;default:NULL," json:"expression,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"`
|
Status string `gorm:"column:status;default:'Active'" json:"status"`
|
||||||
Unit *string `gorm:"column:unit" json:"unit,omitempty"`
|
Unit *string `gorm:"column:unit" json:"unit,omitempty"`
|
||||||
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`
|
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`
|
||||||
|
|||||||
@@ -302,7 +302,7 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
"neName": kpiData.NEName,
|
"neName": kpiData.NEName,
|
||||||
"rmUID": kpiData.RmUid,
|
"rmUID": kpiData.RmUid,
|
||||||
"startIndex": kpiData.Index,
|
"startIndex": kpiData.Index,
|
||||||
"timeGroup": kpiData.Date[:10] + " " + kpiData.StartTime,
|
"timeGroup": kpiData.Date[:10] + " " + kpiData.EndTime,
|
||||||
"createdAt": kpiData.CreatedAt,
|
"createdAt": kpiData.CreatedAt,
|
||||||
"granularity": kpiData.Granularity,
|
"granularity": kpiData.Granularity,
|
||||||
"tenantID": kpiData.TenantID,
|
"tenantID": kpiData.TenantID,
|
||||||
|
|||||||
Reference in New Issue
Block a user