add: custom kpi

This commit is contained in:
2024-08-09 10:31:30 +08:00
parent 299d618551
commit 42c865c385
7 changed files with 185 additions and 40 deletions

View File

@@ -5,10 +5,20 @@ import (
"net/http"
"strings"
"be.ems/lib/services"
"be.ems/src/framework/datasource"
"github.com/gin-gonic/gin"
)
const (
CodeKeyName = "code"
MsgKeyName = "message"
DataKeyName = "data"
CodeError = 0
CodeSuccess = 1
TotalKeyName = "total"
)
func (k *KpiCReport) Get(c *gin.Context) {
var reports []KpiCReport
var conditions []string
@@ -16,7 +26,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
var querys KpiCReportQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
@@ -25,7 +35,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(querys.NeType))
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "Not found NE type"})
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
return
}
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
@@ -62,20 +72,21 @@ func (k *KpiCReport) Get(c *gin.Context) {
//err := datasource.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
err := dborm.Find(&reports).Error
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
//c.JSON(http.StatusOK, map[string]any{"data": titles})
c.JSON(http.StatusOK, reports)
c.JSON(http.StatusOK, services.DataResp(reports))
//c.JSON(http.StatusOK, reports)
}
func (k *KpiCReport) Total(c *gin.Context) {
func (k *KpiCReport) GetTotalList(c *gin.Context) {
var reports []KpiCReport
var conditions []string
var params []any
var querys KpiCReportQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
@@ -84,7 +95,73 @@ func (k *KpiCReport) Total(c *gin.Context) {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(querys.NeType))
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "Not found NE type"})
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
return
}
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
dborm := datasource.DefaultDB().Table(tableName)
if querys.StartTime != "" {
conditions = append(conditions, "created_at >= ?")
params = append(params, querys.StartTime)
}
if querys.EndTime != "" {
conditions = append(conditions, "created_at <= ?")
params = append(params, querys.EndTime)
}
whereSql := ""
if len(conditions) > 0 {
whereSql += strings.Join(conditions, " and ")
dborm = dborm.Where(whereSql, params...)
}
// page number and size
if pageSize := querys.PageSize; pageSize > 0 {
dborm = dborm.Limit(pageSize)
if pageNum := querys.PageNum; pageNum > 0 {
dborm = dborm.Offset((pageNum - 1) * pageSize)
}
}
// order by
if sortField, sortOrder := querys.SortField, querys.SortOrder; sortField != "" && sortOrder != "" {
orderBy := fmt.Sprintf("%s %s", sortField, sortOrder)
dborm = dborm.Order(orderBy)
}
//err := datasource.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
err := dborm.Find(&reports).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
var total int64 = 0
err = dborm.Count(&total).Error
if err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusOK, services.TotalDataResp(reports, total))
//c.JSON(http.StatusOK, reports)
}
func (k *KpiCReport) Total(c *gin.Context) {
var conditions []string
var params []any
var querys KpiCReportQuery
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
// construct condition to get
if querys.NeType != "" {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(querys.NeType))
} else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
return
}
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
@@ -107,25 +184,25 @@ func (k *KpiCReport) Total(c *gin.Context) {
var total int64 = 0
err := dborm.Count(&total).Error
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusOK, map[string]any{"total": total})
c.JSON(http.StatusOK, services.TotalResp(total))
}
func (k *KpiCReport) Post(c *gin.Context) {
var report KpiCReport
if err := c.ShouldBindJSON(&report); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
if err := datasource.DefaultDB().Create(&report).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusCreated, report)
c.JSON(http.StatusCreated, services.DataResp(report))
}
func (k *KpiCReport) Put(c *gin.Context) {
@@ -133,23 +210,23 @@ func (k *KpiCReport) Put(c *gin.Context) {
id := c.Param("id")
if err := datasource.DefaultDB().First(&report, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "KPI report not found"})
c.JSON(http.StatusNotFound, services.ErrResp("KPI report not found"))
return
}
if err := c.ShouldBindJSON(&report); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
datasource.DefaultDB().Save(&report)
c.JSON(http.StatusOK, report)
c.JSON(http.StatusOK, services.DataResp(report))
}
func (k *KpiCReport) Delete(c *gin.Context) {
id := c.Param("id")
if err := datasource.DefaultDB().Delete(&KpiCReport{}, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "KPI report not found"})
c.JSON(http.StatusNotFound, services.ErrResp("KPI report not found"))
return
}

View File

@@ -17,17 +17,17 @@ type KpiCValues []KpiCVal
type KpiCReport struct {
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
NeType *string `gorm:"column:ne_type;default:NULL" json:"ne_type,omitempty"`
NeName *string `gorm:"column:ne_name;default:" json:"ne_name,omitempty"`
RmUID *string `gorm:"column:rm_uid;default:NULL" json:"rm_uid,omitempty"`
NeType *string `gorm:"column:ne_type;default:NULL" json:"neType,omitempty"`
NeName *string `gorm:"column:ne_name;default:" json:"neName,omitempty"`
RmUID *string `gorm:"column:rm_uid;default:NULL" json:"rmUid,omitempty"`
Date string `gorm:"column:date" json:"date"` // time.Time `gorm:"column:date" json:"date"`
StartTime *string `gorm:"column:start_time;default:NULL" json:"start_time,omitempty"`
EndTime *string `gorm:"column:end_time;default:NULL" json:"end_time,omitempty"`
StartTime *string `gorm:"column:start_time;default:NULL" json:"startTime,omitempty"`
EndTime *string `gorm:"column:end_time;default:NULL" json:"endTime,omitempty"`
Index int16 `gorm:"column:index" json:"index"`
Granularity *int8 `gorm:"column:granularity;default:60" json:"granularity,omitempty"` //Time granualarity: 5/10/.../60/300 (second)
KpiValues KpiCValues `gorm:"column:kpi_values;type:json" json:"kpi_values,omitempty"`
CreatedAt *time.Time `gorm:"column:created_at;default:current_timestamp()" json:"created_at,omitempty"`
TenantID *string `gorm:"column:tenant_id;default:NULL" json:"tenant_id,omitempty"`
KpiValues KpiCValues `gorm:"column:kpi_values;type:json" json:"kpiValues,omitempty"`
CreatedAt *time.Time `gorm:"column:created_at;default:current_timestamp()" json:"createdAt,omitempty"`
TenantID *string `gorm:"column:tenant_id;default:NULL" json:"tenantID,omitempty"`
}
type KpiCReportQuery struct {

View File

@@ -15,6 +15,10 @@ func Register(r *gin.RouterGroup) {
middleware.PreAuthorize(nil),
k.Get,
)
pmKPIC.GET("/report/totalList",
middleware.PreAuthorize(nil),
k.Total,
)
pmKPIC.GET("/report/total",
middleware.PreAuthorize(nil),
k.Total,

View File

@@ -4,10 +4,42 @@ import (
"net/http"
"strings"
"be.ems/lib/services"
"be.ems/src/framework/datasource"
"github.com/gin-gonic/gin"
)
func (k *KpiCTitle) GetToalList(c *gin.Context) {
var titles []KpiCTitle
var conditions []string
var params []any
// construct condition to get
if neType := c.Query("neType"); neType != "" {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.ToUpper(neType))
}
if status := c.Query("status"); status != "" {
conditions = append(conditions, "status = ?")
params = append(params, status)
}
whereSql := ""
if len(conditions) > 0 {
whereSql += strings.Join(conditions, " and ")
}
if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
var total int64 = 0
if err := datasource.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
//c.JSON(http.StatusOK, titles)
}
func (k *KpiCTitle) Get(c *gin.Context) {
var titles []KpiCTitle
var conditions []string
@@ -27,11 +59,12 @@ func (k *KpiCTitle) Get(c *gin.Context) {
whereSql += strings.Join(conditions, " and ")
}
if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
//c.JSON(http.StatusOK, map[string]any{"data": titles})
c.JSON(http.StatusOK, titles)
c.JSON(http.StatusOK, services.DataResp(titles))
//c.JSON(http.StatusOK, titles)
}
func (k *KpiCTitle) Total(c *gin.Context) {
@@ -53,25 +86,26 @@ func (k *KpiCTitle) Total(c *gin.Context) {
}
var total int64 = 0
if err := datasource.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusOK, map[string]any{"total": total})
c.JSON(http.StatusOK, services.TotalResp(total))
}
func (k *KpiCTitle) Post(c *gin.Context) {
var title KpiCTitle
if err := c.ShouldBindJSON(&title); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
if err := datasource.DefaultDB().Create(&title).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
return
}
c.JSON(http.StatusCreated, title)
c.JSON(http.StatusCreated, services.DataResp(title))
}
func (k *KpiCTitle) Put(c *gin.Context) {
@@ -79,12 +113,12 @@ func (k *KpiCTitle) Put(c *gin.Context) {
id := c.Param("id")
if err := datasource.DefaultDB().First(&title, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Title not found"})
c.JSON(http.StatusNotFound, services.ErrResp("Title not found"))
return
}
if err := c.ShouldBindJSON(&title); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
return
}
datasource.DefaultDB().Save(&title)
@@ -95,7 +129,7 @@ func (k *KpiCTitle) Delete(c *gin.Context) {
id := c.Param("id")
if err := datasource.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Title not found"})
c.JSON(http.StatusNotFound, services.ErrResp("Title not found"))
return
}

View File

@@ -4,14 +4,14 @@ import "time"
type KpiCTitle struct {
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
NeType *string `gorm:"column:ne_type;default:NULL," json:"ne_type,omitempty"`
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpi_id,omitempty"`
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"`
Status *string `gorm:"column:status" json:"status,omitempty"`
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`
CreatedBy *string `gorm:"column:created_by;default:NULL," json:"created_by,omitempty"`
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updated_at,omitempty"`
CreatedBy *string `gorm:"column:created_by;default:NULL," json:"createdBy,omitempty"`
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updatedAt,omitempty"`
}
func (k *KpiCTitle) TableName() string {

View File

@@ -19,6 +19,10 @@ func Register(r *gin.RouterGroup) {
middleware.PreAuthorize(nil),
k.Total,
)
pmKPIC.GET("/title/totalList",
middleware.PreAuthorize(nil),
k.GetToalList,
)
pmKPIC.POST("/title",
middleware.PreAuthorize(nil),
k.Post,

26
lib/services/response.go Normal file
View File

@@ -0,0 +1,26 @@
package services
const (
CODE_FAIL = 0
CODE_SUCC = 1
)
func ErrResp(msg string) map[string]any {
return map[string]any{"code": CODE_FAIL, "message": msg}
}
func DataResp(data any) map[string]any {
return map[string]any{"code": CODE_SUCC, "data": data}
}
func SuccResp() map[string]any {
return map[string]any{"code": CODE_SUCC, "message": "success"}
}
func TotalResp(total int64) map[string]any {
return map[string]any{"code": CODE_SUCC, "total": total}
}
func TotalDataResp(data any, total int64) map[string]any {
return map[string]any{"code": CODE_SUCC, "data": data, "total": total}
}